【发布时间】:2021-08-01 20:32:09
【问题描述】:
MDN page on colgroup 表示不推荐使用 col 宽度,但是当涉及 colspan 时我没有找到替代方法,您需要指定“colspanned”列的宽度。
下面是一个相当简约的 sn-p 来说明这个问题,对于一个三列两行的表。中间列永远不会用“td”元素显式。
使用 colgroup,可以指定其宽度,然后一切正常。没有 colgroup,HTML 渲染引擎无法求解方程,列宽渲染不正确。
table {
border-collapse: collapse;
}
.w100 { width: 100px; background-color: red }
.w200 { width: 200px; background-color: green }
<!DOCTYPE html>
<html>
<body>
With colgroups
<table>
<colgroup>
<col style="width:100px">
<col style="width:100px">
<col style="width:100px">
</colgroup>
<tbody>
<tr>
<td class="w100">A
<td class="w200" colspan="2">B
<tr>
<td class="w200" colspan="2">C
<td class="w100">D
</tbody>
</table>
<p></p>
No colgroups
<table>
<tbody>
<tr>
<td class="w100">A
<td class="w200" colspan="2">B
<tr>
<td class="w200" colspan="2">C
<td class="w100">D
</tbody>
</table>
</body>
</html>
【问题讨论】:
标签: css html-table col