负marginTop的方式垂直居中
css吧
全部回复
仅看楼主
level 11
狼黎殇83c 楼主
已知元素高度后,使用绝对定位将top设置为50%,mergin-top设置为内容高度的一半(height + padding) / 2;内容超过元素高度时需要设置overflow决定滚动条的出现。
原理:top:50%元素上边界位于包含框中点,设置负外边界使得元素垂直中心与包含框中心重合;
.negative_margin_top{
position:absolute;
top:50%;
left:0;
right:0;
margin:auto;
margin-top:-100px; /*-(height+padding)/2*/
width:200px;
height:200px;
}
2023年01月29日 01点01分 1
level 11
狼黎殇83c 楼主
优点:代码量少、浏览器兼容性高支持ie6 ie7
缺点:不支持响应式(不能使用百分比、min/max-width)
2023年01月29日 01点01分 2
1