【发布时间】:2019-01-20 11:02:39
【问题描述】:
此表格的列宽为 59px 和 100px,即使 50px 和 100px 是指定的列宽。
table {
border-collapse: collapse;
border-spacing: 0;
table-layout: fixed;
}
col:first-child {
width: 50px;
}
col:nth-child(2) {
width: 100px;
}
td, th {
border: 1px solid black;
}
<table>
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
但如果我将width: 0 添加到表中,它就可以工作。
table {
border-collapse: collapse;
border-spacing: 0;
table-layout: fixed;
width: 0;
}
col:first-child {
width: 50px;
}
col:nth-child(2) {
width: 100px;
}
td, th {
border: 1px solid black;
}
<table>
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
为什么需要设置width: 0?有没有更好的办法?>
【问题讨论】:
-
用于
<th>word-break: break-word;断词 -
@Observer,但我不一定要断词。后一个例子不会断词。
标签: html css css-tables