效果展示

<div class="father">
<div class="damao"></div>
</div>
绝对定位
.father {
position: relative;
width: 500px;
height: 400px;
background-color: pink;
}
.damao {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 150px;
height: 150px;
background-color: purple;
}
绝对定位+负边距
.father {
position: relative;
width: 500px;
height: 400px;
background-color: pink;
}
.damao {
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px;
margin-left: -75px;
width: 150px;
height: 150px;
background-color: purple;
}
绝对定位+transform
.father {
position: relative;
width: 500px;
height: 400px;
background-color: pink;
}
.damao {
position: absolute;
top: 50%;
left: 50%;
width: 150px;
height: 150px;
transform: translate(-50%, -50%);
background-color: purple;
}
Grid网格实现
.father {
display: grid;
place-items: center;
width: 500px;
height: 400px;
background-color: pink;
}
.damao {
width: 150px;
height: 150px;
background-color: purple;
}
flex弹性布局
.father {
display: flex;
justify-content: center;
align-items: center;
width: 500px;
height: 400px;
background-color: pink;
}
.damao {
width: 150px;
height: 150px;
background-color: purple;
}
表格布局
.father {
display: table-cell;
width: 500px;
height: 400px;
text-align: center;
vertical-align: middle;
background-color: pink;
}
.damao {
display: inline-block;
width: 150px;
height: 150px;
background-color: purple;
}