【发布时间】:2016-05-02 21:17:59
【问题描述】:
问题:
是否可以相对于浮动 div 的动态高度将浮动 div 中的子元素垂直居中?
背景:
我正在使用 WordPress 主题,其中包含用于创建列的短代码。这些列是通过浮动 div 创建的。因此,我想坚持使用浮动,而不是使用类似问题 (Vertically center content of floating div) 建议的表格布局。
我希望结果具有响应性。图像高度随屏幕宽度而变化,因此我无法在 p 元素上设置显式高度或边距。 p 中的文本也不止一行,所以我无法将 line-height 设置为解决方案 (Vertically centre a div)。
示例:
http://codepen.io/besiix/pen/rxdOWM
.wrapper {
background-color: #50C5B7;
padding: 5px;
}
.image {
width: 100%;
}
.clear {
clear: both;
}
.one-half {
float: left;
width:48%;
margin-right: 4%;
background-color: lightgrey;
position: relative;
display: inline;
box-sizing: border-box;
}
.last {
clear: right;
margin-right: 0;
}
<section class="wrapper">
<div class="one-half">
<p class="v-center"> This wants to be centered vertically in relation to the image on the right.</p>
</div>
<div class="one-half last">
<img class="image" src="http://www.premiercottages.co.uk/images/regions/Scotland.jpg">
</div>
<div class="clear"></div>
</section>
【问题讨论】: