我需要在具有下一个属性的HTML容器中垂直居中内容:
height: auto;
min-height: 50%;
我尝试了不同的对齐技术而没有成功。例如:
您能否建议如何在此类容器中垂直居中内容?我对纯HTML / CSS解决方案感兴趣。
答案 0 :(得分:1)
首先我要使用表结构。像下面一样更新你的CSS。
.main-container {
border: 1px solid;
height: auto;
min-height: 50%; /* important */
display:table;
width:100%;
}
.child-container {
display: inline-table;
height: 100%;
width: 100%;
}
.content {
display: table-cell;
text-align: center;
vertical-align: middle;
}
答案 1 :(得分:1)
我已更新 css :
html, body {
height: 100%;
}
.main-container {
display:table;
border: 1px solid;
height: auto;
min-height: 50%; /* important */
width:100%;
}
.child-container {
display: table-row;
height: 100%;
width: 100%;
}
.content {
display: table-cell;
text-align: center;
vertical-align: middle;
}
请参阅更新后的JSFIDDLE
答案 2 :(得分:0)
小提琴链接:http://jsfiddle.net/theharsh/k7uwnnfb/
如果IE8或更低版本不是问题,你应该尝试使用CSS3技术。 元素可以垂直居中设置,无需考虑高度。
HTML:
<div class="box">
<div class="vcenter">
Yo! This is Centered !
</div>
</div>
CSS:
.box{
height:200px;
background:tomato;
color:#fff;
width:200px;
}
.vcenter{
position: relative;
top:50%;
background:purple;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
}