【发布时间】:2022-01-05 01:23:01
【问题描述】:
我有一系列图像,它们有不同的标题。 这些标题中的每一个都需要出现在图像底部的中间位置。 这是它的外观以及我如何在 div 中构建它的模型。 因为标题的底部和下面的任何 div 之间需要有空间,所以标题不能绝对定位。
我知道这可以在 javascript 中完成,但这将是一个 hack,所以我更愿意找到一个仅 CSS 的解决方案。
...这是目前的代码:JS FIDDLE
.outer {
position: relative;
width: 320px;
margin-bottom:24px // this needs to be the space between the bottom of the caption and the next image or whatever div is beneath.
}
.image {
background-color: blue;
width: 320px;
height: 380px;
position: relative;
border-radius: 5px;
}
.caption {
position: relative;
margin-top: -24px;
/* THIS NEEDS TO BE 50% of the Captions variable height */
}
.card {
margin-right: 25%;
margin-left: 5%;
position: relative;
background-color: #FFFFFF;
padding: 24px;
line-height: 24px;
}
<div class="outer">
<div class="image">
</div>
<div class="caption">
<div class="card">
Lorem ipsum dolar sit amet consectetur idipiscine ipsumd lorem ipsum dolar sit amet. a
</div>
</div>
</div>
【问题讨论】:
-
FWIW,您无需将
display: block;应用于<div>。它们是块级元素。