【发布时间】:2017-02-20 13:42:21
【问题描述】:
我想在tbody 中的td 上设置宽度,它位于thead 下方,colspan="2" 具有硬定义的列宽(%)。浏览器外壳不会动态调整表格宽度。
.sample {
width: 100%;
table-layout: fixed;
}
.sample td:nth-child(1),
.sample th:nth-child(1) {
width: 30%;
}
.sample td:nth-child(2),
.sample th:nth-child(2) {
width: 70%;
}
<table class="sample" border="1">
<thead>
<tr>
<th colspan="2">Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>30%</td>
<td>70%</td>
</tr>
</tbody>
</table>
如果thead 被删除,上述样式有效,但不适用于它。
重要提示,
- 我添加了
table-layout: fixed;,因为我需要列与指定的宽度完全相同。使用table-layoud: auto;会导致浏览器将指定的宽度作为方向,但仍会动态调整宽度。
编辑
接受的答案解决了我上面的问题。但是我无法使用colgroup,因为我使用的是所见即所得的编辑器。因此,我添加了一个带有详细规格的后续问题here。
【问题讨论】:
标签: html css html-table