ARIA table examples

Example 1

This example uses a range of roles to define table structure.

Animals found in different habitats

Habitat Mammal Bird Reptile
Forest Deer Owl Lizard
Ocean Dolphin Albatross Turtle
Desert Camel Falcon Gecko
Wetland Otter Heron Crocodile
HTML markup
<h3 id="aaa">Animals found in different habitats</h3>
<div role="table" aria-labelledby="aaa">
  <div role="rowgroup">
    <div role="row">
      <span role="columnheader">Habitat</span>
      <span role="columnheader">Mammal</span>
      <span role="columnheader">Bird</span>
      <span role="columnheader">Reptile</span>
    </div>
  </div>

  <div role="rowgroup">
    <div role="row">
      <span role="rowheader">Forest</span>
      <span role="cell">Deer</span>
      <span role="cell">Owl</span>
      <span role="cell">Lizard</span>
    </div>
    <div role="row">
      <span role="rowheader">Ocean</span>
      <span role="cell">Dolphin</span>
      <span role="cell">Albatross</span>
      <span role="cell">Turtle</span>
    </div>
    <div role="row">
      <span role="rowheader">Desert</span>
      <span role="cell">Camel</span>
      <span role="cell">Falcon</span>
      <span role="cell">Gecko</span>
    </div>
    <div role="row">
      <span role="rowheader">Wetland</span>
      <span role="cell">Otter</span>
      <span role="cell">Heron</span>
      <span role="cell">Crocodile</span>
    </div>
  </div>
</div>

Example 2: Sortable table

This example uses the same ARIA table roles as Example 1, with a sort button inside each column header. Activate a column header button to sort by that column. aria-sort is added to the sorted columnheader to reflect the current sort direction.

Users read this table the same way as Example 1, using their screen reader's table navigation. The only interactive elements are the sort buttons, which are reached with Tab like any other button.

Species count and average temperature by habitat

Forest 120 12
Ocean 230 17
Desert 45 38
Wetland 88 22
HTML markup
<h3 id="bbb">Species count...</h3>

<div id="sort-announcer" class="sr-only" role="status"></div>

<div role="table" aria-labelledby="bbb" id="sortable-table">
  <div role="rowgroup">
    <div role="row">
      <span role="columnheader" data-type="text">
        <button type="button" aria-label="Habitat - activate to sort">
          Habitat
          <span aria-hidden="true"></span>
        </button>
      </span>
      <span role="columnheader" data-type="number">
        <button type="button" aria-label="Number of species - activate to sort">
          Number of species
          <span aria-hidden="true"></span>
        </button>
      </span>
      <span role="columnheader" data-type="number">
        <button type="button" aria-label="Average temp - activate to sort">
          Average temp
          <span aria-hidden="true"></span>
        </button>
      </span>
    </div>
  </div>
  <div role="rowgroup" id="sortable-table-body">
    <div role="row">
      <span role="rowheader">Forest</span>
      <span role="cell">120</span>
      <span role="cell">12</span>
    </div>

    <!-- remaining rows follow the same pattern -->
  </div>
</div>

<!-- After sorting by Habitat, the active header gains aria-sort -->
<span role="columnheader" aria-sort="ascending" data-type="text">...</span>

Features demonstrated in this example

Why this example uses role="table", not role="grid"

Adding sort buttons doesn't turn a table into a grid. A grid is a composite widget: users are expected to move between individual cells with the arrow keys, and the grid manages focus as a single Tab stop. When a screen reader announces "grid", users expect that behaviour.

A sortable table only needs a few buttons in the header row. Users still read the data with their screen reader's normal table navigation, and reach the buttons with Tab. That's a table with interactive headers, so role="table" is the right fit. This matches the sortable table example in the ARIA Authoring Practices Guide.

Use role="grid" only when users need to move between and interact with individual cells, such as a spreadsheet or an editable data grid, and only when the full arrow-key navigation is implemented.

Native tables first. These examples use ARIA roles to show what each role does. In production, a native <table> with <th> elements and buttons in the header cells provides the same result with far less to get wrong, and is what the APG sortable table example uses.