前几天面试一家公司,被问到垂直居中的方法,我只答出了 margin、table-cell、flex 三种。回来之后觉得特别惭愧,于是整理了一下居中的方案做个记录,希望对大家也有帮助。如果哪里写的不对,欢迎指正,非常感谢。
块级元素居中 html 代码部分
<div class="parent">
<div class="child">child</div>
</div>
行内元素居中 html 代码部分
<div class="parent">
<span class="child">child</span>
</div>
.parent {
text-align: center;
}
低版本浏览器还需要设置 text-align: center;
.parent {
text-align: center;
}
.child {
margin: auto;
border: 1px solid blue;
}
由于本文主要想记录的是垂直居中的方案,这里水平垂直的其他方案就不做过多记录了。
.parent {
height: 200px;
line-height: 200px;
border: 1px solid red;
}
.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
background: blue;
position: absolute;
left: 50%;
top: 50%;
margin-top: -20px;
margin-left: -40px;
}
.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: blue;
}
.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
background: blue;
}
缺点:如果高度固定,需要提前计算尺寸(只在某些特定情况适用)。
.parent {
padding: 5% 0;
}
.child {
padding: 10% 0;
background: blue;
}
.parent {
width: 600px;
height: 200px;
border: 1px solid red;
display: table;
}
.child {
display: table-cell;
vertical-align: middle;
}
或
.parent {
height: 300px;
border: 1px solid red;
display: table-cell;
vertical-align: middle;
/* *display: block;
*font-size: (heightX0.873);
*font-family: arial; */
}
同样适用于多行文字的垂直居中处理
HTML代码:
<div class="parent">
<span class="child">child child child child child child child child child child child child child child child child child child child childchild child child </span>
</div>
CSS代码:
.parent {
width: 400px;
height: 300px;
display: table-cell;
vertical-align: middle;
border: 1px solid red;
}
.child {
display: inline-block;
vertical-align: middle;
background: blue;
}
缺点:兼容性不好
.parent {
width: 600px;
height: 200px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center; /*水平居中*/
}
.child {
background: blue;
}
.parent {
width: 300px;
height: 300px;
border: 1px solid red;
text-align: center;
}
.child {
background: blue;
width: 100px;
height: 40px;
display: inline-block;
vertical-align: middle;
}
.parent::before {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}
并不推荐,但也是个方法。缺点:兼容性太差,需要计算。
.parent {
width: 300px;
height: 300px;
border: 1px solid red;
position: relative;
}
.child {
width: 100px;
height: 100px;
background: blue;
padding: -webkit-calc((100% - 100px) / 2);
padding: -moz-calc((100% - 100px) / 2);
padding: -ms-calc((100% - 100px) / 2);
padding: calc((100% - 100px) / 2);
background-clip: content-box;
}
HTML 代码:
<div class="parent">
<div class="child">child</div>
<div class="brother">brother</div>
</div>
CSS 代码:
.parent {
width: 400px;
height: 400px;
border: 1px solid red;
position: relative;
}
.child, .brother {
display: inline-block;
vertical-align: middle;
}
.child {
background: blue;
font-size: 12px;
}
.brother {
height: 400px;
font-size: 0;
}
当然,还有一种方法,就是使用 table 布局:
<table>
<tr>
<td align="center" valign="middle">content</td>
</tr>
</table>
因为 html 还要加 table 等标签,冗余有点多,而且结构也改变了。

