一,定位
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{
width: 200px;
height: 200px;
background-color: red;
position:absolute;
top:50%;
left: 50%;
margin-left: -100px;//盒子宽度的一半
margin-top: -100px;//盒子高度的一半
}
</style>
</head>
<body>
<div></div>
</body>
</html>
二,flex布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
html{
height: 100vh;//vh意味viewport height,也就是可视窗口高度。
display:flex;
justify-content: center;//水平方向对齐
align-items: center;//垂直方向对齐
}
div{
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div></div>
</body>
</html>