ARIA table examples
Example 1
This example uses a range of roles to define table structure.
role="table": to replicate the<table>elementrole="rowgroup": to replicate the<thead>and<tbody>elementsrole="row": to replicate the<tr>elementrole="columnheader": to replicate the<th>element at the top of columnsrole="rowheader": to replicate the<th>element at the start of rowsrole="cell": to replicate the<td>element- The heading is used to give the ARIA table a label via
aria-labelledby
Animals found in different habitats
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
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
-
aria-labelon the sort buttons. The buttons used to sort rows includearia-labelto provide additional context for screen reader users, for exampleHabitat - activate to sort
. The visible text remains concise, while the accessible name explains that each button can be activated to sort the table. -
aria-sorton the sorted column only. No header hasaria-sortuntil the table is sorted. When a column is sorted, its header getsaria-sort="ascending"oraria-sort="descending", and the attribute is removed from every other header. This follows the ARIA guidance thataria-sortshould only be applied to one header at a time. -
data-typeattributes for JavaScript. Thedata-typeattribute is used only by the JavaScript to decide whether to sort a column as text or as numbers. It provides no accessibility information and is never exposed to assistive technologies. -
A live region
role="status". When the sort order changes, a live region announces the result, such asTable sorted by Habitat, ascending
. This provides immediate feedback, as many screen readers don't announce a change toaria-sorton its own. -
Reordering the DOM, not just the display. The table rows are physically reordered in the document, so the reading order for screen reader users matches the visual order seen by sighted users.
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.