Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 122 ยท Tables

Column Groups

Use colgroup and col to describe and style whole table columns. Learn the span attribute, where column groups go, and the four CSS properties that work on columns.

Tables are written row by row, so there is no element that contains a column's cells. Yet you often want to treat a column as a unit: give it a fixed width, tint its background or highlight the one column a reader should compare. That is what <colgroup> and <col> are for.

They describe the columns of a table before any rows appear. They don't hold any content themselves; they simply give names and styles to vertical slices of the grid.

<colgroup> and <col>

A column group goes inside <table>, after the <caption> (if there is one) and before <thead>, <tbody> or any rows. Inside it, each <col> element stands for one column, counting from the left.

Highlighting one column

One class on one <col> colored an entire column, including the header. Without column groups, you'd need a class on every cell of that column, or a selector like td:nth-child(3), which breaks as soon as someone adds a column or a cell spans two columns.

Try moving class="chosen" to the last <col> to highlight the Agency plan instead.

<col> is a void element. It has no closing tag and no content. All it carries is attributes such as class, span and style.

The span attribute

When several neighboring columns should be treated the same way, you don't have to repeat <col>. The span attribute makes one <col> stand for several columns:

HTML
<colgroup>
  <col>                          <!-- column 1 -->
  <col span="3" class="weekday"> <!-- columns 2, 3 and 4 -->
  <col span="2" class="weekend"> <!-- columns 5 and 6 -->
</colgroup>

You can also put span on <colgroup> itself, but only when that group has no <col> children. Both of these describe the same three columns:

HTML
<colgroup span="3"></colgroup>

<colgroup>
  <col span="3">
</colgroup>

Several column groups

A table may contain more than one <colgroup>. Each group is a meaningful set of columns, such as "the label column" and "the data columns". That grouping is exactly what scope="colgroup" on a header refers to:

Two column groups

The "Quarter 1" header has scope="colgroup", so it labels all three columns of the group it sits above. The column groups also give each quarter its own tint with one class each.

Which CSS properties work on columns?

This is the part that surprises everyone. You might expect to set color, padding or text-align on a <col> and have every cell in the column follow. They don't. Cells are children of rows, not of columns, so most properties never reach them.

Only four properties have an effect on <col> and <colgroup>:

Property Effect
background (and its parts) Paints behind the cells of the column, unless a row or cell has its own background on top.
border Draws borders around the column, but only when the table uses border-collapse: collapse.
width Sets the column's width (a minimum in the default automatic table layout).
visibility: collapse Hides the column completely, as if it weren't there. Support varies across browsers, so test before relying on it.

Backgrounds have a stacking order. From bottom to top, the browser paints the table, then column groups, columns, row groups, rows and finally cells. A background on a <tr> or <td> covers the column's background. If your column highlight doesn't show, check whether something above it has its own background.

Styling the content of a column

For properties like text-align or font-weight, target the cells themselves. Two common approaches:

CSS
/* Right-align every cell in the third column */
td:nth-child(3) { text-align: right; }

/* Or put a class on the cells that need it */
td.amount { text-align: right; font-variant-numeric: tabular-nums; }

Keep in mind that :nth-child counts cells in the row's code, not visual columns, so a colspan earlier in the row shifts the count. You'll see more patterns like this in Styling Tables with CSS.

Setting column widths

Widths are the most practical use of <col>. Combined with table-layout: fixed, the browser sizes columns from the first row and the <col> widths alone, instead of measuring every cell. That's faster for big tables and keeps columns steady when the content changes:

HTML
<style>
  table { table-layout: fixed; width: 100%; }
</style>

<table>
  <colgroup>
    <col style="width: 40%">
    <col style="width: 20%">
    <col style="width: 20%">
    <col style="width: 20%">
  </colgroup>
  โ€ฆ
</table>

Prefer classes to inline styles. The style attribute is fine for a quick demo, but a class on each <col> keeps the widths in your stylesheet with the rest of your design.

When column groups are worth it

Column groups are optional. Plenty of good tables never use them. They earn their place when:

  • One column needs to stand out, such as the recommended plan in a pricing comparison or today's column in a weekly schedule. Moving a single class is far easier than editing every row.
  • Columns form meaningful sets, like months grouped into quarters or "home" and "away" results in a sports table. The groups document that structure in the code, and scope="colgroup" headers can refer to them.
  • You need predictable widths, especially in large tables with table-layout: fixed, where the browser can lay out the table without reading every cell first.

They're not the right tool for styling text inside a column, and they can't replace header cells: a <col> has no visible label, so every column still needs a <th> that tells readers what it contains.

Count spans carefully. The columns described by your <col> and <colgroup> elements should add up to the number of columns in the table. If they describe fewer, the remaining columns simply belong to no group. A miscount never raises an error; your styles just land on the wrong columns, so a quick count saves confusion.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does <colgroup> go inside a table?

    Column groups describe the columns before any rows appear, so they come right after the caption (if any) and before the row groups.
  2. 2. You set text-align: right on a <col>. What happens?

    Cells are children of rows, not columns, so inherited properties like text-align never reach them. Target the cells with a selector instead.
  3. 3. Which of these properties does work on a <col>?

    Only backgrounds, borders (with collapsed borders), width and visibility: collapse have an effect on columns.
  4. 4. Which markup describes the same columns as <col span="2"> inside a colgroup?

    span="2" is shorthand for two <col> elements. colspan belongs to cells, and <colgroup> may not use span when it has <col> children.

Summary

  • <colgroup> and <col> describe columns; they go after <caption> and before any rows.
  • <col> is a void element, and span makes one <col> cover several columns.
  • <colgroup span="n"> works only when the group has no <col> children.
  • Only background, border (with collapsed borders), width and visibility: collapse affect columns.
  • For alignment, fonts and colors, style the cells with selectors or classes.
  • scope="colgroup" on a header labels all the columns in its group.

Next lesson: Styling Tables with CSS.