Someone who can see The Blue Cup's menu finds the price of a cappuccino in a second: they run their eye along the row and glance at the column title. A blind person can't "take a quick look": their screen reader goes through the table cell by cell and can only tell them which row and column "€2.20" belongs to if the code makes it clear. In the previous lessons you already took the first step, using th for the headers and grouping the rows. In this lesson you will finish the job: you will learn how screen readers read tables, how to give them a title with caption, how to state unambiguously which headers apply to each cell with scope and, in complex tables with merged cells, with id and headers. By the end, the site's three tables (menu, lunch menu and opening hours) will be accessible.
Contents
- How screen readers read tables
- A title for the table:
caption scope: which cells each header applies to- Complex tables:
idandheaders - Avoiding nested tables
- Longer descriptions (just in passing)
- Tables on small screens (just in passing)
- How to check a table's accessibility
- Project: accessible tables at The Blue Cup
How screen readers read tables
A screen reader is a program that turns the content of the screen into speech (or braille). The most widely used are NVDA and JAWS on Windows, VoiceOver on macOS and iOS, and TalkBack on Android. When it comes across a well-built data table, this is what happens:
- It announces the table: "Table, Price list, 15 rows, 4 columns". The table's name comes from its title, if it has one.
- The user enters a table navigation mode: with key combinations (in NVDA and JAWS,
Ctrl+Alt+ arrow keys) they move to the cell above, below, to the left or to the right. - As they move, the screen reader announces the headers that change and the content of the cell. Going down the "Price" column, they hear: "Cortado, €1.50", "White coffee, €1.80". Moving along a row, they hear: "Size, 90 ml", "Allergens, Milk", "Price, €1.50".
To be able to do this, the screen reader needs to know which cells are headers (that's what you used th for) and which cells each header applies to. In simple tables, with headers only in the first row or the first column, browsers work this out well. But as soon as there are row and column headers, groups or merged cells, the guesswork fails, and the person hears figures with no context.
Remember table-based layout: if a table contains no
that all and its structure looks like a layout, many browsers treat it as a layout table and don't offer table navigation mode. Another reason to usethfor every real header.
A title for the table: caption
caption is the title of the table. It's written as the first child of table, before colgroup and thead:
<table>
<caption>Black coffees: size and price</caption>
<thead>
<tr>
<th>Product</th>
<th>Size</th>
<th>Price</th>
</tr>
</thead>
…
</table>- It's visible: by default, the browser displays it centred above the table.
- It's associated with the table: the screen reader announces it on entering the table and uses it in its list of the tables on the page. A preceding paragraph or heading, on the other hand, isn't part of the table.
- There can only be one per table, and it always comes first.
- It can contain text with inline formatting, such as
abbrorsmall.
The complete order of the children of table is as follows:
graph LR
A["caption"] --> B["colgroup / col"]
B --> C["thead"]
C --> D["tbody (one or more)"]
D --> E["tfoot"]
caption and the page headings
If the table is already under an h2 or h3, do you need a caption? Yes, because they serve different purposes:
h2/h3 heading |
caption |
|
|---|---|---|
| Organises | The page | The table |
| Used by the screen reader | In the list of headings on the page | When announcing the table and in the list of tables |
| Can contain | A section title | A title that describes the data |
The recommendation is to keep the section heading and have the caption describe the data more specifically, without repeating the same text. For example, under the h2 "Price list", a caption such as "Specialities and coffees: size, allergens and price".
In old HTML you will see the
summaryattribute ontable(<table summary="…">), intended to give a summary to screen readers only. It's obsolete in HTML5: usecaptionand, if more is needed, a descriptive paragraph.
scope: which cells each header applies to
The scope attribute goes on a th and indicates in which direction that header applies:
| Value | The header applies to… | Example at The Blue Cup |
|---|---|---|
col |
All the cells in its column, below it | "Price" in the menu's head |
row |
All the cells in its row, to the right | "Cappuccino" at the start of its row |
colgroup |
All the cells in the column group (colgroup) it heads |
"Monday to Friday" in the opening hours |
rowgroup |
All the cells in the row group (tbody, thead or tfoot) it sits in |
"Black coffees" on the menu |
<table>
<caption>Black coffees: size and price</caption>
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Size</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Espresso</th>
<td>30 ml</td>
<td>€1.40</td>
</tr>
</tbody>
</table>With this code, when the screen reader reaches "€1.40", it knows that its headers are "Espresso" (from the scope="row" in its row) and "Price" (from the scope="col" in its column), and it can announce "Espresso, Price, €1.40".
colgroup and rowgroup: group headers
For scope="colgroup" to work, the table must have its column groups defined with colgroup, as you did in the opening hours. The header applies to the columns of the group in which it starts:
<colgroup></colgroup> <!-- column 1 -->
<colgroup span="5"></colgroup> <!-- columns 2 to 6 -->
<colgroup span="2"></colgroup> <!-- columns 7 and 8 -->
<thead>
<tr>
<th rowspan="2" scope="col">Service</th>
<th colspan="5" scope="colgroup">Monday to Friday</th>
<th colspan="2" scope="colgroup">Weekend</th>
</tr>
…"Monday to Friday" starts in column 2, which belongs to the second colgroup: that's why it applies to columns 2 to 6. So a cell for Wednesday has two column headers: "Wednesday" and "Monday to Friday".
scope="rowgroup" works the same way with rows: a th with this value inside a tbody heads all the rows in that tbody. That's exactly the case of the category rows on the menu: "Black coffees" is the header for its whole group.
Practical rules for scope
scopeonly goes on ath. On atdit isn't valid.- Always write it in tables with both row and column headers, even if browsers could work it out. It's little effort and removes any doubt.
- The corner cell that titles the column of row headers ("Service", "Product") gets
scope="col": it's the title of that column. - If the corner is empty, it can be an empty
tdinstead of athwith no text: an empty header heads nothing.
Complex tables: id and headers
scope works very well when the headers are in the first row and the first column (or in their groups). But there are tables where the relationship between data and headers doesn't follow those straight lines, especially when there are merged headers in the middle of the table. That's the case with the lunch menu:
| Dish | Options | Price |
|---|---|---|
| Starter (2 rows) | Pumpkin soup | €9.50 (6 rows) |
| ↑ | Seasonal salad | ↑ |
| Main (2 rows) | Vegetable quiche | ↑ |
The "Seasonal salad" cell is in a row that doesn't have its own row header: it inherits it from "Starter", which comes from the row above. And "€9.50" applies to all the dishes. Some screen readers interpret these merges correctly with scope and others don't. When the relationship must be guaranteed, it's declared cell by cell with two attributes:
- Each header (
th) is given anidthat is unique on the page. - Each data cell is given a
headersattribute with the list of its headers'ids, separated by spaces.
<table>
<caption>Lunch menu: dishes and price</caption>
<thead>
<tr>
<th id="lunch-dish">Dish</th>
<th id="lunch-options">Options</th>
<th id="lunch-price">Price</th>
</tr>
</thead>
<tbody>
<tr>
<th id="lunch-starter" headers="lunch-dish" rowspan="2">Starter</th>
<td headers="lunch-starter lunch-options">Pumpkin soup</td>
<td headers="lunch-price" rowspan="2">€9.50</td>
</tr>
<tr>
<td headers="lunch-starter lunch-options">Seasonal salad</td>
</tr>
</tbody>
</table>Line by line:
- The three
thelements in the head have anid:lunch-dish,lunch-optionsandlunch-price. Thelunch-prefix avoids clashes with otherids on the page, such ascoffeesorspecialities, becauseids must be unique across the whole document. - "Starter" is also a
thwith anid, and its ownheaders="lunch-dish"indicates that it belongs to the "Dish" column. Athcan haveheaderstoo. - "Seasonal salad" declares its two headers:
lunch-starter(its row, even though it's merged from above) andlunch-options(its column). The screen reader will announce "Starter, Options, Seasonal salad" without depending on how it interprets therowspan. - The price declares its column header,
lunch-price.
scope |
id + headers |
|
|---|---|---|
| Where it's written | Only on the th elements |
On the th elements (id) and on every data cell (headers) |
| Effort | Low | High: all the references have to be maintained |
| When to use it | In the vast majority of tables | In complex tables, with merged headers in the middle or several irregular levels |
| Risk | Differing interpretations in complex tables | If you change an id and forget a headers, the relationship breaks silently |
The best advice about
headers: if you find yourself writingheadersin a huge table, first consider whether you can simplify it or split it into several tables. A simple table withscopeis easier for everyone to maintain and understand.
Avoiding nested tables
HTML allows you to put a table inside a cell of another table. For data, don't do it:
- The screen reader enters a table inside another table and the user loses track of where they are and which headers apply.
- Header relationships don't cross tables: the
thelements of the outer table don't apply to the cells of the inner one. - Visually it's usually confusing too.
Nested tables were typical of the table-based layout you saw in Basic Table Structure. If you feel the need to nest, the solution is almost always one of these: several tbody elements with group rows, two consecutive tables, each with its own caption, or merging header cells to create levels.
Longer descriptions (just in passing)
Sometimes a table needs more explanation than a title: how to read it, what its abbreviations mean, what exceptions it has. The simplest option, and the most accessible for everyone, is a visible paragraph just before the table. If you also want the screen reader to read it on entering the table, you can link it with the aria-describedby attribute, which points to the paragraph's id:
<p id="lunch-description">Monday to Friday, from 13:00 to 16:00. Choose a starter, a main, a dessert and a drink.</p>
<table aria-describedby="lunch-description">
<caption>Lunch menu: dishes and price</caption>
…
</table>The screen reader will announce the title (from the caption) and then the description (from the paragraph). The aria-* attributes are part of ARIA, a set of accessibility attributes that you will study in detail in Using ARIA Roles and Attributes (Module 10). Use it only as an extra: the caption and a good structure are what matter.
Tables on small screens (just in passing)
An eight-column table, like the opening hours, doesn't fit on a mobile screen. The solutions (allowing horizontal scrolling inside a container, rearranging the cells with CSS, or simplifying the table for small screens) are part of responsive design and you will see them in Module 9. What matters now: none of them should break the structure you have just built; accessibility is designed into the HTML, and the appearance comes afterwards, with CSS.
How to check a table's accessibility
You don't need to be a screen reader expert to carry out a first check:
- The accessibility tree in DevTools: in Chrome or Edge, select a cell in the Elements panel and open the Accessibility tab. You will see the cell's role (
cell,columnheader,rowheader) and the table's name, taken from thecaption. In Firefox, the Accessibility Inspector offers something similar. - A real screen reader: NVDA is free for Windows; VoiceOver comes with macOS (
Cmd+F5to turn it on). Enter the table and move around it with the table navigation key combinations. Listen to whether each piece of data is announced with its headers. - The isolated cell test: pick any cell and ask yourself: "if I only heard this cell and its headers, would I understand it?". "Bread-making workshop, Saturday, 10:00–13:00" makes sense; "10:00–13:00" on its own doesn't.
In Accessibility Testing (Module 10) you will learn about automated tools and a more thorough review method.
Project: accessible tables at The Blue Cup
- The price list
In menu.html, add a caption and the scope attributes to the table in div#prices (the start of the table is shown, along with one complete group and the pattern for the rest):
<table>
<caption>Specialities and coffees: size, allergens and price</caption>
<colgroup>
<col class="col-product">
<col span="2" class="col-details">
<col class="col-price">
</colgroup>
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Size</th>
<th scope="col">Allergens</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody id="specialities">
<tr>
<th colspan="4" scope="rowgroup">House specialities</th>
</tr>
<tr>
<th scope="row">Autumn seasonal coffee</th>
<td>200 ml</td>
<td>None</td>
<td>€2.50</td>
</tr>
<tr>
<th scope="row">Butter croissant</th>
<td>1 piece</td>
<td>Gluten, milk, egg</td>
<td>€2.40</td>
</tr>
<tr>
<th scope="row">Lemon sponge cake</th>
<td>1 slice</td>
<td>Gluten, milk, egg</td>
<td>€2.20</td>
</tr>
<tr>
<th scope="row">Sourdough bread</th>
<td>500 g loaf</td>
<td>Gluten</td>
<td>€3.00</td>
</tr>
</tbody>
<!-- In the #coffees and #milk-coffees groups, the same:
scope="rowgroup" on the category row
and scope="row" on each product's th. -->
…
</table>- The
captiondescribes the data without repeating theh2"Price list". - The four column titles get
scope="col". - Each category row gets
scope="rowgroup": "Black coffees" heads its wholetbody. A user checking the price of the americano will also hear which group it belongs to. - Each product gets
scope="row". - The
tfootdoesn't change: it's a note, not a header.
- The lunch menu
This is the site's complex table: give it a caption, id + headers and the description with aria-describedby. Replace the paragraph and the table in div#lunch-menu with:
<p id="lunch-description">Monday to Friday, from 13:00 to 16:00. Choose a starter, a main, a dessert and a drink.</p>
<table aria-describedby="lunch-description">
<caption>Lunch menu: dishes and price</caption>
<thead>
<tr>
<th id="lunch-dish">Dish</th>
<th id="lunch-options">Options</th>
<th id="lunch-price">Price</th>
</tr>
</thead>
<tbody>
<tr>
<th id="lunch-starter" headers="lunch-dish" rowspan="2">Starter</th>
<td headers="lunch-starter lunch-options">Pumpkin soup</td>
<td headers="lunch-price" rowspan="6">€9.50</td>
</tr>
<tr>
<td headers="lunch-starter lunch-options">Seasonal salad</td>
</tr>
<tr>
<th id="lunch-main" headers="lunch-dish" rowspan="2">Main</th>
<td headers="lunch-main lunch-options">Vegetable quiche</td>
</tr>
<tr>
<td headers="lunch-main lunch-options">Sourdough sandwich with cheese and tomato</td>
</tr>
<tr>
<th id="lunch-dessert" headers="lunch-dish">Dessert</th>
<td headers="lunch-dessert lunch-options">Lemon sponge cake or seasonal fruit</td>
</tr>
<tr>
<th id="lunch-drink" headers="lunch-dish">Drink</th>
<td headers="lunch-drink lunch-options">Water, soft drink or coffee</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">Includes sourdough bread. Price includes <abbr title="Value Added Tax">VAT</abbr>. Ask about the allergens in each dish.</td>
</tr>
</tfoot>
</table>- All the
ids start withlunch-, and none of them matches any otheridinmenu.html. - Each option declares its dish and the "Options" column; each dish declares the "Dish" column; the price declares the "Price" column.
scopeisn't used here: when a table usesheaders, they are what count. Mixing both systems in the same table only adds confusion.- The paragraph with the menu's times is still visible to everyone and is now also linked to the table.
- The opening hours in
index.html
index.htmlAdd the caption and the scope attributes to the opening hours table (the tbody shows the first row; the other services follow the same pattern):
<h3>Weekly opening hours by service</h3>
<table>
<caption>Opening hours for each service, Monday to Sunday</caption>
<colgroup></colgroup>
<colgroup span="5"></colgroup>
<colgroup span="2"></colgroup>
<thead>
<tr>
<th rowspan="2" scope="col">Service</th>
<th colspan="5" scope="colgroup">Monday to Friday</th>
<th colspan="2" scope="colgroup">Weekend</th>
</tr>
<tr>
<th scope="col">Monday</th>
<th scope="col">Tuesday</th>
<th scope="col">Wednesday</th>
<th scope="col">Thursday</th>
<th scope="col">Friday</th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Counter</th>
<td colspan="7">8:00–20:00</td>
</tr>
<!-- Bread sales, Lunch menu and Bread-making workshop:
th scope="row" on the service name. -->
</tbody>
…
</table>- The
captionstays true to theh3without repeating it word for word. - "Monday to Friday" and "Weekend" use
scope="colgroup", relying on the threecolgroupelements you created in Table Head, Body and Foot. At last they have a job to do. - The merged data cells ("8:00–20:00" across seven columns) rely on the group headers: even if a screen reader announces only "Monday", the "Monday to Friday" or "Weekend" group completes the information. This table has headers only in the first column and in the head, so
scopeis enough; it doesn't needheaders.
- Checking
Open menu.html with Live Server, select the "€9.50" cell in the Elements panel and look at the Accessibility tab: the table's name should be the text of the caption. If you can, turn on NVDA or VoiceOver and go down the menu's price column and along the workshop row in the opening hours.
Common Mistakes and Tips
- Writing
captionanywhere other than as the first child oftable: it must come beforecolgroupandthead. - Using an empty
thin the corner: if it has no text, use an emptytdor give it a real title ("Service", "Product"). - Putting
scopeon atd: it isn't valid. If that cell is a header, turn it into ath. - Using
scope="colgroup"withoutcolgroupelements: the browser doesn't know which columns make up the group. headerswith commas (headers="a, b"): theids are separated by spaces only.- Repeated
ids across tables on the same page: they break theheadersreferences (and anchor links). Use a prefix for each table. - Writing
headersin every table just in case: in simple tables,scopeis enough and much easier to maintain. - Using the obsolete
summaryattribute: replace it withcaptionand, if necessary, a descriptive paragraph. - Tip: the best accessible table is a simple table. Before adding attributes, ask yourself whether you can simplify the structure.
Exercises
Exercise 1. Add a caption and the appropriate scope attributes to this table of upcoming workshops:
<table>
<thead>
<tr>
<th>Workshop</th>
<th>Date</th>
<th>Places left</th>
</tr>
</thead>
<tbody>
<tr>
<th>Sourdough bread</th>
<td>Saturday 6 December</td>
<td>4</td>
</tr>
<tr>
<th>Christmas pastries</th>
<td>Saturday 3 January</td>
<td>12</td>
</tr>
</tbody>
</table>Exercise 2. Say what's wrong with each fragment and fix it:
<!-- A -->
<table>
<thead>…</thead>
<caption>Single-origin coffees</caption>
…
</table>
<!-- B -->
<td scope="row">Colombia</td>
<!-- C -->
<th id="price">Price</th>
…
<td headers="product-ethiopia, price">€8.20</td>Exercise 3. On the lunch menu, The Blue Cup's management wants to add a "Vegan option" row to the mains: "Bean burger". Write the new row with its headers and say which other attributes in the table you need to change.
Solutions
Solution 1.
<table>
<caption>Upcoming workshops: dates and places left</caption>
<thead>
<tr>
<th scope="col">Workshop</th>
<th scope="col">Date</th>
<th scope="col">Places left</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Sourdough bread</th>
<td>Saturday 6 December</td>
<td>4</td>
</tr>
<tr>
<th scope="row">Christmas pastries</th>
<td>Saturday 3 January</td>
<td>12</td>
</tr>
</tbody>
</table>The screen reader will announce, for example, "Christmas pastries, Places left, 12".
Solution 2.
- A:
captionmust be the first child oftable, beforethead. The fix: move it to straight after<table>. - B:
scopeisn't valid on atd. If "Colombia" heads its row, it must be<th scope="row">Colombia</th>. - C: the
ids inheadersare separated by spaces, not commas:headers="product-ethiopia price". It's also a good idea forpriceto have a prefix specific to the table (for example,beans-price) so that it doesn't clash with otherids on the page.
Solution 3. The new row goes after the sandwich row:
You also need to change two rowspan values: the one on "Main" goes from 2 to 3 (it now has three options) and the one on the price goes from 6 to 7 (the body has one more row). If you forget the second one, "€9.50" would no longer cover the drink row and a gap would appear in the price column.
Conclusion
You now understand how someone using a screen reader moves through a table and what they need from the code: a title with caption as the table's first child, th headers with their scope (col, row, colgroup or rowgroup) to indicate which cells they apply to and, in complex tables with merged headers, the explicit relationship between id and headers. You know how to avoid nested tables, how to add a description with aria-describedby in passing and how to check the result in the accessibility tree. The Blue Cup's menu, lunch menu and opening hours are now accessible tables. What they aren't yet is attractive: they still have no borders, minimal padding and left-aligned prices. In the next lesson, Styling Tables, you will round off the module by giving them a professional look with CSS, and you will see why the old presentational attributes no longer have a place in your code.
HTML Course
Module 1: Introduction to HTML
- What Is HTML?
- Setting Up Your Work Environment
- Basic Structure of an HTML Document
- Tags, Elements and Attributes
- Creating Your First HTML Page
- File Organisation and Paths
Module 2: Text Formatting in HTML
- Headings and Paragraphs
- Text Formatting Tags
- Lists: Ordered, Unordered and Description Lists
- Block Quotes and Preformatted Text
- Block and Inline Elements: div and span
Module 3: Links and Media in HTML
- Creating Hyperlinks
- Special Links: Anchors, Email, Phone and Downloads
- Adding Images
- Embedding Videos
- Using the Audio Element
Module 4: Tables in HTML
- Basic Table Structure
- Table Head, Body and Foot
- Merging Cells: colspan and rowspan
- Accessible Tables: caption, scope and headers
- Styling Tables
Module 5: Forms in HTML
- Creating a Basic Form
- Form Elements: input, textarea and select
- HTML5 Input Types
- Labels and Grouping: label, fieldset, legend and datalist
- Form Attributes and Validation
- Submitting Forms
Module 6: HTML5 Semantic Elements
- Introduction to Semantic HTML
- header, nav and footer
- article, section and aside
- main and figure
- Other Semantic Elements: time, address, details and dialog
Module 7: Advanced HTML Techniques
- HTML Entities
- Using iframes
- Data Attributes
- Metadata: meta, SEO and Open Graph
- Microdata and Structured Data
- Graphics in HTML: SVG and canvas
Module 8: Integrating HTML and CSS
- Linking CSS to HTML
- Inline, Internal and External CSS
- Basic CSS Selectors
- The Box Model
- Styling HTML Elements
- Introduction to Layout with Flexbox and Grid
Module 9: Responsive Web Design
Module 10: Best Practices and Accessibility
- HTML Best Practices
- Validating HTML Code
- Web Accessibility Basics
- Using ARIA Roles and Attributes
- Accessibility Testing
- Performance and Resource Loading
