【发布时间】:2014-03-04 16:45:30
【问题描述】:
我正在尝试使用 line-height 将 div 垂直居中,而不为 line-height 指定设置的像素值。我需要将 line-height 扩展到它的 div 的大小。使用 '100vh' 有效,但视口单元没有得到足够广泛的广泛支持。将 line-height 设置为 100% 似乎不起作用。这是我的 HTML:
<div class="background">
<div class="lightboxbg">
<div class="wrapper">
<div class="centerme"></div>
</div>
</div>
</div>
还有我的 CSS:
html, body {
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
}
.background {
width: 90%;
height: 100%;
background-color: AntiqueWhite;
}
.lightboxbg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: black;
vertical-align: middle;
text-align: center;
}
.wrapper {
vertical-align: middle;
line-height: 100%;
height: 100%;
width: 100%
}
.centerme {
vertical-align: middle;
display: inline-block;
width: 100px;
height: 100px;
background-color: blue;
}
And here's a jsfiddle. 如果我可以将包装器的行高扩展到包装器的高度,蓝色框将居中,但我不知道该怎么做。感谢阅读。
编辑:查看 Nathan Lee 的关于表格单元的解决方案的答案,Fredric Fohlin 的一个非常狂野的“绝对定位”答案,以及 MM Tac 的使用绝对定位的解决方案。
【问题讨论】:
标签: html vertical-alignment centering css