<div class='box' >
<div class='center' ></div>
</div>
// 样式
.box {
width: 700px;
height: 700px;
border: 1px solid red;
}
.center {
width: 100px;
height: 100px;
border: 1px solid green;
}
// 第一种
.box {
display: grid;
}
.center {
align-self: center;
justify-self: center;
}
// 第二种
.box {
display: grid;
place-items: center;
}
.center {}
// 第三种
.box {
display: table-cell;
vertical-align: middle;
}
.center {
margin: auto;
}
// 第四种
.box {
display: flex;
justify-content: center;
align-items: center;
}
.center {}
// 第五种
.box {
position: relative;
}
.center {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
// 第六种
.box {
position: relative;
}
.center {
position: absolute;
left: calc(50% - 50px);
top: calc(50% - 50px);
}
// 第七种
.box {
position: relative;
}
.center {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
// 第八种
.box {
position: relative;
}
.center {
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}