UI Patterns
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.

Cloud usage by project, November
ml-inference3.564.022.14,890.00+24.7
analytics-pipeline0.42,310.512.93,102.60+7.9
image-resizer6.1940.21,204.82,318.77−3.5
search-index9.4186.041.71,630.90+8.8
api-gateway48.212.4310.51,284.40+7.9
web-frontend31.74.1892.3942.18−6.9
backups0.15,120.00.0512.00+2.8
auth-service22.90.818.2211.05+2.8
staging4.60.07.396.48−26.9
email-sender2.8No value9.474.12−8.0
billing-worker1.23.30.658.300.0
docs-site1.90.654.223.70No value
Total132.88,641.92,574.015,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 --numeric modifier 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.4 above 940.2 above 5,120.0 — mixing 5120 and 0.8 breaks decimal alignment, and CSS can't align on the decimal point (text-align: "." was specified and never shipped). Set minimumFractionDigits and maximumFractionDigits to the same value per column.
  • Tabular figures. With proportional digits 1111 is narrower than 8888 and right-aligned columns drift. tabular-nums fixes 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.NumberFormat emits a hyphen, which is narrower and doesn't line up with +. Use signDisplay: "exceptZero" so 0.0 gets 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. staging has storage, it's empty.
  • — — no value: not applicable or not available. email-sender has no storage at all; docs-site is 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-sort on the sorted column only, set to ascending or descending. Remove it from other headers rather than setting none (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: localeCompare with { numeric: true } puts node-2 before node-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" plus aria-labelledby pointing at the <caption> gives it a name (Roselli). shadcn's Table renders that container for you; the component here takes containerProps to 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-auto div inside an overflow-y-auto one and position: sticky; top: 0 stops 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. With border-collapse: collapse (the Tailwind default) the header's bottom border stays behind and scrolls away, and Chrome doesn't paint box-shadow on collapsed cells either. Draw the line and the column shadow with ::before / ::after on 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

On this page