Yesterday in the knowledge of various look at the front end of the post, accidentally and learned a skill, again I moved to share with you.
Still the same problem:
How do I get a div centered in the parent element?
In the previous article we used the absolute positioning Position:absolute;
Combining the negative value of margin achieves the center effect.
The second way of solving
Here we look at the new approach:
Html:
<div class= "Container" >
<div class= "box" ></div>
</div>
Css:
. container{
width:300px;
height:300px;
position:relative;
}
. box{
width:50px;
height:50px;
Margin:auto;
Position:absolute;
top:0;
bottom:0;
left:0;
right:0;
}
It's done! ('? ') )
Principle
There's no margin to count! This method directly fix the centering problem.
We set the absolute positioning of box, up and down are 0;
Then the box will be confused, in the end where to go???
Forget it, I'll stay in the middle, qaq.
Think about it and suddenly think this box is cute, hahaha.
CSS in the image using absolute positioning to achieve the center effect [second article]