Complex Tables, Spans and Column Groups

Intermediate · 8 min

How colspan and rowspan merge cells, what colgroup and col are for, and why a table with merged cells needs extra care for people using assistive software.

Before thisAccessible Tables, Headers and Captions

Miljan Peric · 8 min

Complex Tables, Spans and Column Groups

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Miljan PericReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `colspan` makes a cell cover several columns and `rowspan` makes it cover several rows. The cells it covers are not written: every row's cell count, with spans added up, must match.
  • `colspan="0"` is treated as 1 in a test, but `rowspan="0"` is meaningful: the cell stretches to the end of its row group. In a test it was as tall as all three rows it spanned.
  • `<colgroup>` and `<col>` let you style or size whole columns without repeating a class on every cell. Only a few properties apply to a column, including `width`, `background` and `border`.
  • Merged cells are the main way a table breaks for screen reader users. Where a span is a header, use `id` and `headers` to say which header each cell belongs to, or split the table.
  • A table with spans has no single grid, so check the result visually and in the accessibility tree whenever you change one.

Why spans exist

A simple table is a clean grid: every row has the same number of cells, and every cell fits under one column header. Real data does not always fit. A report has a “2025” heading over two quarters. A schedule has a lunch break that runs across every column. A product comparison has a row where one note applies to all the plans.

You could add empty cells to fill the grid, but that tells the browser there are cells where there are none. HTML gives you two attributes to merge cells instead: colspan and rowspan.

colspan and rowspan

colspan makes one cell cover several columns in its row:

<tr>
  <th scope="col" colspan="2">2025</th>
  <th scope="col">2026</th>
</tr>
<tr>
  <td>Q1</td><td>Q2</td><td>Q1</td>
</tr>

The first row is one cell spanning two columns plus one more cell, so it fills three columns. The second row has three cells. That is the core rule: add up each row’s spans, and every row must come to the same number of columns. If a row is short, the browser does not complain; it just leaves a ragged edge. A mismatch is the most common bug in a table with spans, and it is easy to miss because nothing throws an error.

rowspan does the same thing downward. A cell with rowspan="2" covers its own row and the one below it, and the row below leaves out the cell it covers. People often get this wrong because the missing cell is in a different row than the one that holds the span.

I checked the edge values in a browser. colspan="0" was read as 1: a cell with it was the same width as a normal one in headless Chromium, Firefox and WebKit, and its colSpan property returned 1. rowspan="0" is different. It is a real value meaning “to the end of the row group”. A cell with rowspan="0" in a three-row table was 70px tall in Chromium and WebKit against 22px for a single row, so it covered all three rows. In Firefox the numbers were 73.6 and 23.2, the same proportion. Its rowSpan property still read 0.

Column groups

Sometimes the thing you want to say belongs to a whole column. You want the price column shaded, or the first column narrow. You could add a class to every cell, but a <colgroup> lets you say it once:

<table>
  <colgroup>
    <col>
    <col class="price">
  </colgroup>
  <tr><th>Item</th><th>Price</th></tr>
  ...
</table>

A <colgroup> holds <col> elements, one per column. A <col span="2"> covers two columns. Place <colgroup> after the <caption> and before any <thead>, <tbody> or <tr>. CSS allows only a handful of properties on columns, mainly width, background, border and visibility, because the cells inside are not children of the <col> in the DOM. If you want to change the text color in a column, a <col> will not do it; use td:nth-child(2) on the cells.

Spans and accessibility

Merged cells are a real cost for people using assistive technology. A screen reader builds its idea of “which header is above this cell?” from the grid. With spans, the grid is no longer regular, and the mapping can become wrong or ambiguous. A “2025” header over two columns does not tell the software which of the two columns a cell in the second row belongs to unless you say so.

The tools are the ones from accessible tables. scope="colgroup" and scope="rowgroup" on a <th> mark it as a header for a group of columns or rows. For anything stronger, give each header an id and give each cell a headers attribute listing the ids of every header that applies, as in <td headers="y25 q1">.

The honest advice, from MDN and the W3C, is to avoid complexity if you can. Two simple tables, each with one header row, are easier to build, easier to read and easier to maintain than one table with merged headers. Reach for spans when the data really has that shape, and check the result.

How to check

Add a border to the table while you work, so the grid is visible. Then count: does each row’s total add up to the same number? Open the accessibility panel in developer tools and look at how the header cells and data cells are reported. Finally, if the table matters, try it with a real screen reader. The testing HTML accessibility lesson covers how.

What the video covers

The embed is the whole of Miljan Peric’s “HTML5 Tutorial #16: Advanced HTML Tables, colspan, rowspan & colgroup” (October 2026), episode 16 of a beginner HTML5 series. Its description says it covers how colspan merges cells across columns, how rowspan merges across rows, and colgroup and col for grouping columns, using semantic HTML. It is from a small channel and is only a few weeks old at the time of writing, with no chapters. I could not read a transcript, so I do not describe what the demos build. The video is about construction; the accessibility advice, the row-counting rule and the browser measurements above are the lesson’s own. I tested the span behavior in headless Chromium, Firefox and WebKit; I did not test how any screen reader announces a spanned header.

Where this fits

This finishes the tables group after table structure and accessible tables. Table layout reappears in HTML email and legacy constraints, where tables still do layout work. Next the course moves to forms, where the data is collected rather than displayed.

Check yourself

  1. 01A row contains `<td colspan="2">A</td><td>B</td>`. The next row has three `<td>` cells. Is the table well-formed?

    Yes. The first row has a cell spanning two columns plus one more, so it fills three columns, which matches the three cells in the second row. The rule is that each row, counting spans, fills the same number of columns.

  2. 02What does `rowspan="0"` do?

    It makes the cell extend to the end of its row group (the `<thead>`, `<tbody>` or `<tfoot>` it sits in). In a test with three rows in one group, the cell was 70px tall against 22px for a single row in headless Chromium and the same height as the three rows combined.

  3. 03You want the second column of a table shaded. Why use `<colgroup>`?

    A `<col>` for that column can carry the background, so you set it once. Without it you would add a class to every cell in the column, and you would have to remember to add it to new rows. Only certain properties, such as `background`, `width` and `border`, apply to a `<col>`.

  4. 04Why do merged header cells cause trouble for screen reader users?

    The accessibility tree has to work out which headers apply to each cell. A header that spans several columns does not map cleanly to a single column, so a screen reader may announce the wrong header or none. Explicit `id` and `headers` links fix it, and simpler tables avoid it.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark