Style a data table with HTML and CSS
33 A data table, put together
hardBuild a finished fares table: caption, column and row headers, striped body, right-aligned figures that line up, a totals row that is actually correct, and horizontal scrolling instead of a broken layout on a narrow screen.
Stuck? Three hints, smallest first
- 1. Numbers right-aligned, text left-aligned, and
font-variant-numeric: tabular-numsso the digits share a width. - 2. Wrap the table in a container with
overflow-x: auto, and give the containertabindex="0"so a keyboard can scroll it. - 3. A totals row belongs in
<tfoot>, and the total should be the sum of the rows above it.
<div class="tablewrap" tabindex="0" role="region" aria-labelledby="fares-cap">
<table class="fares">
<caption id="fares-cap">Fares and seats, week 12</caption>
<thead>
<tr>
<th scope="col">Route</th>
<th scope="col" class="num">Seats</th>
<th scope="col" class="num">Fare</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Abuja</th>
<td class="num">14</td>
<td class="num">₦18,000</td>
</tr>
<tr>
<th scope="row">Kano</th>
<td class="num">30</td>
<td class="num">₦22,000</td>
</tr>
<tr>
<th scope="row">Enugu</th>
<td class="num">18</td>
<td class="num">₦12,500</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td class="num">62</td>
<td class="num">₦52,500</td>
</tr>
</tfoot>
</table>
</div>
.tablewrap {
overflow-x: auto; /* scroll the table, do not break the page */
max-width: 320px;
border: 1px solid #c9cad1;
border-radius: 8px;
}
/* The wrapper is focusable so it can be scrolled by keyboard, so it needs a
focus ring of its own. */
.tablewrap:focus-visible {
outline: 2px solid #3055d8;
outline-offset: 2px;
}
.fares {
border-collapse: collapse;
width: 100%;
font-size: 0.88rem;
}
.fares caption {
padding: 8px 10px;
text-align: left;
color: #5c5c66;
font-weight: 600;
}
.fares th,
.fares td {
padding: 7px 10px;
border-bottom: 1px solid #e4e5e9;
text-align: left;
white-space: nowrap;
}
.fares thead th { background: #17171a; color: #ffffff; }
.fares tbody tr:nth-child(even) { background: #f2f3f5; }
/* Figures line up only if the digits are the same width. */
.num {
text-align: right;
font-variant-numeric: tabular-nums;
}
.fares tfoot th,
.fares tfoot td {
border-top: 2px solid #17171a;
border-bottom: 0;
font-weight: 700;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
// Add the body up and compare with what tfoot claims, rather than trusting it.
const body = html.slice(html.indexOf('<tbody>'), html.indexOf('</tbody>'));
const foot = html.slice(html.indexOf('<tfoot>'));
const naira = (s) => [...s.matchAll(/₦([\d,]+)/g)].map((m) => Number(m[1].replace(/,/g, '')));
const plain = (s) => [...s.matchAll(/<td class="num">(\d+)<\/td>/g)].map((m) => Number(m[1]));
const sum = (xs) => xs.reduce((a, b) => a + b, 0);
test('three data rows in the body', naira(body).length, 3);
test('the fares add up to 52,500', sum(naira(body)), 52500);
test('and the total row says exactly that', naira(foot), [52500]);
test('the seats add up to 62, which the foot also matches', [sum(plain(body)), plain(foot)[0]], [62, 62]);
test('every column and row header declares a scope', (html.match(/scope="(col|row)"/g) || []).length, 7);
test('figures are right aligned with tabular digits', /tabular-nums/.test(css) && /text-align:\s*right/.test(css), true);
test('the wrapper scrolls instead of breaking the layout', /overflow-x:\s*auto/.test(css), true);
test('and is focusable, so it has its own focus ring', /tabindex="0"/.test(html) && /\.tablewrap:focus-visible/.test(css), true);
Output
The interesting checks here are the arithmetic ones: a totals row is a claim, and a table whose footer disagrees with its body is worse than no footer. Two accessibility details go together at the end. A scroll container needs tabindex="0" or a keyboard user cannot reach the hidden columns at all — and once it is focusable it needs a visible focus ring, which is why both are asserted as one. role="region" with aria-labelledby pointing at the caption gives that scrollable area a name instead of announcing an anonymous group.