Table <table>
Tables display data in rows and columns, so it is easy to scan and compare. Commonly used for lists of records, reports, and settings.
<table>A stylesheet, no JavaScript. The sort state it shows is written in the markup, with aria-sort. The same markup works in a server-rendered page and in a framework template.
Options
Basic
Add class="l-table" to a <table>. Name it with a <caption>, head each column with <th scope="col"> and each row with the <th scope="row"> that identifies it. Add data-align="end" to the header and cells of a column of figures; digits are tabular across the table, so they line up. The header sticks to the top of its scroll container. Totals go in a <tfoot>, set apart by a stronger rule; several <tbody> elements group rows.
| SKU | Name | In stock |
|---|---|---|
| SKU-1042 | Desk lamp | 4,210 |
| SKU-2087 | Office chair | 12,004 |
Code
<table class="l-table">
<caption>
Products
</caption>
<thead>
<tr>
<th scope="col">SKU</th>
<th scope="col">Name</th>
<th
scope="col"
data-align="end"
>
In stock
</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">SKU-1042</th>
<td>Desk lamp</td>
<td data-align="end">4,210</td>
</tr>
<tr>
<th scope="row">SKU-2087</th>
<td>Office chair</td>
<td data-align="end">12,004</td>
</tr>
</tbody>
</table>Sizes
Add data-size="sm" or data-size="lg". Rows follow the control size scale, so an .l-button of the same data-size fits a row. A row is a minimum height: content taller than it grows the row.
| SKU | Name | In stock |
|---|---|---|
| SKU-1042 | Desk lamp | 4,210 |
| SKU-2087 | Office chair | 12,004 |
| SKU | Name | In stock |
|---|---|---|
| SKU-1042 | Desk lamp | 4,210 |
| SKU-2087 | Office chair | 12,004 |
Code
<div class="flex w-full flex-col gap-6">
<table
class="l-table"
data-size="sm"
>
<caption>
Products
</caption>
<thead>
<tr>
<th scope="col">SKU</th>
<th scope="col">Name</th>
<th
scope="col"
data-align="end"
>
In stock
</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">SKU-1042</th>
<td>Desk lamp</td>
<td data-align="end">4,210</td>
</tr>
<tr>
<th scope="row">SKU-2087</th>
<td>Office chair</td>
<td data-align="end">12,004</td>
</tr>
</tbody>
</table>
<table
class="l-table"
data-size="lg"
>
<caption>
Products
</caption>
<thead>
<tr>
<th scope="col">SKU</th>
<th scope="col">Name</th>
<th
scope="col"
data-align="end"
>
In stock
</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">SKU-1042</th>
<td>Desk lamp</td>
<td data-align="end">4,210</td>
</tr>
<tr>
<th scope="row">SKU-2087</th>
<td>Office chair</td>
<td data-align="end">12,004</td>
</tr>
</tbody>
</table>
</div>Sortable columns
Put a <button> in each sortable header, and aria-sort="ascending" or "descending" on the sorted one. The stylesheet shows the state written in the markup; it does not reorder rows. When the server sorts, use a link to the sorted URL instead of the button (<a href="?sort=-sku">). Disable the button of a column the current query cannot sort by. Any other action in a header is an .l-button and keeps its own look.
| SKU-2087 | Office chair | 12,004 |
|---|---|---|
| SKU-1042 | Desk lamp | 4,210 |
| SKU-3110 | Standing desk | 865 |
Code
<table class="l-table">
<caption>
Products
</caption>
<thead>
<tr>
<th scope="col"><button type="button">SKU</button></th>
<th scope="col"><button type="button">Name</button></th>
<th
scope="col"
data-align="end"
aria-sort="descending"
>
<button type="button">In stock</button>
</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">SKU-2087</th>
<td>Office chair</td>
<td data-align="end">12,004</td>
</tr>
<tr>
<th scope="row">SKU-1042</th>
<td>Desk lamp</td>
<td data-align="end">4,210</td>
</tr>
<tr>
<th scope="row">SKU-3110</th>
<td>Standing desk</td>
<td data-align="end">865</td>
</tr>
</tbody>
</table>Accessibility
Criteria
Your part 4
Check these in your markupMark up column and row headers
Use
<th scope="col">for column headers and<th scope="row">for the cell that identifies each rowA screen reader then announces the column and the record with every cell
Name the table
Add a
<caption>(visible or.l-visually-hidden) or anaria-labelSet
aria-sorton the sorted<th>onlyIt is announced with the header
Sort with a native
<button>Put it inside the header, or use a link when the server sorts
As in the APG sortable table example
Built in 2
Handled by the elementUnsorted glyph contrast passes
The direction glyph of an unsorted column uses the tertiary text color, above 3:1 against the surface in both schemes
Focus ring on header buttons
Header buttons show the Luxen focus ring
Keyboard interactions
API reference
Importing
@import 'luxen-ui/css/table';Attributes & Properties
aria-sortascending | descendingAttribute- On the sorted column's
<th>, which holds a<button>(or a link, when the server sorts): shows the direction. A disabled button marks a column the current query cannot sort by; an.l-buttonin a header is another action and keeps its own look. data-alignstart | center | endAttribute- On a column's
<th>and<td>s: aligns the column. Useendfor figures; digits are tabular across the table, so they line up. A sort button in anendheader puts its glyph before the label, keeping the label flush with the figures. data-sizesm | lgAttribute- Row height, on the control size scale: a control of the same size fits a row. Defaults to the medium size.
CSS classes
.l-tableClass- Applied to the
<table>. Name it with a<caption>(visible or.l-visually-hidden) oraria-label; head the columns with<th scope="col">and each row with the<th scope="row">that identifies it.
CSS custom properties
--background-colordefault:var(--l-color-surface)Custom property- Background of the table. The sticky header repeats it, so it stays opaque over the rows scrolling under it.
--border-colordefault:var(--l-color-divider)Custom property- Rules between rows. The stronger rules under the header and above the totals use
--l-color-border. --row-heightdefault:calc(var(--l-size-control-md) + 2 * var(--l-spacing-1))Custom property- Minimum row height; content taller than it grows the row.