盒子垂直居中

定位方式(盒子宽高已知)

1
2
3
4
5
position:absolute;
left:50%;
top:50px;
margi-left:-(自身宽度的一半)
margin-top:-(自身高度的一半)

table-cell

1
2
3
4
5
6
7
//父级
display:table-cell;
vertical:center;
height:200px;
width:300px;
//子级
margin:0 auto

flex

1
2
3
4
display:flex;
display:-webkit-flex;
align-items:center;
justify-content:center;

trabsform(适用于子盒子宽高不确定的)

1
2
3
4
5
6
7
8
9
//父亲
position:relative;
width:500px;
height:200px;
//子级
position:absolute;
top:50%;
left:50%;
transform:translate(50%,50%)//注意:这里启用了3D加速,会比较消耗CPU资源

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器