How to align div horizontally and vertically, and align div horizontally and vertically
How to achieve div vertical and center effects:
Sometimes we may need to center a div in its parent container. Check the code instance before analyzing it.
Code example:
<! DOCTYPE html>
In the above Code, the position attribute is used to implement the vertical center effect of the object. After using top: 50% and left: 50% for the child object, the center of the object is not implemented, but the vertical center of the point in the upper left corner of the object is realized, as shown in the figure below:
We need to center the center of the object, so that the entire object is centered, so we use margin: -50px 0 0-100px: Move the object up and left half of the object height and half of the width.
Note:
The parent object adopts absolute positioning. Otherwise, the absolute positioning of the sub-object takes window as the reference object.
Original address: http://www.51texiao.cn/div_cssjiaocheng/2015/0503/586.html
Most original address: http://www.softwhy.com