1.绝对定位+ transform: translate(-50%, -50%);
.parent {
height: 500px;
position: relative;
}
.child {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
2.绝对定位+margin:auto
.parent {
height: 500px;
position: relative;
}
.child{
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
3.利用弹性布局 display:flex
.parent{
height:500px;
width:600px:
display: flex;
align-items: center;
justify-content: center;
}
.child{
height:300px;
width:300px:
}
4.使用表格布局
.parent{
height: 500px;
width: 400px;
background-color: green;
display: table-cell;
vertical-align: middle;
}
.child{
height: 200px;
width: 200px;
background-color: red;
margin: auto;
}