我正在使用flexbox在一个窄列中显示文本标签和数值,如果不适合,则使用省略号进行截断。
这个方法一直很好用,直到我需要把整个列放到table-cell中 - 这时浏览器(Chrome)就忽视了列宽,并使表格足够宽以适应所有文本。
以下是标签布局:
<div class="line">
<span>Very long label text</span>
<span>12345</span>
</div>
.line {
display: flex;
width: 100%;
}
.line span:first-child {
white-space: nowrap;
flex-grow: 1;
overflow: hidden;
text-overflow: ellipsis;
}
.line span:last-child {
flex-shrink: 0;
margin-left: 5px;
}
将其放置在具有固定宽度的常规 div 中可以按预期工作。将其放置在 table-cell 中则无法正常工作。
示例: http://jsfiddle.net/98o7m7am/
.wrapper {
width: 150px;
}
.table {
display: table;
}
.table > div {
display: table-cell;
}
.line {
display: flex;
width: 100%;
}
.line span:first-child {
white-space: nowrap;
flex-grow: 1;
overflow: hidden;
text-overflow: ellipsis;
}
.line span:last-child {
flex-shrink: 0;
margin-left: 5px;
}
<div class="wrapper">
<div class="line">
<span>Very long label text</span>
<span>12345</span>
</div>
</div>
<div class="table wrapper">
<div>
<div class="line">
<span>Very long label text</span>
<span>12345</span>
</div>
</div>
</div>