加了绝对定位的盒子不能通过margin:0 auto 水平居中,但是可以先让盒子左移父级元素水平中心位置(left:50%),然后再让盒子向左移动自身宽度的一半。同理,给相同的top值,将盒子移动到父级盒子中心。
<style>
* {
margin: 0;
padding: 0;
}
.fu {
position: relative;
width: 500px;
height: 500px;
background-color: pink;
}
.fu div {
position: absolute;
left: 50%;
margin-left: -100px;
top: 50%;
margin-top: -100px;
width: 200px;
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="fu">
<div>123</div>
</div>
</body>```