【发布时间】:2017-10-04 05:03:49
【问题描述】:
我正在开发一个显示大型卡片网格的网络应用程序,其高度本质上是可变的。
为了美观,我们使用 jQuery 的 .matchHeight() 来平衡每行中卡片的高度。
它的性能不能很好地扩展,所以今天我一直在迁移到一个基于弹性盒的解决方案,它的速度要快得多。
但是,我丢失了一个行为 - 如果卡片标题的内容不合适,应该用省略号截断。
目标:
- 3 列
- 列宽因父项而异
- 列间距不变
- 行内高度均等
如何安排尊重容器大小和尊重text-overflow: ellipsis; 和white-space: nowrap;?
(没有 jQuery 标签,因为我们正在远离它)
我的解决方案是目前的形式,除了截断之外,它实现了我的所有目标:
https://codepen.io/anon/pen/QvqZYY
#container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start; /* Bias cards to stack from left edge */
align-items: stretch; /* Within a row, all cards the same height */
border: thin solid gray;
}
.card-wrapper {
width: 33.33%;
display: flex;
background: #e0e0ff;
}
.card {
flex-grow: 1;
margin: 7px;
display: flex;
flex-direction: column;
border: thin solid gray;
background: #e0ffff;
}
.card div {
border: thin solid gray;
}
.card div:nth-child(1) {
white-space: nowrap;
text-overflow: ellipsis;
}
.card div:nth-child(2) {
flex-grow: 2;
}
<div id="container">
<div class="card-wrapper">
<div class="card">
<div>Title</div>
<div>Multiline<br/>Body</div>
<div>Footer</div>
</div>
</div>
<div class="card-wrapper"><div class="card"><div>Really long rambling title that pushes beyond the bounds of the container, unless your screen is really, really wide</div><div>Body</div><div>Footer</div></div></div>
<div class="card-wrapper"><div class="card"><div>Title</div><div>Body</div><div>Footer</div></div></div>
<div class="card-wrapper"><div class="card"><div>Title</div><div>Body</div><div>Footer</div></div></div>
<div class="card-wrapper"><div class="card"><div>Title</div><div>Body</div><div>Footer</div></div></div>
</div>
【问题讨论】: