【发布时间】:2014-01-08 19:58:10
【问题描述】:
我正在尝试将图像完全填充到设备的全屏。所以我给了.container:100% 和 body,html 到 100% 的高度。现在我想将child div 垂直居中对齐,无论它有什么内容,但它应该垂直居中。所以我尝试在margin-top 和bottom 上都给出50% height 和25%。所以正好 100% 它应该符合计算。但这并没有像预期的那样发生。
为什么它没有垂直居中?换句话说,为什么 div 的顶部和底部没有相等的空间,即使我用height:50%; 给出了相等的顶部和底部边距
如何垂直居中对齐?不应该有垂直滚动条,因为我将 body,html.container 的高度设置为 100%,但它正在出现。
CSS
.child{
width: 100%;
height: 50%;
margin-top: 25%;
margin-bottom: 25%;
background-color: red;
color:white;
}
html,body,.container{
width:100%;
height:100%;
background:url('http://cdn.morguefile.com/imageData/public/files/e/earl53/
preview/fldr_2009_05_25/file5821243281047.jpg');
background-size:cover;
}
HTML
<div class="container">
<div class="child">
Hello i(red color div) should be cenetered in vertical direction
</div>
</div>
【问题讨论】:
-
我已经更新了 om Fiddle check jsfiddle.net/5LMY7/8