Open the league table in a football app. Your eye goes down to your team's row, then across to the Points column. You don't need to read the rest. You only find the place where that row and that column meet.
A table is data laid out in rows and columns. Every cell (one box of data) sits where one row heading meets one column heading, like your team's Points number. If Mrs. Sri draws lines in her guest book for the columns Guest, Room, Check-in, Nights and Total, the book becomes a table too. To find what Joko pays, your eye goes down to Joko's row, then across to the Total column. An HTML table works exactly the same way. A screen reader can also read a table by row and column, as long as you tell it which cells are headings.
Tables for data people compare
In the Density & Scale lesson, Mrs. Sri's dashboard showed 16 bookings as a list. Now Mrs. Sri wants to compare: who stays longest, and which booking is largest. When every row has the same fields (guest, room, date, nights, total), and people compare those rows, use a table. When each item stands on its own, like the three room cards for guests with a photo and a booking button, use cards or a list.
Mrs. Sri's bookings table
This is Mrs. Sri's bookings table. Each <tr> is one row, <th> is a heading cell, and <td> is a data cell:
<table>
<caption id="cap">December bookings (16)</caption>
<thead>
<tr>
<th scope="col">Guest</th>
<th scope="col">Room</th>
<th scope="col">Check-in</th>
<th scope="col" class="num">Nights</th>
<th scope="col" class="num">Total</th>
</tr>
</thead>
<tbody>
<tr><th scope="row">Rina</th><td>Terrace</td><td>1 Dec</td><td class="num">2</td><td class="num">$70.00</td></tr>
<tr><th scope="row">Mr. Hadi</th><td>Garden</td><td>2 Dec</td><td class="num">1</td><td class="num">$42.50</td></tr>
<!-- 14 more bookings -->
</tbody>
</table>
<style>
table { border-collapse: collapse; width: 100%; }
caption { text-align: left; font-weight: 700; padding-bottom: 8px; }
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid #e2e8f0; white-space: nowrap; }
thead th { border-bottom: 2px solid #e2e8f0; color: #475569; font-size: 14px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
</style>
<caption>is the table's name. A screen reader says it on entering the table: "December bookings (16), table, 5 columns".<thead>wraps the heading row, and<tbody>wraps the data rows. Both sit inside<table>.<th scope="col">means "I'm the heading for the cells below me".<th scope="row">on the guest's name means "I'm the heading for the cells beside me". When it stops on "$70.00", a screen reader also says "Rina" and "Total". So people know whose number it is, and what the number is.border-collapse: collapsemerges the borders of neighbouring cells into one line. Without it, every cell has its own border, with a 2px gap between them (the browser's defaultborder-spacing).white-space: nowrapstops "Mr. Hadi" and "1 Dec" from breaking onto two lines of text.
Numbers right-aligned, every digit the same width
Text is read from the left, but numbers are compared from the right: units under units, thousands under thousands. If the Total column is left-aligned, "$27.50" and "$127.50" start at the same point, and the eye has to count digits to see which is in the hundreds. On Mrs. Sri's dashboard, when the Total column is left-aligned, the right ends of its numbers are up to 7.1px apart. Right-aligning lines up the last digits, so a longer number clearly looks bigger. That is why numbers in a table are aligned to the right. Text stays left-aligned.
One more thing: in many fonts the digits aren't the same width. We measured the Mac system font (San Francisco, 16px) in Chrome, Safari and Firefox: "1111111" is 49.77px wide, and "0000000" is 68.36px. So a "1" is only 7.11px, and a "0" is 9.77px. With font-variant-numeric: tabular-nums, every digit becomes 9.77px. In the Total column, right-aligning alone lines up the last digit. But without tabular-nums, the left edges of "$70.00", "$42.50" and the other four-digit totals still drift by up to 2.7px, because the digits aren't the same width. With tabular-nums, every digit position (units, tens, hundreds and so on) lines up to the pixel.
Sorting with aria-sort
Mrs. Sri wants to know the largest booking. Each column heading gets a button for sorting. The column currently used for sorting gets aria-sort, an attribute that tells a screen reader the sort direction of that column:
<th scope="col" class="num" aria-sort="descending">
<button type="button">Total</button>
</th>
Then this JavaScript sorts the rows, from the largest total:
const tbody = document.querySelector('tbody');
const total = (row) => Number(row.cells[4].textContent.replace(/\D/g, ''));
const rows = [...tbody.rows].sort((a, b) => total(b) - total(a)); // largest first
tbody.append(...rows); // move the rows into the new order
aria-sortgoes on the<th>, not on its button, and only on the one column doing the sorting. Its value isascending(small to large) ordescending(large to small). The other columns get noaria-sortat all.- The
<button>makes the column heading reachable with Tab and pressable with Enter. row.cells[4]is the fifth cell in the row, Total (counting starts at 0).replace(/\D/g, '')drops everything that isn't a digit, so "$127.50" becomes 12750, the total in cents. That is why every total is written with its cents.- The result: Joko and Eko ($127.50) on top, then Maya and Wulan ($85.00). Joko is above Eko because JavaScript's
sortis stable: rows with equal values keep their original order.
The table on a 375px screen
The table of 16 bookings is 408px wide. If Mrs. Sri opens it on a phone 375px wide, with no extra CSS at all, what happens? Is the rightmost column cut off, does text wrap onto new lines, or can the whole page be dragged sideways? Guess first.
We opened this table on a 375px phone in Chrome, Safari and Firefox. The table doesn't shrink below 408px, because every cell is nowrap. The page becomes 424px wide (408 + 16px padding). So the whole page can be dragged sideways by 49px: the heading and everything else move with it. Nothing is cut off, and no text wraps. That's the answer to the guess.
Two patterns fix it:
<div class="scroll" role="region" aria-labelledby="cap" tabindex="0">
<table> ... </table>
</div>
<style>
.scroll { overflow-x: auto; } /* pattern 1: a scroll box */
@media (max-width: 480px) { .opt { display: none; } } /* pattern 2: fewer columns */
</style>
- Scroll box: the page is back to 375px wide, and only the table scrolls, inside its own box (
overflow-x: autogives that box a sideways scroll).tabindex="0"lets people reach the box with Tab, then scroll it with the arrow keys.role="region"(a mark that says the box is one part of the page) witharia-labelledby="cap"gives the box a name, taken from the caption. WCAG 1.4.10 (Reflow) explicitly exempts data tables from its ban on sideways scrolling, because a row has to be read whole. - Fewer columns: the Room and Nights columns get
class="opt", and are hidden (display: none) on screens 480px wide or narrower. The table becomes 343px wide and fits the screen, but Mrs. Sri loses two pieces of information. Use this only when those columns are rarely needed on a phone.
⚠️ What makes a table hard to read
- A
<th>withoutscope, or a bold<td>as a heading. It looks like a heading, but a screen reader doesn't know which cells it heads. - Left-aligned numbers. Tens and hundreds don't line up, and the eye has to count digits. Align them right, and add
tabular-nums. aria-sorton every column, or on the<button>. One is enough, on the<th>of the column doing the sorting.overflow: hiddenon the table's wrapper. The right-hand columns are cut off and can't be reached at all. Useoverflow-x: auto.- A table for layout. If the content has no sensible row and column headings, it isn't a table. Use grid or flex.
TL;DR: Use a table (data in rows and columns) for data people compare across rows. <caption> names the table, and <thead> and <tbody> separate the heading row from the data rows. <th scope="col"> heads a column, and <th scope="row"> heads a row. With these, a screen reader also says a cell's row and column headings, for example "Rina" and "Total" for "$70.00". table { border-collapse: collapse; } merges the borders of neighbouring cells. Numbers are right-aligned, with font-variant-numeric: tabular-nums so every digit has the same width (without it, in the Mac font a "1" is 7.11px and a "0" 9.77px). The column doing the sorting gets aria-sort="ascending" or "descending" on its <th>, so a screen reader knows the sort direction, with a <button> inside that <th>. On a 375px phone, a 408px table makes the whole page shift by 49px. Wrap the table in a scroll box, <div class="scroll" role="region" aria-labelledby="cap" tabindex="0"> with overflow-x: auto, so only the table scrolls. Or hide the columns that are rarely used.
⚡ Tables for data people compare
Write:
<table>
<caption id="cap">December bookings (16)</caption>
<thead>
<tr>
<th scope="col">Guest</th>
<th scope="col" class="num">Total</th>
</tr>
</thead>
<tbody>
<tr><th scope="row">Rina</th><td class="num">$70.00</td></tr>
</tbody>
</table>
Guess first: which attribute tells a screen reader that a th heads a column?
scope="col", meaning "I'm the heading for the cells below me". scope="row" on the guest's name means it heads the cells beside it. So on "$70.00", a screen reader also says "Rina" and "Total".
Meaning: A table lays data out in rows and columns, for data people compare across rows. <th> is a heading cell, and <td> a data cell. <caption> names the table, and <thead> and <tbody> separate the heading row from the data rows. In the CSS, border-collapse: collapse merges the borders of neighbouring cells.
⚡ Numbers align right
Write:
.num { text-align: right; font-variant-numeric: tabular-nums; }
Meaning: Text is read from the left, but numbers are compared from the right: units under units, thousands under thousands. That is why number cells (class .num) are aligned right. tabular-nums makes every digit the same width, because in the Mac font a "1" is only 7.11px and a "0" 9.77px.
⚡ A wide table on a phone
Meaning: A table 408px wide on a phone 375px wide lets the whole page be dragged sideways. Wrap the table in <div class="scroll" role="region" aria-labelledby="cap" tabindex="0"> and give it overflow-x: auto, so only the table scrolls. tabindex="0" lets people reach that box with Tab. role="region" marks it as one part of the page, and aria-labelledby="cap" gives it a name from the table's caption.