MDN关于colgroup的页面表明col宽度已被弃用,但在涉及到colspan时,我并没有找到替代方法。当你需要指定“colspanned”列的宽度时,必须要用到它。
下面是一个相当简洁的代码片段,用来说明此问题,包含了两行三列的表格。中间的列从未用“td”元素显式表示过。
使用colgroup,可以指定其宽度,然后一切都很好。如果没有colgroup,HTML渲染引擎将无法解决方程式,列宽度将呈现不正确。
下面是一个相当简洁的代码片段,用来说明此问题,包含了两行三列的表格。中间的列从未用“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>