方法一,flex布局:
#box {
width: 600px;
height: 500px;
border: red solid 3px;
display: flex;
justify-content: center;
align-items: center;
}
#content{
width:300px;
height:200px;
background: red;
}
方法二,position布局:
#box {
width: 600px;
height: 500px;
border: red solid 3px;
position: relative;
}
#content{
width:300px;
height:200px;
background: red;
margin:auto auto;
position: absolute;
top:0;
left:0;
right:0;
bottom:0;
}
方法三,计算子元素margin值居中:
#box {
width: 600px;
height: 500px;
border: red solid 3px;
position: relative;
}
#content{
width:300px;
height:200px;
background: red;
position: absolute;
left:50%;
top:50%;
margin:-100px 0 0 -150px;
}
方法四,transform布局:
#box {
width: 600px;
height: 500px;
border: red solid 3px;
position: relative;
}
#content{
width:300px;
height:200px;
background: red;
position: absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
}
完。