ARIA Roles, States and Properties

Intermediate · 19 min

What ARIA adds to the accessibility tree, the difference between roles, states and properties, and the rules that keep it from making a page worse.

Before thisKeyboard, Focus and tabindex

Stanford WebCamp · 19 min

ARIA Roles, States and Properties

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Stanford WebCampReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • ARIA changes what the accessibility tree says about an element. It never changes what the element does: no focus, no key handling, no click behaviour.
  • A role says what something is (`tab`, `dialog`), a state says what condition it is in right now (`aria-expanded`, `aria-selected`, `aria-pressed`), and a property describes a relationship or label that rarely changes (`aria-controls`, `aria-label`).
  • Use a native element first. The first rule of ARIA is not to use it when HTML already has the semantics.
  • ARIA you add is a promise. A `role="menuitem"` on a button outside any menu was exposed as a menu item, and a `div role="button"` took no Enter or Space presses.
  • Some native elements already report ARIA states for you: a button with `popovertarget` reported `expanded` and flipped it when the popover opened.

The gap ARIA fills

HTML has a fixed vocabulary of elements. It has a button and a nav, but no tab panel, no tree view, no “this region updates by itself”. When a team built these out of divs anyway, assistive technology saw a pile of generic nodes. WAI-ARIA (Accessible Rich Internet Applications) was added so authors could describe those widgets by hand: “this div is a tab, it is selected, it controls that panel.”

That is the whole job. ARIA is annotation on the accessibility tree. It is a label stuck on a switch, not the switch.

Roles, states, properties

ARIA has three kinds of attribute, and mixing them up leads to bugs.

  • Role (role="tab"): what the element is. It tends to be fixed for the element’s life.
  • State (aria-selected, aria-expanded, aria-pressed, aria-checked): a condition that changes as the user works. If the visuals change and the state does not, the tree is lying.
  • Property (aria-controls, aria-describedby, aria-label): a label or relationship that mostly stays put. The line between states and properties is blurry, but asking “does this change while the page runs?” sorts most attributes.

I checked how these show up using Chromium 159’s accessibility tree. A button with aria-expanded="false" was exposed with expanded=false, and after a script flipped the attribute to true it was exposed as expanded=true. A pair of role="tab" buttons with aria-selected of true and false came out as a selected tab and an unselected one. The names of the elements came from their text, which is the next lesson’s subject.

ARIA changes nothing about behaviour

This is the sentence that prevents most ARIA bugs. Roles and states describe; they do not wire anything up. In a test, a <div role="button"> with no tabindex could not be focused, and the version with tabindex="0" still produced zero click events from Enter and Space, while a plain button produced both. The tree said “button” and the keyboard said “nothing”. The focus lesson showed the same thing from the other side.

So the working rules, which the W3C’s guidance sets out in a longer form, boil down to this:

  1. If HTML has an element for it, use that. button, a, details, dialog, input type="checkbox".
  2. Don’t change a native element’s meaning with a role unless you have to.
  3. Every ARIA control must work with the keyboard, because ARIA won’t do that for you.
  4. Don’t put aria-hidden or role="presentation" on something focusable.
  5. Every interactive control needs an accessible name.

“No ARIA is better than bad ARIA” follows. Bad ARIA is a promise the page doesn’t keep. In a test a plain <button role="menuitem"> outside any menu was exposed as a menuitem with no menu around it, which is a description that screen reader users would take at face value.

Native elements already speak ARIA

Part of the point of modern HTML is that you don’t write the attributes. A <button popovertarget="menu"> was exposed with expanded=false, and once the popover opened it was expanded=true, with no ARIA in the markup. summary does the same job for details. A native <dialog> opened with showModal() gets a dialog role. This is why the next lessons on details, dialog and the popover attribute have so little ARIA in them. When you do write a widget by hand, the W3C’s ARIA Authoring Practices Guide has the full pattern for each one: the roles, the states and the key bindings.

A word on aria-label on a div

One result that surprised me: a plain div with aria-label="label on a div" still exposed that name in Chromium’s tree, although the spec says naming is not allowed on a generic element. Do not rely on it. Labels belong on elements with a role that supports names, which the names and descriptions lesson lays out.

What the video covers

This is “Introduction to WAI-ARIA (Accessible Rich Internet Applications)” from Stanford WebCamp (May 2021, about 19 minutes). Its description says it is a brief introduction to ARIA concepts: what it is, why it is needed, rules for using it, and common mistakes. I could not read a transcript, so I can’t say which mistakes it picks or in what order. The browser results above, including the keyboard and tab-order tests, are mine and were not taken from it. The frontend roadmap’s accessible HTML and ARIA basics lesson covers the same ground more briefly.

Where this fits

This follows keyboard, focus and tabindex, because ARIA without keyboard support fails. Next, accessible names and descriptions covers the attributes that label controls, which are the most common ARIA you will write.

Check yourself

  1. 01What is the difference between a role and a state?

    A role is what an element is, and it usually stays fixed: `tab`, `button`, `dialog`. A state is a current condition that changes while the page runs: `aria-expanded`, `aria-selected`, `aria-checked`. Scripts have to keep states in sync with what the user sees.

  2. 02A disclosure button toggles a panel. Which ARIA belongs on the button?

    `aria-expanded` set to `true` or `false` and updated on every toggle, and optionally `aria-controls` pointing at the panel's id. In a test the button's exposed state changed from `expanded=false` to `expanded=true` when the script flipped the attribute. Better still, use `details` and `summary`, or a `popovertarget` button, where the browser does it for you.

  3. 03Why is `<div role="button">` worse than `<button>`?

    The role only changes how the element is described. In a test it was not focusable without `tabindex`, and with `tabindex="0"` it still ignored Enter and Space. A `button` is focusable, activates on both keys, and has a role, all at once.

  4. 04A teammate puts `aria-hidden="true"` on a modal's background content and calls it done. What is still wrong?

    `aria-hidden` removes the content from the tree, but its links and buttons stay in the tab order. In a test, Tab still reached buttons inside an `aria-hidden` container. Keyboard users can then focus controls they cannot hear. Use `inert`, or `showModal()`, which does both jobs.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark