This table has column widths 59px and 100px, even though 50px and 100px are the specified column widths.
table {
border-collapse: collapse;
border-spacing: 0;
table-layout: fixed;
}
col:first-child {
width: 50px;
}
col:nth-child(2) {
width: 100px;
}
td, th {
border: 1px solid black;
}
<table>
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
But if I add width: 0
to the table, it works.
table {
border-collapse: collapse;
border-spacing: 0;
table-layout: fixed;
width: 0;
}
col:first-child {
width: 50px;
}
col:nth-child(2) {
width: 100px;
}
td, th {
border: 1px solid black;
}
<table>
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
Why is setting width: 0
necessary? Is there a better way to do it?>