单行文本水平&垂直居中
div {
line-height:200;
height:200;
text-align:center;
}
仅居中元素定宽高适用
- absolute + 负margin
- absolute + margin auto
- absolute + calc
居中元素不定宽高
- absolute + transform
- line-height
- writing-mode
- table
- css-table
- flex
- grid
<div class="wp">
<div class="box size">123123</div>
</div>
/* 公共代码 */
.wp {
border: 1px solid red;
width: 300px;
height: 300px;
}
.box {
background: green;
}
.box.size{
width: 100px;
height: 100px;
}
absolute + 负margin
.wp {
position: relative;
}
.box {
position: absolute;;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
absolute + margin auto
<div class="wp">
<div class="box size">123123</div>
</div>
.wp {
position: relative;
}
.box {
position: absolute;;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
absolute + calc
<div class="wp">
<div class="box size">123123</div>
</div>
.wp {
position: relative;
}
.box {
position: absolute;;
top: calc(50% - 50px);
left: calc(50% - 50px);
}
absolute + transform
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
position: relative;
}
.box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
line-height
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
line-height: 300px;
text-align: center;
font-size: 0px;
}
.box {
font-size: 16px;
display: inline-block;
vertical-align: middle;
line-height: initial;
text-align: left; /* 修正文字 */
}
writing-mode
<div class="div1">水平方向</div>
<div class="div2">垂直方向</div>
.div2 {
writing-mode: vertical-lr;
}
<div class="wp">
<div class="wp-inner">
<div class="box">123123</div>
</div>
</div>
.wp {
writing-mode: vertical-lr;
text-align: center;
}
.wp-inner {
writing-mode: horizontal-tb;
display: inline-block;
text-align: center;
width: 100%;
}
.box {
display: inline-block;
margin: auto;
text-align: left;
}
table
<table>
<tbody>
<tr>
<td class="wp">
<div class="box">123123</div>
</td>
</tr>
</tbody>
</table>
.wp {
text-align: center;
}
.box {
display: inline-block;
}
css-table
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
display: table-cell;
text-align: center;
vertical-align: middle;
}
.box {
display: inline-block;
}
flex
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
display: flex;
justify-content: center;
align-items: center;
}
grid
<div class="wp">
<div class="box">123123</div>
</div>
.wp {
display: grid;
}
.box {
align-self: center;
justify-self: center;
}
总结
PC端有兼容性要求,宽高固定,推荐absolute + 负margin
PC端有兼容要求,宽高不固定,推荐css-table
ie6+, chrome4+, firefox2+
ie6+, chrome4+, firefox2+
ie9+, chrome19+, firefox4+
ie9+, chrome4+, firefox3.5+
ie6+, chrome4+, firefox3.5+
ie6+, chrome4+, firefox2+
ie6+, chrome4+, firefox2+
ie8+, chrome4+, firefox2+
ie10+, chrome4+, firefox2+
ie10+, chrome57+, firefox52+