colgroup col width 属性现在已被弃用,是否还有其他替代方案?

Eri*_*nge 4 css html-table col

colgroup 上的 MDN 页面表明col 宽度已被弃用,但是当涉及 colspan 时我还没有找到替代方案,并且您需要指定“colspanned”列的宽度。

下面是一个相当简单的片段来说明这个问题,适用于一个三列两行的表。中间列永远不会用“td”元素显式显示。

使用 colgroup,可以指定其宽度,然后一切都很好。如果没有 colgroup,HTML 渲染引擎将无法求解方程,并且列宽渲染不正确。

  table {
    border-collapse: collapse;
  }
  .w100 { width: 100px; background-color: red }
  .w200 { width: 200px; background-color: green }
Run Code Online (Sandbox Code Playgroud)
<!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>
Run Code Online (Sandbox Code Playgroud)

csk*_*kwg 5

官方的解决方案是在 CSS 中使用 :nth-child 。

.table_8 td:nth-child(1) {
    text-align: right;
    width: 32px;
}
Run Code Online (Sandbox Code Playgroud)

但这是非常不切实际的!在我看来,应该废弃 col width。据我所知,所有浏览器都支持它。 https://caniuse.com/?search=col%20width