Sub topic 8
Styling whole columns with colgroup and col
This chapter assumes you can write a basic table with table, tr, td, and th, and that you know CSS class selectors and properties. It also assumes you have seen thead and tbody, since column groups sit beside them in the table structure.
The problem colgroup solves
Suppose the last two columns of a table hold numbers and you want a tinted background and a fixed width on both. You could add a class to every cell in those columns. That works, but it is repetitive and easy to get wrong when rows are added later.
A column group lets you name columns once, at the top of the table, and style them as a unit.
The working model
A colgroup element is a child of table. It holds one or more col elements. Each col stands for one column, or several columns if it has a span attribute. A col has no content and no closing tag.
The browser matches each col to columns by position, counting from the left. It does not link a col to any particular cell in the markup.
<table>
<caption>City facts</caption>
<colgroup>
<col>
<col span="2" class="numeric">
</colgroup>
<thead>
<tr>
<th scope="col">City</th>
<th scope="col">Population</th>
<th scope="col">Area (km²)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Oslo</td>
<td>700000</td>
<td>454</td>
</tr>
<tr>
<td>Bergen</td>
<td>290000</td>
<td>465</td>
</tr>
</tbody>
</table>The first col covers column 1 and has no styling. The second has span="2", so it covers columns 2 and 3 and carries the numeric class.
.numeric {
background: #eef6ff;
width: 8rem;
}Both numeric columns now share the tint and the width, and no cell needed a class.
Placement rules
The position of colgroup is strict. It must come after caption (if there is one) and before thead, tbody, tfoot, and any tr elements. Putting it later produces invalid HTML, and browsers may handle it inconsistently.
You can also write colgroup with a span attribute and no col children. In that case the group covers that many columns as one unit. If a colgroup contains col children, do not give the colgroup a span, because the children define the columns.
Which CSS properties work
Only a short list of properties has any effect on col and colgroup:
| Property | Effect on a column |
|---|---|
| `background` | Paints behind the cells in the column |
| `width` | Sets the minimum width of the column |
| `border` | Draws column edges, but only reliably with `border-collapse: collapse` |
| `visibility` | `collapse` hides the column and frees its space |
This is the most common error. Properties such as text-align, color, and font-weight set on a col do not reach the cells in that column. Cells do not inherit from columns, because a column is not an ancestor of its cells in the document tree. To right-align numbers, you still need a rule on the cells themselves, for example td:nth-child(n+2) { text-align: right; }.
Interaction with spanning cells
Columns are counted on the table grid, not by counting td elements in a row. A cell with colspan="2" occupies two grid columns, so it sits across two col entries. If you merge cells, count grid columns when deciding how many col elements you need. A mismatch, such as three col entries for a four-column grid, leaves the extra column unstyled.
Common mistakes
- Placing
colgroupaftertheadortbody. - Writing
<col></col>or<col />with content.colis a void element and takes no content. - Expecting
text-alignorcoloron acolto style the cell text. - Forgetting that
spandefaults to 1, so acolcovers one column unless you say otherwise. - Using more
colcolumns than the grid has, which has no visible effect and hides the error.
Exercise
A table has four columns. You want the second and third columns to have a yellow background and the fourth to be 6rem wide. Write the colgroup markup. Then explain why adding text-align: right to the fourth col would not align that column's text.
Show answer
Use <colgroup><col><col span="2" style="background: yellow"><col style="width: 6rem"></colgroup>, or classes in place of the inline styles. It sits after caption and before the row groups. The text-align rule fails because text-align is not one of the properties that apply to columns, and cells do not inherit from col. You must target the cells, for example with td:nth-child(4).