前几天面试一家公司,被问到垂直居中的方法,我只答出了margin、table-cell、flex三种。回来以后以为特别惭愧,因而整理了一下居中的方案作个记录,但愿对你们也有帮助。 若是哪里写的不对,欢迎指正,很是感谢。css
改于2021年2月4日,根据目前经常使用的方案进行了调整。html
<div class="parent">
<div class="child">child</div>
</div>
复制代码
<div class="parent">
<span class="child">child</span>
</div>
复制代码
.parent {
text-align: center;
}
复制代码
(低版本浏览器还须要设置
text-align: center;
)web
.parent {
text-align: center;
}
.child {
width: 100px;
margin: auto;
border: 1px solid blue;
}
复制代码
因为本文主要想记录的是垂直居中的方案,这里水平垂直的其余方案就不作过多记录了。面试
很经常使用,主要用于文字的排版,也能够用于图片元素居中浏览器
.parent {
height: 200px;
line-height: 200px;
border: 1px solid red;
}
复制代码
优势:更灵活,也更简洁,可维护性也更强。只要不考虑IE,这个方案几乎是最优选择吧。
缺点:若是还在使用IE浏览器的话,flex布局就没那么香了。markdown
.parent {
width: 600px;
height: 200px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center; /*水平居中*/
}
.child {
background: blue;
}
复制代码
优势:不须要提早知道尺寸,兼容性好
缺点:这个方法曾经是我最喜欢用的一个,可是目前已经不建议使用绝对定位absolut
了,若是还在用IE开发,这个方法仍是比较推荐的。
此方法出自张鑫旭老师的博客 小tip: margin:auto实现绝对定位元素的水平垂直居中wordpress
.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
background: blue;
}
复制代码
优势:也是一个比较好的实现方式,较简洁oop
.parent {
width: 600px;
height: 200px;
border: 1px solid red;
display: table;
}
.child {
display: table-cell;
vertical-align: middle;
}
复制代码
或:布局
这个方案是在知乎看到的,原文说是淘宝团队的方案:flex
用 CSS 实现元素垂直居中,有哪些好的方案? - Gino的回答 - 知乎
张鑫旭老师的博客也有提到过:
我所知道的几种display:table-cell的应用
.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;
}
复制代码
缺点:
- 须要提早知道
child
的尺寸,margin-top: -(高度的一半);
margin-left: -(宽度的一半);
- 如今已经不建议使用绝对定位
absolute
,特别是在移动设备上。优势: 兼容性不错,也算是一点小技巧吧。
.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 {
padding: 5% 0;
}
.child {
padding: 10% 0;
background: blue;
}
复制代码
这个方案是先从这位博主的文章中看到:
CSS:使用伪元素作水平垂直居中的微深刻研究
而后发现张鑫旭老师的文章中也有提到:
:after伪类+content内容生成经典应用举例
.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;
}
复制代码
也是个不错的方法。
缺点:须要知道child的尺寸,须要计算,一样的,若是还在使用IE的小伙伴,不推荐。
.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;
}
复制代码
vertical-align: middle;
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等标签,冗余有点多,并且结构也改变了。