我有一个固定宽度和高度的表格单元格,如果文本太大,单元格大小应该保持不变,文本应该被溢出隐藏:隐藏。
div {
display: table-cell
height: 100px;
width: 100px;
overflow: hidden;
vertical-align: middle;
}
但是,如果添加的文本过多,表格单元格会扩展到 100 像素以上。有什么技巧可以防止它膨胀吗?
文本长度应为几行,因此“white-space:nowrap”解决方案不适用
原文由 skyisred 发布,翻译遵循 CC BY-SA 4.0 许可协议
根据 CSS 2.1 规则,表格单元格的高度是“内容所需的最小高度”。 Thus, you need to restrict the height indirectly using inner markup, normally a
div
element (<td><div>content</div></td>
), and setheight
andoverflow
div
元素上的属性(当然,没有设置display: table-cell
元素,因为这会使其高度遵守 CSS 2.1 表格单元格规则)。