- flex的居中。(IE10+)
.parent {
display: flex;
justify-content: center;
align-items: center;
}
或
.parent {
display: flex;
}
.child {
margin: auto;
}
- 先移动父级的50%,再利用 translate往回移动自身的50%。(IE9+)
.child {
margin-top: 50%;
margin-left: 50%;
transform: translate(-50%,-50%);
}
或
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
- 利用
position和margin。 (IE8+)
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
buttom:0;
left: 0;
roght: 0;
margin: auto;
}
- 通过
display:table-cell来把.parent模拟成一个表格单元格,利用表格的居中特性。(IE8+)
.parent {
display: table-cell;
vertical-align: middle;
text-align: center;
}
- 利用
:after伪选择器占位. (IE8+)
- IE8的伪选择器要使用
:after,更新版的浏览器可以使用:after和::after
.parent {
text-align: center;
}
.child {
display:inline-block;
vertical-align: middle;
}
.parent:after {
content: "";
vertical-align: middle;
height: 100%;
display: inline-block;
}