css中三种绝对定位元素的水平垂直居中的方法

作者:袖梨 2022-06-25

1.css实现居中

缺点:需要提前知道元素的宽度和高度。

Document
.box{
width: 600px;
height: 400px;
position: absolute;
left: 50%; top: 50%;
border:1px solid #000;
background:red;
margin-top: -200px; /* 高度的一半 */
margin-left: -300px; /* 宽度的一半 */
}

相关文章

精彩推荐