总结的八种方式嘎嘎好使
<!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>
<style>
* {
margin: 0;
padding: 0;
}
.parent {
width: 100vw;
height: 100vh;
background: pink;
}
.son {
width: 100px;
height: 100px;
background: blue;
}
/* 1 */
/* .parent{
position: relative;
}
.son{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
} */
/* 2 */
/* .parent{
position: relative;
}
.son{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
} */
/* 3 */
/* .parent{
display: flex;
justify-content: center;
align-items: center;
} */
/* 4 */
/* .parent{
display: flex;
}
.son{
margin: auto;
} */
/* 5 */
/* .parent{
display: grid;
}
.son{
display: inline-block;
align-self: center;
justify-self: center;
} */
/* 6 */
.parent {
display: grid;
place-items: center;
}
/* 7 */
/* .parent{
display: -webkit-box;
-webkit-box-align: center;
-webkit-box-pack: center;
} */
/* 8 */
/* .parent {
display: table-cell;
vertical-align: middle;
}
.son {
margin: auto;
} */
</style>
</head>
<body>
<div class="parent">
<div class="son"></div>
</div>
</body>
</html>
可以都试一试,就不一一说明了,都很简单,当然还有很多的方法,只是本人没有想到啦!!!