我正在处理包含图像和文本元素的缩略图组件的布局。图像具有height: 100px;
和自动宽度,而文本应该跨越图像宽度的宽度,并且应该隐藏溢出。
<div>
<img src="...">
<span>a bunch of text that should be cut off</span>
</div>
我尝试使用flexbox完成此操作,但我无法让文字宽度等于图片宽度。相反,文本只会增长100%并延伸图像。
答案 0 :(得分:1)
以下代码有效:
.main {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
width: 50%; // px would also work
}
.main img {
height: 100px;
flex: 0 0 100%;
}
.main div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
&#13;
<div class="main">
<img src="http://i.imgur.com/uiGur1h.jpg" alt="">
<div>Text go here </div>
</div>
<div class="main">
<img src="http://i.imgur.com/uiGur1h.jpg" alt="">
<div>Text would go here but would be cut off if it's very long </div>
</div>
&#13;
参考https://developer.mozilla.org/en/docs/Web/CSS/text-overflow
中的示例