gpt4 book ai didi

css - 如何将列宽设置为整个表格宽度(固定)减去另一列(针对文本自动调整)?

转载 作者:行者123 更新时间:2023-11-27 23:38:08 28 4
gpt4 key购买 nike

我正在设置一个包含两列的表格的 css 属性。表格宽度是其父项(固定宽度的父项)的 100%。第一列由内容宽度自动调整(无换行)。第二列宽度应为表格宽度减去第一列宽度,溢出文本应被截断。

目前,我只需要它在 Chrome 上工作。

.table {
width: 100%;
}

.td-value {
white-space: nowrap;
overflow-x: hidden;
text-overflow: ellipsis;
}
<table class="table">
<tr class="tr">
<td class="td-key ">Key</td>
<td class="td-value ">A long value</td>
</tr>
</table>

最佳答案

您可以给值列 width: 100% 并将内容包装在 flex 容器中,这会限制子项的大小。

.table {
width: 100%;
border-collapse: collapse;
}

td {
padding: 0.5em;
border: 1px solid #888;
}

.td-key {
width: auto;
}

.td-value {
width: 100%;
}

.flex-wrapper {
display: flex;
}

.cell-content {
width: 0;
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<table class="table">
<tr class="tr">
<td class="td-key">Key</td>
<td class="td-value">
<div class='flex-wrapper'>
<div class='cell-content'>
A very very very very
very very very very very
very very very very very
very very very very very
very very very very very
very very very very very
long value
</div>
</div>
</td>
</tr>
</table>

关于css - 如何将列宽设置为整个表格宽度(固定)减去另一列(针对文本自动调整)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152103/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com