Skip to content

Sortable Table Example

Sortable Table Example

Read This First

The code in this example is not intended for production environments. Before using it for any purpose, read this to understand why.

This is an illustrative example of one way of using ARIA that conforms with the ARIA specification.

About This Example

The example below illustrates an implementation of the Table Pattern for a table with sortable rows. The example uses HTML table markup for all elements of the table structure, e.g., cells, rows, column headers, and caption. The aria-sort attribute is set on the column header of the currently sorted column, and the header text of sortable columns is wrapped in a button element. One column, the Address column is not sortable.

Similar examples include:

  • Data Grid Examples: Three example implementations of grid that include features relevant to presenting tabular information, such as content editing, sort, and column hiding.
  • Table Example: ARIA table made using HTML div and span elements.

Example Option

Adds a diamond shaped icon (e.g. ) to the header of each column that can be sorted but is not currently sorted. Some sortable tables add an icon to unsorted columns to help users distinguish sortable columns from columns that cannot be sorted. It is important that the shape of the unsorted icon differ in more than just color and size from the icons that indicate sort direction (e.g. '▼' and '▲') so people with visual impairments can easily distinguish them.

Example

Students currently enrolled in WAI-ARIA 101 , column headers with buttons are sortable.
Address
Fred Jackson Canary, Inc. 123 Broad St. 56
Sara James Cardinal, Inc. 457 First St. 7
Ralph Jefferson Robin, Inc. 456 Main St. 513
Nancy Jensen Eagle, Inc. 2203 Logan Dr. 3.5

Accessibility Features

  • To help screen reader users understand the purpose of the buttons in the column headers, an off-screen description of the sort functionality of the buttons is appended to the caption text. The description is added to the caption instead of to each button to prevent repetitious verbosity that could interfere with understanding of the column titles.
  • To enhance perceivability when operating the sort buttons, visual keyboard focus and hover are styled using CSS :hover and :focus pseudo-classes:
    • To make it easier to perceive when a button has focus, the focus indicator encompasses both the column label and sort direction icon.
    • The cursor is changed to a pointer when hovering over the button to help people identify it as an interactive element.
    • To make it easier to perceive that clicking either the column label or the sort direction icon will sort the table, hover styles the button and icon in the same way that focus does.
    • To make activating sort easier for people with visual and movement impairments who are using a pointing device, the click target is maximized not only by making both the column label and sort icon clickable but also by using CSS positioning and sizing to make the button fill the entire header cell area.
  • To ensure the sorting direction icons have sufficient contrast with the background when high contrast settings invert colors, character entities (e.g. '▼' and '▲') are used to indicate the sorting direction.

Keyboard Support

Not applicable: The only interactive elements are HTML button elements, and all their keyboard functionality is provided by browsers.

Role, Property, State, and Tabindex Attributes

Role Attribute Element Usage
aria-sort="value" th
  • Set on the currently sorted column. When the sorted column is changed, the aria-sort attribute is removed and set on the newly sorted column.
  • A value of "ascending" indicates the data cells in the column are sorted in ascending order.
  • A value of "descending" indicates the data cells in the column are sorted in descending order.
aria-hidden="true" span Removes the character entities used for sort icons from the accessibility tree to prevent them from being included in the accessible name of the sort buttons.

JavaScript and CSS Source Code

HTML Source Code

To copy the following HTML code, please open it in CodePen.

Back to Top

This is an unpublished draft preview that might include content that is not yet approved. The published website is at w3.org/WAI/.