Table
A grid of facts people compare: a caption, sortable columns that say how they are sorted, and rows that turn into cards on a phone.
Try it
Press a column name to sort by it, and again to flip the order. Switch to Phone width to see each row become a card.
| Status | |||
|---|---|---|---|
| Copper Kettle Cafe | Starter | Paused | $19 |
| Harbor Bakery | Studio | Active | $49 |
| Juniper Florals | Partner | Active | $129 |
| Linden Yoga | Starter | Trial | $19 |
| Maple Dental | Studio | Active | $49 |
| Northwind Books | Partner | Past due | $129 |
Made-up businesses, for the demo.
Code
Plain HTML for any site, or React. Both use the same styles, so the table looks the same wherever it lands.
ReadyAny website, Shopify, WordPress, Square, plain hosting
<link rel="stylesheet" href="tokens.css">
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="parts.css">
<!-- Table: a caption names it. The first cell of each row is its header.
prism-table--stack turns rows into cards on narrow widths; the roles keep it a table for screen readers. -->
<div class="prism-table-wrap" role="region" aria-labelledby="clients-cap" tabindex="0">
<table class="prism-table prism-table--stack" role="table">
<caption id="clients-cap">Clients</caption>
<thead role="rowgroup">
<tr role="row">
<th scope="col" role="columnheader" aria-sort="ascending">
<button type="button" class="prism-table__sort">Client<svg class="prism-table__arrow" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.8" aria-hidden="true"><path d="M8 12V4M4.5 7.5L8 4l3.5 3.5" stroke-linecap="round" stroke-linejoin="round"/></svg></button>
</th>
<th scope="col" role="columnheader">Plan</th>
<th scope="col" role="columnheader">Status</th>
<th scope="col" role="columnheader" class="prism-table__num">Monthly</th>
</tr>
</thead>
<tbody role="rowgroup">
<tr role="row">
<th scope="row" role="rowheader">Harbor Bakery</th>
<td role="cell" data-label="Plan">Studio</td>
<td role="cell" data-label="Status"><span class="prism-badge prism-badge--success">Active</span></td>
<td role="cell" data-label="Monthly" class="prism-table__num">$49</td>
</tr>
<!-- more rows -->
</tbody>
</table>
</div>
<!-- Sorting: when a header button is pressed, re-order the rows, and move aria-sort
("ascending" or "descending") to that header. Only one header has aria-sort at a time. -->Accessibility
- A real table with a caption, column headers, and a header cell for each row, so screen readers can say where you are.
- Sorting uses buttons in the headers. The sorted column says ascending or descending, and the caption says it too.
- Numbers line up on the right with even widths, so they are easy to compare.
- On narrow widths each row becomes a card showing every column name. The roles are written out, so it stays a table for screen readers.
- If a wide table must scroll sideways, the scroll area is named and can be reached by keyboard.
- Status is a badge with words, never a colored dot alone.