方式一:
.parent {
width:500px;
height:500px;
border:2px solid red;
position:relative;
}
.child {
width:200px;
height:200px;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background-color: red;
}
方式二:
.parent {
width:500px;
height:500px;
border:2px solid red;
position:relative;
}
.child {
width:200px;
height:200px;
margin:auto;
position:absolute;
left:50%;
top:50%;
margin-left: -100px;/*使用负值margin将子块偏移自身宽度值的50%*/
margin-top:-100px;
background-color: red;
}
方式三:
.parent {
width:500px;
height:500px;
border:2px solid red;
display:table-cell;
vertical-align:middle;
text-align: center;
}
.child {
width:200px;
height:200px;
display:inline-block;
background-color: red;
}
方式四:
.parent {
width:500px;
height:500px;
border:2px solid red;
display:flex;
justify-content:center;
align-items:center;
}
.child {
width:200px;
height:200px;
background-color: red;
}