# Table Structure & Caption

> A proper table is a grid of rows and columns with a caption for the accessible name and header cells that tell screen readers how every data cell relates to its row and column.

Subject: HTML | Lesson 23 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/table-structure-and-caption
Previous: [iframe, sandbox & Embeds](https://codewithanimation.in/html/iframe-and-embeds.md) | Next: [Spanning & Accessible Tables](https://codewithanimation.in/html/table-spanning-and-a11y.md)

## What it is

An HTML table starts with `<table>` and is built from rows (`<tr>`) that contain either header cells (`<th>`) or data cells (`<td>`). Rows are grouped into three semantic sections: `<thead>` for column headers, `<tbody>` for the main data, and `<tfoot>` for summary rows like totals. `<caption>` is an optional but important element that sits as the very first child of `<table>` and provides a visible title.

`<th>` cells accept a `scope` attribute that declares the direction of the header: `scope="col"` means this cell is the header for its entire column, and `scope="row"` means it is the header for its entire row. This association is what lets assistive technology read a cell and immediately announce which column header and row header it belongs to.

## Why it matters

`<caption>` is the table's accessible name. When a screen reader user encounters a table, the caption is the first thing announced, giving context before a single data cell is read. Without a caption, the table is announced as a generic table and the user has no immediate way to know what the data is about. A good caption is one sentence that names the data: not "Table 1" but "Monthly server uptime by region".

The `scope` attribute on `<th>` is what turns a visual grid into a navigable structure. Screen readers use scope associations to read a cell as "Row: Japan, Column: Q3, Value: 98.2%" rather than just "98.2%". Without scope, the column context is lost and multi-column tables become very hard to parse by ear.

## How it works

Build the table top to bottom: `<caption>`, then `<thead>`, then `<tbody>`, then `<tfoot>` if you have summary rows. Put column headers in `<th scope="col">` inside the `<thead>`. Put row headers (if the first column labels the row) in `<th scope="row">` inside each `<tbody>` row. Every other cell is `<td>`.

For styling, reach for CSS rather than the deprecated `border`, `cellpadding`, and `cellspacing` attributes. `<colgroup>` and `<col>` let you style entire columns without adding classes to every cell. A `<col>` inside `<colgroup>` targets the column at that index: `<colgroup><col><col style="background: var(--highlight)"></colgroup>` highlights the second column.

A `<tfoot>` row can hold totals or averages. It is placed after `<tbody>` in the source but browsers have traditionally rendered it at the bottom of the table regardless of source order (this behaviour is now deprecated; just put `<tfoot>` last).

## Common mistake: Using a table for page layout instead of tabular data

Layout tables were a workaround from the late 1990s, before CSS existed to position elements reliably. They produce markup that screen readers try to interpret as data, announcing row and column counts that mean nothing. CSS Grid and Flexbox replaced layout tables entirely. If the content is not genuinely a grid of labelled rows and columns, use a layout tool, not `<table>`.

## Tip: Write the caption as the table's one-sentence answer to 'what is this?'

If someone asked you to summarise the table in a single sentence before showing them the data, your answer is the caption. "Q3 sales by region" tells the reader the time period, the metric, and the breakdown. "Data table" tells them nothing. The caption also appears in the accessibility tree as the table's label, so it doubles as documentation for any tool that processes the page.

## Try it: Try it

Watch the table assemble in order: caption first, then the header row, then each data row in turn.

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

## Check yourself

### Question 1

What is the accessible role of <caption> on a table?

- A. It provides the table's accessible name, announced before the data
- B. It adds a decorative title above the table with no semantic effect
- C. It replaces the need for <th> elements

Correct answer: A

`<caption>` is the table's accessible name. A screen reader user hears it before navigating any cells. A good caption answers "what is this data about?" in one phrase.

- Why not B: <caption> is not decorative. It is the table's accessible name in the accessibility tree, announced by screen readers before any cell is read.
- Why not C: <caption> names the table; <th> with scope labels individual columns and rows. Both are needed for a fully accessible table.

### Question 2

A table's first column contains country names. What scope value should those <th> cells have?

- A. scope="row"
- B. scope="col"
- C. No scope is needed; <th> conveys header semantics on its own

Correct answer: A

A `<th>` in the first column that labels the row needs `scope="row"`. The top-row headers that label columns need `scope="col"`. Together they create the full grid of associations screen readers use.

- Why not B: scope="col" marks a cell as the header for an entire column. Country names in the first column are headers for their row, so scope="row" is correct.
- Why not C: Without scope, a screen reader cannot tell whether a <th> is a row header or a column header. Adding scope disambiguates the association so cells are announced with full context.

### Question 3

What is the correct order of structural sections inside a <table>?

- A. <caption>, <thead>, <tbody>, <tfoot>
- B. <thead>, <caption>, <tbody>, <tfoot>
- C. <thead>, <tfoot>, <tbody> (tfoot before tbody for visual rendering)

Correct answer: A

`<caption>` comes first, then `<thead>`, then `<tbody>`, then `<tfoot>`. This is both the valid and the logical reading order for the table's structure.

- Why not B: <caption> must be the first child of <table>, before any row group elements. Moving it after <thead> is invalid markup.
- Why not C: Placing <tfoot> before <tbody> in source order to influence rendering is a legacy workaround that is now deprecated. Write the sections in the logical reading order: thead, tbody, tfoot.

## Remember this

- `<caption>` is the first child of `<table>` and provides the table's accessible name.
- `<th scope="col">` labels a column; `<th scope="row">` labels a row; both are needed for screen reader navigation.
- `<thead>`, `<tbody>`, `<tfoot>` group rows semantically and enable CSS tricks like sticky column headers.
- Use tables for tabular data only; use CSS Grid or Flexbox for page layout.
