代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body {
display: flex;
align-items: center;
/*定义body的元素垂直居中*/
justify-content: center;
/*定义body的里的元素水平居中*/
}
.div {
width: 100px;
height: 100px;
background-color: black;
}
</style>
<body>
<div class="div">
</div>
</body>
</html>
去掉就可以实现水平竖直居中了,但是加上就只能实现水平居中,不能理解。