英文:
Html table cell to wrap text without extending height
问题
以下是您要翻译的内容:
"I have the following table for which I'd like to set cell heights and widths determined by provided dimensions. It works fine as long as the cell content does not wrap on its own. In the image, the first row is meant to be 20px high, but the orange content extends the row height beyond the 20px. It would work fine, if the 'white-space' was set to 'nowrap' (as for the top/left cell).
However, I would like the text to wrap, but it should be cut off where it would go beyond the designated row height (similar to how the text it cut off to the right when it would go beyond the width).
Is that possible and how would I be able to achieve that?"
Update:
"I've got somewhat of a solution, but it requires the height to be set in the cell itself which I'd like to avoid (ideally, the height should be set by <tr>
). I applied display: inline-block; height: 20px
to the span of the orange cell:
<span style='white-space: normal; display: inline-block: height: 20px'>
I want this text to wrap, yet not to expand the height of the cell/row
</span>
英文:
I have the following table for which I'd like to set cell heights and widths determined by provided dimensions. It works fine as long as the cell content does not wrap on its own. In the image, the first row is meant to be 20px high, but the orange content extends the row height beyond the 20px. It would work fine, if the white-space
was set to nowrap
(as for the top/left cell).
However, I would like the text to wrap, but it should be cut off where it would go beyond the designated row height (similar to how the text it cut off to the right when it would go beyond the width).
Is that possible and how would I be able to achieve that?
<html>
<body>
<table cellspacing='0' cellpadding='0' style='display: table; table-layout: fixed; width: 200px'>
<colgroup width='50'></colgroup>
<colgroup width='50'></colgroup>
<colgroup width='20'></colgroup>
<tr style='height: 20px; overflow: hidden; white-space: nowrap;'>
<td style='background-color: olive; display: table-cell; white-space: nowrap; text-overflow: ellipsis; overflow: hidden;'>
Very long text
</td>
<td style='background-color: orange; display: table-cell; overflow: hidden;'>
<span style='white-space: normal;'>
I want this text to wrap, yet not to expand the height of the cell/row
</span>
</td>
<td style='background-color: steelblue; display: table-cell'>
Fooo barrrr
</td>
</tr>
<tr>
<td style='background-color: aliceblue;'>
<span>
Blablub
</span>
</td>
<td style='background-color: aqua;'>
<span>
Long text
</span>
</td>
<td style='background-color: aquamarine; display: table-cell; overflow: hidden; white-space: nowrap;'>
Another text
</td>
</tr>
</table>
</body>
</html>
Update
I've got somewhat of a solution, but it requires the height to be set in the cell itself which I'd like to avoid (ideally, the height should be set by <tr>
). I applied display: inline-block; height: 20px
to the span of the orange cell:
<span style='white-space: normal; display: inline-block: height: 20px'>
I want this text to wrap, yet not to expand the height of the cell/row
</span>
答案1
得分: 1
以下是您要翻译的内容:
默认情况下,td
的默认值会覆盖 tr
上的声明。溢出声明不会被继承,而高度声明等同于最小高度。
请使用以下声明来设置 td
:display: block; height: inherit; white-space: normal;
此示例演示了如何使用 CSS 处理不推荐使用的 HTML 属性 cellspacing
、cellpadding
和 width
。您为 colgroup
使用的宽度总计为 120px,但表格宽度为 200px,因此浏览器需要计算如何扩展 colgroup
以适应。在此示例中,我替换了计算出的值。
table {
table-layout: fixed;
border-spacing: 0;
width: 200px;
}
td {
padding: 0;
}
colgroup {
width: 83.33px;
}
colgroup:nth-of-type(3) {
width: 33.34px;
}
<table>
<colgroup></colgroup>
<colgroup></colgroup>
<colgroup></colgroup>
<tr style='height: 20px; white-space: nowrap;'>
<td style='background-color: olive; text-overflow: ellipsis; overflow: hidden;'>
非常长的文本
</td>
<td style='background-color: orange; overflow: hidden; display: block; height: inherit; white-space: normal;'>
我希望这个文本换行,但不要增加单元格/行的高度
</td>
<td style='background-color: steelblue;'>
Fooo barrrr
</td>
</tr>
<tr>
<td style='background-color: aliceblue;'>
<span>
Blablub
</span>
</td>
<td style='background-color: aqua;'>
<span>
长文本
</span>
</td>
<td style='background-color: aquamarine; overflow: hidden; white-space: nowrap;'>
另一段文本
</td>
</tr>
</table>
英文:
The default values for td
overrule the declarations on the tr
. The overflow declaration is not inherited and the height declaration is equivalent to min-height.
Use these declarations for the td
:<br>display: block; height: inherit; white-space: normal;
<hr>
This example shows how to use CSS for the deprecated HTML attributes cellspacing
, cellpadding
, and width
. The widths that you used for the colgroup
s add up to 120px but the table is 200px, so the browser has to calculate how to expand the colgroups to fit. I've substituted the calculated values in this example.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
table {
table-layout: fixed;
border-spacing: 0;
width: 200px;
}
td {
padding: 0;
}
colgroup {
width: 83.33px;
}
colgroup:nth-of-type(3) {
width: 33.34px;
}
<!-- language: lang-html -->
<table>
<colgroup></colgroup>
<colgroup></colgroup>
<colgroup></colgroup>
<tr style='height: 20px; white-space: nowrap;'>
<td style='background-color: olive; text-overflow: ellipsis; overflow: hidden;'>
Very long text
</td>
<td style='background-color: orange; overflow: hidden; display: block; height: inherit; white-space: normal;'>
I want this text to wrap, yet not to expand the height of the cell/row
</td>
<td style='background-color: steelblue;'>
Fooo barrrr
</td>
</tr>
<tr>
<td style='background-color: aliceblue;'>
<span>
Blablub
</span>
</td>
<td style='background-color: aqua;'>
<span>
Long text
</span>
</td>
<td style='background-color: aquamarine; overflow: hidden; white-space: nowrap;'>
Another text
</td>
</tr>
</table>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论