代码如图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.box {
position: absolute;
width: 100px;
height: 100px;
background-color: pink;
/* 加了绝对定位的盒子不能通过margin:0 auto; 进行水平居中 */
margin: 0 auto;
}
</style>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.box {
position: absolute;
width: 100px;
height: 100px;
background-color: pink;
/* 加了绝对定位的盒子不能通过margin:0 auto; 进行水平居中 */
/* margin: 0 auto; */
left: 50%;
margin-left: -100px;
}
</style>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.box {
position: absolute;
width: 100px;
height: 100px;
background-color: pink;
/* 加了绝对定位的盒子不能通过margin:0 auto; 进行水平居中 */
/* margin: 0 auto; */
/* 水平居中 */
left: 50%;
margin-left: -100px;
/* 垂直居中 */
top: 50%;
margin-top: -100px;
}
</style>
<body>
<div class="box"></div>
</body>
</html>