Display
Data tables
Tables people compare numbers in — alignment, units, empty cells, sorting, scrolling on narrow screens.
Example
The markup is shadcn's Table as is. Everything below is about what goes into the cells and headers.
| ml-inference | 3.5 | 64.0 | 22.1 | 4,890.00 | +24.7 |
|---|---|---|---|---|---|
| analytics-pipeline | 0.4 | 2,310.5 | 12.9 | 3,102.60 | +7.9 |
| image-resizer | 6.1 | 940.2 | 1,204.8 | 2,318.77 | −3.5 |
| search-index | 9.4 | 186.0 | 41.7 | 1,630.90 | +8.8 |
| api-gateway | 48.2 | 12.4 | 310.5 | 1,284.40 | +7.9 |
| web-frontend | 31.7 | 4.1 | 892.3 | 942.18 | −6.9 |
| backups | 0.1 | 5,120.0 | 0.0 | 512.00 | +2.8 |
| auth-service | 22.9 | 0.8 | 18.2 | 211.05 | +2.8 |
| staging | 4.6 | 0.0 | 7.3 | 96.48 | −26.9 |
| email-sender | 2.8 | No value | 9.4 | 74.12 | −8.0 |
| billing-worker | 1.2 | 3.3 | 0.6 | 58.30 | 0.0 |
| docs-site | 1.9 | 0.6 | 54.2 | 23.70 | No value |
| Total | 132.8 | 8,641.9 | 2,574.0 | 15,144.50 | +9.2 |
Numbers
- Right-align numbers, left-align text. Right alignment puts ones under
ones and thousands under thousands, so magnitude is visible before you
read a digit. The GOV.UK table does it with
--numericmodifier classes on both<th>and<td>. - The header follows its column. A right-aligned column gets a right-aligned header. In the demo the sort icon sits on the left of numeric headers so the label's right edge lines up with the numbers.
- Same precision for the whole column.
12.4above940.2above5,120.0— mixing5120and0.8breaks decimal alignment, and CSS can't align on the decimal point (text-align: "."was specified and never shipped). SetminimumFractionDigitsandmaximumFractionDigitsto the same value per column. - Tabular figures. With proportional digits
1111is narrower than8888and right-aligned columns drift.tabular-numsfixes it — see Tabular figures. - Units in the header, not in every cell. "Storage, GB" once instead of "GB" twelve times. If a column mixes units (KB, MB, GB), it can't be compared at a glance — pick one unit per column.
- Signed deltas use a true minus (
−, U+2212).Intl.NumberFormatemits a hyphen, which is narrower and doesn't line up with+. UsesignDisplay: "exceptZero"so0.0gets no sign. - Color is not the sign. If you tint deltas, keep the
+/−. And whether up is good depends on the column: cost going up is bad.
Empty cells
Three different things, three different renderings:
0.0— measured, and it's zero.staginghas storage, it's empty.—— no value: not applicable or not available.email-senderhas no storage at all;docs-siteis new and has no previous month to compare. Screen readers announce—as "em dash" or skip it, so add visually hidden text ("No value").- Blank — reads as "still loading" or "broken". Avoid it.
Sorting puts no-value rows last in both directions — they aren't smaller than zero.
Sorting
- A
<button>inside the<th>, not a click handler on the cell — that's what makes it focusable and announced as a control (APG sortable table). aria-sorton the sorted column only, set toascendingordescending. Remove it from other headers rather than settingnone(Roselli).- Show the state in the header. An arrow on the sorted column and a faint up/down glyph on the others, so it's visible which columns sort at all.
- First click on a numeric column sorts largest first. The question is usually "what costs the most?". Text columns start A→Z.
- Stable tie-breaker. Equal values fall back to the name, so rows don't shuffle between renders.
- Text sorts naturally:
localeComparewith{ numeric: true }putsnode-2beforenode-10. - Totals stay in
<tfoot>and are never sorted with the data.
Scrolling
- The scroll container is focusable.
tabIndex={0}lets keyboard users scroll it with arrow keys;role="region"plusaria-labelledbypointing at the<caption>gives it a name (Roselli). shadcn'sTablerenders that container for you; the component here takescontainerPropsto reach it. - One scroll container for both axes. A sticky header needs the
vertically scrolling element to be its nearest scroll container. Nest an
overflow-x-autodiv inside anoverflow-y-autoone andposition: sticky; top: 0stops working. - Sticky header, first column and totals. The row label and the column label stay visible wherever you scroll — otherwise the number you're looking at has no meaning.
- Show that there's more. A fade on the right edge while more columns are hidden; a shadow on the sticky column once content scrolls under it.
- Borders under
position: sticky. Withborder-collapse: collapse(the Tailwind default) the header's bottom border stays behind and scrolls away, and Chrome doesn't paintbox-shadowon collapsed cells either. Draw the line and the column shadow with::before/::afteron the sticky cells. - Sticky cells need an opaque background, including the hover color, or the scrolled content shows through.
Narrow screens
Horizontal scroll with a sticky first column keeps the table a table: you can still compare a column top to bottom. Turning each row into a card reads well for one record at a time but loses exactly that — comparing "Cost" across projects means reading every card. Use cards when people look up one row (a contact list), scrolling when they compare (anything with a total).
Rows
- Row lines or zebra stripes — either works. In Jessica Enders' studies striping never hurt and sometimes helped accuracy; the first, smaller study found no consistent difference. shadcn's default is row lines with a hover highlight, which also helps follow a wide row.
- Start with no vertical borders. Whitespace separates columns; Butterick suggests turning all borders off and adding back only what's needed.
Accessibility
<caption>names the table like a heading names a section (GOV.UK, WAI). It's also the name of the scroll region.<th scope="col">for column headers,<th scope="row">for the row label — the project name here. Screen readers then announce "api-gateway, Cost, 1,284.40" instead of a bare number.- Real table elements. A
<div>grid styled to look like a table has no table navigation for screen readers.
Further reading
- GOV.UK Design System — Table
- W3C WAI — Tables tutorial
- W3C APG — Sortable table example
- Adrian Roselli — Sortable Table Columns
- Adrian Roselli — Under-Engineered Responsive Tables
- Jessica Enders — Zebra Striping: Does it Really Help?
- Jessica Enders — Zebra Striping: More Data for the Case
- Butterick's Practical Typography — Tables and Grids of numbers