这是对于这个问题的跟进: 只使用CSS自动调整图像下面的文本
为什么在这段代码中,inline-block div元素会在元素右侧产生额外的宽度?
.item {
display: inline-block;
background-color: red;
}
.image-container {
background-color: blue;
display: table;
width: 1%;
}
img {
height: 120px;
}
.text-wrapper {
overflow: hidden;
}
<div class="item">
<div class='image-container'>
<img src='http://i.imgur.com/nV2qBpe.jpg' class="image">
<div class='text-wrapper'>
<p class='text'>Some text that may need to wrap into multiple lines</p>
</div>
</div>
</div>
<div class="item">
<div class='image-container'>
<img src='http://i.imgur.com/nV2qBpe.jpg' class="image">
<div class='text-wrapper'>
<p class='text'>Some text that may need to wrap into multiple lines</p>
</div>
</div>
</div>
<div class="item">
<div class='image-container'>
<img src='http://i.imgur.com/nV2qBpe.jpg' class="image">
<div class='text-wrapper'>
<p class='text'>Some text that may need to wrap into multiple lines</p>
</div>
</div>
</div>
<div class="item">
<div class='image-container'>
<img src='http://i.imgur.com/nV2qBpe.jpg' class="image">
<div class='text-wrapper'>
<p class='text'>Some text that may need to wrap into multiple lines</p>
</div>
</div>
</div>
编辑:这不是空格问题,请参见此jsfiddle(没有任何空格),请注意div仍然占用大量额外空间(红色区域):https://jsfiddle.net/2Lzo9vfc/332/
编辑2:为了澄清我的要求:我有N个具有不同宽度的图像,希望在“动态表格”中进行布局,即图像应该是内联的,因此当宽度超过父级水平空间时,它们将自动换行。 这变得棘手的地方是我有一些文本希望显示在每个图像下面,该文本也应随着图像宽度进行换行(正如我所说,图像宽度可能会不同)。
display:table
? - Martininline-block
div一个在300px范围内的宽度。 - starlocke