我有以下要求:
div
,里面有图片+文字。div
应保持其宽高比,divs
内的所有图片应具有相同的高度(为此,我使用:Maintain the aspect ratio of a div with CSS)。div
内的图片,以便完全填充父div
。我有两个主要问题:
div
更高,因为它内部有文字。这与padding-bottom: 150%
- 方法相冲突,该方法用于在divs
大小更改时保留宽高比。但是,目标是所有divs
都具有相同的大小,无论它们内部有多少文本。答案 0 :(得分:1)
将文字添加为position:absolute
.inner-text{
position:absolute;
width:100%;
height:100%;
text-align:center;
z-index:1;
}
.event:hover .inner-text{
background:rgba(255,255,255,0.5)
}
<div class="bit-3">
<div class="event" style="background-image: url(https://container25.at/wpdev/wp-content/uploads/2016/06/test.jpg)">
<div class="inner-text">
Text inner
</div>
</div>
</div>
更新小提琴代码:https://jsfiddle.net/f5uxz4mx/8/
答案 1 :(得分:0)
尝试添加background-size:contains; height:0;
.bit-3 {
width: 33.33333%;
float: left;
max-resolution: res;-left: 20px;
}
.event {
margin: 0 auto;
position: relative;
border: 2px solid #000000;
padding-bottom: 150%;
background-size: cover;
}
<div class="bit-3">
<div class="event" style="background-image: url(https://container25.at/wpdev/wp-content/uploads/2016/06/test.jpg);background-size: contain;height: 0;">asdf text
</div>
</div>
<div class="bit-3">
<div class="event" style="background-image: url(https://container25.at/wpdev/wp-content/uploads/2016/06/20170715_mareados.jpg)">
</div>
</div>