Skip to content
Showcase theme Default Brutalist Coral Inspired by Airbnb Midnight Inspired by Linear Editorial Inspired by Oura How theming works
Interface sounds: off

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.

HTML tag<table>
⏣Native
⬡Progressive
◇Plain
⬢Shadow-DOM
Native element

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.

Products
SKU Name In stock
SKU-1042 Desk lamp 4,210
SKU-2087 Office chair 12,004
Code
html
<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.

Products
SKU Name In stock
SKU-1042 Desk lamp 4,210
SKU-2087 Office chair 12,004
Products
SKU Name In stock
SKU-1042 Desk lamp 4,210
SKU-2087 Office chair 12,004
Code
html
<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.

Products
SKU-2087 Office chair 12,004
SKU-1042 Desk lamp 4,210
SKU-3110 Standing desk 865
Code
html
<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 markup
  • Mark up column and row headers

    Use <th scope="col"> for column headers and <th scope="row"> for the cell that identifies each row

    A screen reader then announces the column and the record with every cell

    WCAG1.3.1RGAA5.65.7
  • Name the table

    Add a <caption> (visible or .l-visually-hidden) or an aria-label

    WCAG1.3.1RGAA5.4
  • Set aria-sort on the sorted <th> only

    It is announced with the header

    WCAG4.1.2RGAA7.1
  • 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

    WCAG4.1.2RGAA7.1

Built in 2

Handled by the element
  • Unsorted glyph contrast passes

    The direction glyph of an unsorted column uses the tertiary text color, above 3:1 against the surface in both schemes

    WCAG1.4.11RGAA3.3
  • Focus ring on header buttons

    Header buttons show the Luxen focus ring

    WCAG2.4.7RGAA10.7

Keyboard interactions ​

Tab
Moves through the header controls, then the controls in each row
Enter
Activates the focused header button
Space
Activates the focused header button

API reference ​

Importing ​

css
@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-button in 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. Use end for figures; digits are tabular across the table, so they line up. A sort button in an end header 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) or aria-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.