# Spanning & Accessible Tables

> colspan and rowspan let a single cell occupy multiple columns or rows. Spanning is what allows HTML tables to match the merged-cell layouts that data naturally suggests, and the headers attribute keeps that structure navigable by screen readers.

Subject: HTML | Lesson 24 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/table-spanning-and-a11y
Previous: [Table Structure & Caption](https://codewithanimation.in/html/table-structure-and-caption.md) | Next: [The form Element & Submission](https://codewithanimation.in/html/form-and-submission.md)

## What it is

`colspan` and `rowspan` are attributes on `<td>` and `<th>` that extend a cell across multiple column or row slots. `colspan="3"` means: this cell occupies three consecutive columns. `rowspan="2"` means: this cell continues down into the next row as well. The number is the count of slots consumed, including the cell's own home slot.

For header cells that span columns, `scope="colgroup"` declares that the header covers an entire `<colgroup>` of columns. For header cells that span rows, `scope="rowgroup"` covers an entire row group. When a table is complex enough that `scope` cannot unambiguously match a header to its data cells, the `headers` attribute on each `<td>` provides the link explicitly: it holds a space-separated list of `id` values from the relevant `<th>` elements.

## Why it matters

Real-world tabular data often has grouped structure: a conference with rooms that share a keynote slot, a product catalogue with variants that share a name, a timetable with days that span multiple time blocks. `colspan` and `rowspan` let the HTML table reflect that structure rather than flatten it into duplicated or empty cells. A visually merged cell that is not actually merged in the markup misleads assistive technology about the table's shape.

The `headers` attribute is the escape hatch for complex spanning. When a `<td>` sits under a column-spanning `<th>`, screen readers cannot always infer the association from `scope` alone, especially in multi-level header situations. Setting `headers="q1 revenue"` on each data cell (where `q1` and `revenue` are ids of the relevant header cells) creates an unambiguous, auditable link that survives any layout complexity.

## How it works

Add `colspan` or `rowspan` to the cell that should expand, then remove the displaced cells from the affected rows or columns. If you `colspan="2"` a cell, the cell that would have been in the next column of that row must not exist in the markup. If you `rowspan="2"` a cell in the first row, the second row has one fewer `<td>` than the other rows. Getting the cell count wrong causes the layout to misalign.

For a header that spans two columns and needs to associate with data below, give the `<th>` an `id`, then add a `headers` attribute to each data cell in those columns. For a straightforward column header, `scope="col"` is enough. Use `headers` when you have: grouped column headers above individual column headers, or a row header that labels only a subset of cells in that row.

The `<colgroup>` and `<col>` elements give you a hook for styling entire columns in CSS without touching every cell. A `<col>` inside `<colgroup>` targets the column at its index position. A `<th>` that precedes a `<colgroup>` with `scope="colgroup"` declares itself the header for every column in that group.

## Common mistake: Forgetting to remove the displaced cells

When you span a cell, you are claiming slots that would otherwise belong to sibling cells. If those sibling cells still exist in the markup, the table overflows: columns push out of alignment or cells appear in the wrong position. The rule is simple: for every slot a `colspan` or `rowspan` claims beyond the first, remove one cell from the affected row or column. A `colspan="3"` in one row means two fewer `<td>` elements in that row.

## Tip: Use headers + id for any table with more than one level of column headers

A table with a top row of group headers (such as "Q1" spanning two columns) and a second row of individual column headers ("Revenue" and "Units") has two header levels. A `<td>` in the data body is associated with both a group header and an individual header. `scope` cannot express this: it links a header to the cells directly below or to the right, not to cells below another header that itself is below a spanning header. Give each multi-level `<th>` a unique `id` and list those ids in a `headers` attribute on each `<td>`. The markup is verbose but the association is exact, and any conforming AT will read the cell with full context.

## Try it: Try it

See how `colspan` and `rowspan` shape the table grid, and how the `headers` attribute links data cells to their spanning headers.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/table-spanning-and-a11y

## Check yourself

### Question 1

A table row has three columns. You add colspan="2" to the first cell. How many <td> elements should that row now contain?

- A. 2: the spanning cell plus one more
- B. 3: the spanning cell plus the two it covers
- C. 1: only the spanning cell, as it now fills the whole row

Correct answer: A

`colspan="2"` merges the first cell with the slot to its right. The displaced cell is removed. The third column still needs its own `<td>`, leaving two cells total in the row.

- Why not B: colspan="2" claims two column slots with one cell. The cell that would fill the second slot must be removed. Three cells would overflow the table grid.
- Why not C: colspan="2" spans two of the three columns. The third column still needs its own cell. Only the cell displaced by the span is removed.

### Question 2

A <th rowspan="3"> appears in the first cell of a row in a four-column table. How many <td> cells do each of the NEXT two rows need?

- A. 3 each: the rowspan occupies one slot in those rows
- B. 4 each: rowspan does not remove slots from subsequent rows
- C. 2 each: rowspan and colspan both remove two cells

Correct answer: A

`rowspan="3"` means the cell occupies its own slot plus the same slot in the next two rows. Each of those rows is left with 3 `<td>` elements, not 4.

- Why not B: rowspan="3" claims a slot in the current row AND in the next two rows. Each of those two subsequent rows therefore needs one fewer cell to avoid overflowing the grid.
- Why not C: rowspan="3" claims one slot per affected row (the cell's own column slot). Only one cell per row is displaced, leaving three cells in each of the two subsequent rows of a four-column table.

### Question 3

When does scope become insufficient and the headers attribute become necessary?

- A. When a data cell is under more than one level of header (multi-level spanning headers)
- B. Whenever colspan or rowspan is used anywhere in the table
- C. Only in tables with more than ten rows

Correct answer: A

When a `<td>` sits under a column-spanning group header AND an individual column header, `scope` cannot express both associations. Giving each `<th>` an `id` and listing those ids in a `headers` attribute on the `<td>` makes the association explicit.

- Why not B: Simple spanning does not always break scope. A single-level colspan header with scope="colgroup" can still work. The headers attribute becomes necessary when a data cell has multiple ancestor headers that scope cannot uniquely resolve.
- Why not C: Row count is irrelevant. The need for headers is driven by header hierarchy complexity, not table size.

## Remember this

- For every slot a `colspan` or `rowspan` claims beyond the first, remove one cell from the affected row or column.
- `scope="colgroup"` marks a `<th>` as the header for all columns in a `<colgroup>`; `scope="rowgroup"` does the same for a row group.
- Use `id` on `<th>` and `headers="id1 id2"` on `<td>` when a cell is under multiple levels of header and `scope` is ambiguous.
- WCAG 1.3.1 requires that visual spanning be conveyed programmatically: a merged cell with no accessible header association fails the criterion.
