【发布时间】:2011-06-26 07:57:36
【问题描述】:
我一直在绞尽脑汁想使用以下方法在 css 中创建垂直对齐
.base{
background-color:green;
width:200px;
height:200px;
overflow:auto;
position:relative;
}
.vert-align{
padding-top:50%;
height:50%;
}
<!-- and used the following div structure. -->
<div class="base">
<div class="vert-align">
Content Here
</div>
</div>
虽然这似乎适用于这种情况,但令我惊讶的是,当我增加或减少基本 div 的宽度时,垂直对齐会突然对齐。我期待当我设置 padding-top 属性时,它会将填充作为父容器高度的百分比,在我们的例子中是基础,但是上面的 50% 的值被计算为百分比宽度。 :(
有没有办法将填充和/或边距设置为高度的百分比,而无需使用 JavaScript?
【问题讨论】:
标签: html css vertical-alignment