頁面結構都是:flex
<div class="parent" style="background-color:red;width: 400px;height: 400px;"> <div class="child" style="background-color:blue;width: 200px;height: 200px;"></div> </div>
1、水平居中spa
1.設置父元素和子元素,inline-block+text-align:ie6↑code
.parent {
text-align: center;
}
.child {
display: inline-block;
}
2.設置子元素,table+margin: ie8↑orm
.child {
display:table;
margin:0 auto;
}
3.設置父元素和子元素,absolute+transform:ie10↑blog
.parent {
position:relative;
}
.child {
position:absolute;
left:50%;
transform:translateX(-50%);
}
4.設置父元素和子元素,flex+margin:ie9↑it
.parent {
display:flex;
}
.child {
margin:0 auto;
}
5.設置父元素,flex+justify-content:ie9↑io
.parent {
display:flex;
justify-content:center;
}
2、垂直居中table
1.設置父元素,table-cell+vertical-align:ie8↑form
.parent {
display:table-cell;
vertical-align:middle;
}
2.設置父元素和子元素,absolute+transform:ie10↑class
.parent {
position:relative;
}
.child {
position:absolute;
top:50%;
transform:translateY(-50%);
}
3.設置父元素,flex+align-items(可用於浮動元素):ie9↑
.parent {
display:flex;
align-items:center;
}
3、水平+垂直
1.設置父元素和子元素,absolute+transform:ie10↑
.parent {
position:relative;
}
.child {
position:absolute;
left:50%;
top:50%;
transform:tranplate(-50%,-50%);
}
2.設置父元素和子元素,inline-block+text-align+table-cell+vertical-align:ie8↑
.parent {
text-align:center;
display:table-cell;
vertical-align:middle;
}
.child {
display:inline-block;
}
3.設置父元素,flex+justify-content+align-items:ie9↑
.parent {
display: flex;
justify-content: center;
align-items: center;
}