方法一
.element {
width: 600px; height: 400px;
position: absolute; left: 0; top: 0; right: 0; bottom: 0;
margin: auto; /* 有了这个就自动居中了 */
}
方法二
.element{
width: 100px;
position: absolute;
left: 50%;
margin-left: -50px;
}
2个方法都需要有个宽度
本文介绍了两种使用CSS实现页面元素水平垂直居中的方法。方法一利用绝对定位与自动外边距,使元素在其父容器中居中。方法二通过设置元素宽度、左偏移50%及负左边距,实现相同效果。两者均需定义元素宽度。

1万+

被折叠的 条评论
为什么被折叠?



