In my table I set the width of the first cell in a column to be 100px
.
However, when the text in one of the cell in this column is too long, the width of the column becomes more than 100px
. How could I disable this expansion?
In my table I set the width of the first cell in a column to be 100px
.
However, when the text in one of the cell in this column is too long, the width of the column becomes more than 100px
. How could I disable this expansion?
我玩了一会儿,因为我搞不清楚.
您需要设置单元格宽度(th
或td
工作,我两者都设置),并将table-layout
设置为fixed
.出于某种原因,单元格宽度似乎只有在设置了表格宽度的情况下才会保持不变(我认为这很愚蠢,但没什么).
此外,将overflow
属性设置为hidden
也很有用,以防止表中出现任何额外的文本.
您还应该确保将所有的边框和大小都留给CSS.
好的,下面是我得到的:
table {
border: 1px solid black;
table-layout: fixed;
width: 200px;
}
th,
td {
border: 1px solid black;
width: 100px;
overflow: hidden;
}
<table>
<tr>
<th>header 1</th>
<th>header 234567895678657</th>
</tr>
<tr>
<td>data asdfasdfasdfasdfasdf</td>
<td>data 2</td>
</tr>
</table>
这家伙也有类似的问题:Table cell widths - fixing width, wrapping/truncating long words