Is there any alternative to colgroup col width attribute now it has been deprecated?

Viewed 1131

The MDN page on colgroup indicates that col width is deprecated, however I have not found an alternative when colspan is involved, and you need to specify the width of a "colspanned" column.

Below is a rather minimalistic snippet to illustrate the issue, for a table with three columns and two rows. The middle column is never explicited with a "td" elements.

With a colgroup, it is possible to specify its width, and then everything is well. Without colgroup, the HTML rendering engine is unable to solve the equation, and column widths render incorrectly.

  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>

1 Answers

The official solution is using :nth-child in CSS.

.table_8 td:nth-child(1) {
    text-align: right;
    width: 32px;
}

But this is very unpractical! In my opinion, col width should be de-deprecated. It is supported by all browsers, as far as I can see. https://caniuse.com/?search=col%20width

Related