代码编写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
position: absolute;
width: 200px;
height: 200px;
background-color: pink;
top: 50%;
transform: translate(0, -50%);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f82f3532b6b40738029d471e4fac7c13.png)
正确操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html,body{
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.box{
width: 200px;
height: 200px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bf132ae552fc653b4b7e18697100fa45.png)