【发布时间】:2021-12-17 00:22:54
【问题描述】:
我正在尝试实现以下布局:具有多个固定宽度列的网格,其中一个具有未知宽度的内容,它将由多个 span 元素组成,不会换行,并且必须被截断省略号。
一个更具体的例子:
这是我尝试过的,但没有成功,使用 flexbox:
.grid {
width: 300px;
border: 1px solid black;
display: grid;
grid-template-columns: [main] minmax(0, 1fr) [foo] 20px [bar] 20px
}
main {
grid-column: main;
white-space: nowrap;
background: yellow;
}
.color-marker {
height: 10px;
width: 10px;
background: red;
border-radius: 50%;
}
.test {
display: flex;
align-items: baseline;
gap: 0.6rem;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
}
.test > * {
flex: 0 0 auto;
}
.foo {
grid-column: foo;
height: 20px;
width: 20px;
background-color: orange;
}
.bar {
grid-column: bar;
height: 20px;
width: 20px;
background-color: purple;
}
<div class="grid">
<main>
<div class="test">
<div class="color-marker"></div>
<span>
Lorem ipsum
</span>
<span>
sed do eiusmod tempor incididunt ut labore
</span>
</div>
</main>
<div class="foo">
</div>
<div class="bar">
</div>
</div>
我还尝试使用内联网格而不是 flexbox,但这也没有帮助 (example on jsbin)。
有没有办法让它工作?
免责声明:我意识到这个问题的变体已经在 Stack Overflow 上提出过;但没有一个建议的解决方案对我有用。以下是我检查过的线程:
- Flexbox in flexbox not working with ellipsis text overflow
- text-overflow ellipsis not working in nested flexbox
- text-overflow ellipsis on flex child not working
我希望这不会使我的问题重复。
【问题讨论】: