Sub topic 4
Naming a Table with caption
This chapter covers the <caption> element: what it does, where it goes, how it differs from a heading, how to style it, and how it interacts with accessibility tools. It assumes you can write a basic table with table, tr, td, and th, and that you know basic CSS selectors and properties.
What caption is for
A table of numbers means little without context. A reader needs to know what the rows and columns measure before the values make sense. The caption element gives a table a title or short description that belongs to that table and no other.
The browser and assistive technology both treat the caption as part of the table. This is the main reason to prefer it over a nearby paragraph or heading. When you move, copy, or restructure the table, the caption comes with it. A screen reader announces the caption when the user reaches the table, so a person who navigates by tables hears what each one is before deciding whether to enter it.
<table>
<caption>Monthly expenses</caption>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
<tr>
<th scope="row">Rent</th>
<td>900</td>
</tr>
<tr>
<th scope="row">Food</th>
<td>300</td>
</tr>
</table>The caption names the table's subject. The header cells then name each row and column.
Placement rules
The caption element must be the first child of its table. Nothing may come before it, not a tr, not a colgroup, not a thead. A table may have only one caption.
The order of the allowed children is:
caption(optional, at most one)colgroupelements (zero or more)thead(optional)tbodyelements ortrelementstfoot(optional)
The HTML parser is forgiving, but a caption in the wrong place is invalid markup. Browsers may handle it in unexpected ways. A validator reports an error.
<table>
<caption>Population and area of cities</caption>
<colgroup>
<col>
<col span="2" class="numeric">
</colgroup>
<tr>
<th scope="col">City</th>
<th scope="col">Population</th>
<th scope="col">Area</th>
</tr>
<tr>
<td>Oslo</td>
<td>700000</td>
<td>454</td>
</tr>
</table>Here the caption comes before the colgroup, as required.
What a caption can contain
The content model of caption is flow content, with one restriction: it must not contain a descendant table element. In practice you will use plain text, and sometimes inline elements such as em, strong, abbr, or a. Longer captions can hold a few sentences, or even a paragraph, but keep them short enough to scan.
<table>
<caption>
Quarterly sales by region, in thousands of units.
<em>Figures for 2025 are provisional.</em>
</caption>
<tr>
<th scope="col">Region</th>
<th scope="col">Q1</th>
</tr>
<tr>
<th scope="row">North</th>
<td>120</td>
</tr>
</table>A good caption answers "what is this table about?" in one line. Put the unit of measure or the time period in it when the cells do not repeat that information.
Caption versus a heading
Both a heading (h2, h3, and so on) and a caption can introduce a table, and they do different jobs.
| Aspect | `caption` | Heading element |
|---|---|---|
| Belongs to the table | Yes, it is a child of `table` | No, it is a sibling placed before it |
| Appears in the document outline | No | Yes |
| Announced when entering the table | Yes | No, it is read as normal page text |
| Best for | Naming the table itself | Starting a section of the page |
Use a heading when the table sits inside a section of the page and the section has a topic. Use a caption when the table needs its own name that stays with it. The two can coexist. A section titled "Budget" might hold a table captioned "Monthly expenses, January to June".
Avoid repeating the same text in both places. Screen reader users would hear it twice in a row.
Accessible names and the caption
Every table can have an accessible name. This is the text that assistive technology reads to identify it. The caption element supplies that name automatically. You do not need aria-label or aria-labelledby when a caption is present.
If you do need a different name, the browser applies the usual precedence. An aria-labelledby or aria-label attribute on the table overrides the caption for the accessible name. Use this sparingly. Most of the time the visible caption should be the name, so that sighted and non-sighted users get the same information.
<table aria-describedby="sales-note">
<caption>Sales by region</caption>
<tr>
<th scope="col">Region</th>
<th scope="col">Total</th>
</tr>
<tr>
<th scope="row">North</th>
<td>255</td>
</tr>
</table>
<p id="sales-note">Totals combine the first two quarters of 2025.</p>In this example the caption is the name and the paragraph is a longer description. Linking the description with aria-describedby keeps the caption short while still making the extra detail available. This split is the usual pattern for a table that needs more explanation than a title can carry.
Styling a caption
By default a caption is displayed above the table and centered across the table's width. You can change both with CSS.
caption {
caption-side: top;
font-weight: bold;
text-align: left;
padding-bottom: 0.5rem;
}The caption-side property accepts top and bottom. Placing a caption below the table is a presentation choice. It does not change the DOM order, which means a screen reader still reads the caption first. The logical values block-start and block-end are also defined and adapt to the writing mode, though browser support for them is newer, so check your targets before relying on them.
A few practical points:
- A caption is laid out as a block box tied to the table's width. It does not make the table wider.
- Setting
text-alignon the caption controls its alignment, independent of the cells. - Margins on a caption behave differently than on normal blocks. Use
paddingwhen you want space between the caption and the table body. - Do not use
display: noneto hide a caption. This removes it from the accessibility tree, so the table loses its name.
Hiding a caption visually
Sometimes the design already shows a heading and a visible caption would repeat it. The accessible solution is to hide the caption visually while keeping it available to screen readers. This pattern is often called "visually hidden".
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}<table>
<caption class="visually-hidden">Monthly expenses</caption>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
</table>Use this only when a visible title already sits next to the table. A visible caption helps everyone, including people with cognitive disabilities and people who skim the page, so keep it visible by default.
Captions and wide tables
When you wrap a table in a scrolling container to handle small screens, decide where the caption lives. If the caption is inside the table, it scrolls horizontally with the table. A long caption may then slip out of view. You can keep the title visible by putting a heading above the scrolling wrapper, or by giving the wrapper an aria-labelledby that points at a heading. Whichever you choose, keep the table's own caption so the table stays named when read on its own.
<div class="table-scroll" tabindex="0" role="region" aria-labelledby="exp-title">
<table>
<caption id="exp-title">Monthly expenses</caption>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
</table>
</div>Here the id on the caption lets the scroll region reuse the same text as its label, so the text is written only once.
Common mistakes
- Putting the caption after the rows. It must be the first child of
table. Move it up. - Using a
divorpabove the table as a fake caption. It looks the same but is not tied to the table, so assistive technology does not announce it as the table's name. - Using
titleon the table instead of a caption. Thetitleattribute is hidden from touch and keyboard users in most browsers and is an unreliable name. - Writing a caption that only says "Table". It adds no information. Describe the content.
- Writing a caption that restates the column headings. The caption names the subject. The headers already describe the columns.
- Adding a second caption. Only one is allowed. Combine the text, or use
aria-describedbyfor extra detail. - Hiding the caption with
display: none. Use the visually hidden pattern if you must hide it.
Checking your work
You can check a caption in three ways.
- Run the page through an HTML validator and confirm there are no errors about caption placement.
- Open the browser's accessibility inspector and look at the table node. The computed name should equal your caption text.
- Turn on a screen reader and move to the table. The caption should be announced first.
A diagram helps show where the caption sits in the structure of a table and how it relates to the other children.
Summary of rules
- Use
captionto give a table a title that travels with it. - Place it as the first child of
table, and use only one. - Keep it short, and put units or periods in it when the cells do not.
- Choose a heading when you are starting a page section, a caption when you are naming a table, and both when each adds something.
- Style it with
caption-side,text-align, andpadding. - Never remove it with
display: none.
Exercise
A page has a table preceded by <p class="title">Quarterly sales</p>, and the table element starts with a tr. A teammate says the p already works as a caption. Rewrite the start of the table so the title is a true caption, then explain one benefit for screen reader users.
Show answer
Replace the paragraph with <caption>Quarterly sales</caption> and place it as the first child of table, before the first tr. The table then has an accessible name. A screen reader announces "Quarterly sales" when the user reaches the table, so they know what it contains before reading cells. A separate paragraph is not linked to the table, so it is not announced as the table's name.
Exercise
A designer wants the caption below the table, left-aligned, and bold. Write the CSS. Does the move below the table change the order in which a screen reader reads it?
Show answer
Use caption { caption-side: bottom; text-align: left; font-weight: bold; }. The move is visual only. The caption stays the first child of table in the DOM, so a screen reader still reads it first.
Exercise
A table has <caption>Sales</caption> and also needs a note that totals combine two quarters. How do you add the note without making the caption long?
Show answer
Put the note in a separate element with an id, such as <p id="sales-note">Totals combine the first two quarters.</p>, and add aria-describedby="sales-note" to the table. The caption stays a short name and the note is the description.