垂直居中

1、div的水平居中:

margin:0 auto

有高度和宽度的垂直居中:

如果想水平加垂直居中的DIV已知高度和宽度,是最好办的了! 
1、先让这个DIV绝对定位; 
2、让他距离上边50%,左边50%;这会这个DIV的左上角这个点就是窗口的正中间; 
3、因为已经知道了这个DIV的高和宽了,那么再从这里点向左移动总宽的一半就可以了, 向上呢也同理;

下面的只是适合absolute的,relative是不适合的。


/*垂直居中,所以一共有三种水平垂直居中的方法

*/
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;

 

或者通过计算高度和宽度:

top: 50%;
left: 50%;
margin: 一半,一半;

不知道高度和宽度的水平垂直居中

position: absolute;
top: 50%;
left: 50%;
transform: translate("-50%","-50%");

原文地址:https://www.cnblogs.com/caicaihong/p/9598999.html