Sub topic 4
Naming a table with caption
This chapter assumes you can write a basic table with table, tr, td, and th, and that you can write simple CSS rules. It covers the caption element: where it goes, what it does for assistive technology, how to style it, and when to use something else.
What caption does
A caption element gives a table a title. It tells a reader what the data is before they read any cells. Browsers also expose the caption as the table's accessible name. A screen reader can announce "Monthly expenses, table, 3 rows, 2 columns" when a user reaches the table. Without a caption, the user hears only "table" and has to read cells to guess the purpose.
A caption is part of the table. A heading such as h2 placed above the table is not. Moving or restyling the table does not separate the caption from its data.
Placement rules
Follow these rules:
captionmust be the first child oftable. It comes beforecolgroup,thead,tbody,tfoot, andtr.- A table can have at most one
caption. - The caption can hold text and inline elements such as
em,a, orabbr. It must not contain another table or any table-structure elements.
<table>
<caption>Monthly expenses, January 2026</caption>
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Rent</th>
<td>900</td>
</tr>
<tr>
<th scope="row">Food</th>
<td>300</td>
</tr>
</tbody>
</table>Writing a good caption
A caption should say what the table contains, in a short phrase. Keep these points in mind:
- Name the subject and, if it matters, the period or unit: "Sales by region, 2025 (thousands of USD)".
- Do not start with "Table of" or "This table shows". Assistive technology already announces that it is a table.
- Do not repeat the caption in a heading directly above the table. Either the heading introduces a section and the caption names the table, or you use only one of them.
- Put longer explanation, such as how the data was collected, in a paragraph near the table. A long caption is read aloud every time the user reaches the table.
| Weak caption | Better caption |
|---|---|
| Table 1 | Monthly expenses, January 2026 |
| This table shows population and area | Population and area of Nordic capitals |
| Data | Quarterly sales by region, 2025 |
Styling the caption
By default, browsers center the caption above the table. Use caption-side to place it above (top) or below (bottom) the table. Use text-align for horizontal alignment.
caption {
caption-side: top;
font-weight: bold;
text-align: left;
padding-bottom: 0.5rem;
}caption-side changes only the visual position. The caption stays the first child in the source, so assistive technology still announces it first. Keep the markup order fixed and move the caption with CSS.
Caption versus other names
A table can get an accessible name in other ways. The right choice depends on whether sighted users should see the name.
<h2 id="expenses-heading">Expenses</h2>
<table aria-labelledby="expenses-heading">
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
</table>Here aria-labelledby points to an existing heading by its id. This works, but the link between heading and table exists only through that attribute. Prefer caption when the title belongs with the table. Use aria-label only when a visible name would be redundant. Avoid title on the table, because it is unreliable for touch and keyboard users.
Common errors
- Caption not first. Putting
captionaftertheadortris invalid HTML. Browsers may handle it inconsistently, so do not depend on the result. Validate your markup to catch it. - Two captions. Only one is allowed. If you need a title and a note, put the note in a paragraph or a
figurewithfigcaption. - Using a div as a title. A
divabove the table gives sighted users a title but gives assistive technology nothing. - Hiding the caption. If you hide it with
display: none, it is removed from the accessibility tree too. If it must be hidden visually, use a visually-hidden CSS technique instead. A visible caption is usually better.
Exercise
A page has a table whose first child is a tr, followed by a caption as the last child of the table. What is wrong, and how do you fix it while keeping the title displayed below the table?
Show answer
The caption must be the first child of table, so move it above the tr. To display it below the table, keep it first in the markup and add caption-side: bottom in CSS. The source order stays correct for assistive technology, and the visual position changes.