More HTML answers

by Danny
Published 3 min read

Give data tables a caption, proper th cells with scope, and wrap them in a scrollable region on small screens. Here is the markup and CSS, plus what to avoid.

The short answer

#

Use real table markup: a caption, thead and tbody, and th cells with a scope. For small screens, wrap the table in a container that scrolls horizontally, and make that container focusable and labelled so keyboard users can scroll it too.


_24
<div class="table-scroll" role="region" aria-labelledby="plans-caption" tabindex="0">
_24
<table>
_24
<caption id="plans-caption">Plan comparison</caption>
_24
<thead>
_24
<tr>
_24
<th scope="col">Plan</th>
_24
<th scope="col">Price</th>
_24
<th scope="col">Users</th>
_24
</tr>
_24
</thead>
_24
<tbody>
_24
<tr>
_24
<th scope="row">Starter</th>
_24
<td>£5</td>
_24
<td>1</td>
_24
</tr>
_24
<tr>
_24
<th scope="row">Team</th>
_24
<td>£20</td>
_24
<td>10</td>
_24
</tr>
_24
</tbody>
_24
</table>
_24
</div>


_10
.table-scroll {
_10
overflow-x: auto;
_10
}

Why it happens

#

Screen readers can move through a table cell by cell and announce the matching row and column headers as they go, for example "Team, Price, £20". That only works if the headers are marked up as th and the browser can work out which cells they belong to. A grid of div elements, or a table where every cell is a td with bold styling, gives none of that information.

Tables also do not shrink well. Their minimum width is the sum of their columns' minimum content, so on a phone a wide table either overflows the page and causes horizontal scrolling of the whole layout, or gets squashed until it is unreadable.

How to fix it

#

Add a caption

The caption element must be the first child of the table. It names the table, and screen readers announce it when the user enters the table. If the design has no visible title, you can visually hide the caption, but keep it in the markup.

Mark up headers with th and scope

Use th for every header cell. Add scope="col" to column headers and scope="row" to row headers. Browsers can often guess simple cases, but scope removes the guesswork, especially when the first column is a header.

Group rows with thead, tbody and, if needed, tfoot. This also lets you style the header and footer without extra classes.

Handle complex tables carefully

For headers that span several columns, use colspan and scope="colgroup". For tables where a cell relates to headers that scope cannot express, give each th an id and list them in the cell's headers attribute.


_10
<th id="q1" scope="col">Q1</th>
_10
<th id="north" scope="row">North</th>
_10
<td headers="north q1">120</td>

If you find yourself doing this a lot, consider splitting the data into several simpler tables.

Make the scroll container usable

The overflow-x: auto wrapper keeps the table from breaking the page layout. Three attributes make it accessible:

  • tabindex="0" lets keyboard users focus the region and scroll it with the arrow keys.
  • role="region" turns it into a landmark, which a focusable element needs so it has a meaningful role.
  • aria-labelledby points at the caption so the region is announced by name.

Give the focused wrapper a visible outline, and consider a visual hint that more content is off to the side.


_10
.table-scroll:focus-visible {
_10
outline: 3px solid #2563eb;
_10
outline-offset: 2px;
_10
}
_10
_10
.table-scroll th[scope='row'] {
_10
position: sticky;
_10
left: 0;
_10
background: white;
_10
}

The sticky first column keeps row headers in view while the user scrolls sideways, which makes wide tables much easier to read.

Reflow into cards only if you must

A common pattern turns each row into a stacked card on small screens by changing the table elements to display: block. This can work, but in some browsers changing the display value of table elements removes their table semantics, so screen readers no longer treat it as a table. If you use this pattern, test with a screen reader, or restore the roles with role="table", role="row" and role="cell". Horizontal scrolling is simpler and safer for most data.

Common pitfalls

#
  • Using tables for layout. Tables are for data with rows and columns. Use CSS grid or flexbox for layout.
  • Using a heading above the table instead of a caption. A heading is fine for page structure, but the caption is what gets tied to the table.
  • Empty header cells. A blank top-left th is common; use a td there instead, or give it text.
  • Setting overflow on the table itself. Tables ignore overflow. Put it on a wrapper.
  • Hiding columns on mobile. Removing data with display: none on small screens means some users never get it. Prefer scrolling.
  • Relying on colour or zebra stripes. They help, but the header markup is what screen readers use.
#

Was this resource helpful?