Sub topic 13
ARIA: When Native HTML Is Not Enough
This chapter assumes you know basic HTML syntax, have used browser developer tools, and have read the earlier chapters on landmarks, forms and interactive elements. It also assumes you know that browsers build an accessibility tree from your markup.
What ARIA is
ARIA stands for Accessible Rich Internet Applications. It is a set of attributes that change what the browser reports to assistive technology (AT), such as screen readers. ARIA changes the accessibility tree only. It does not change how an element looks, whether it can receive focus, or how it responds to the keyboard.
ARIA attributes fall into three groups:
- Roles say what an element is, for example
role="tab"orrole="alert". - States describe conditions that change, for example
aria-expanded="true"oraria-checked="false". - Properties describe relationships and labels that change less often, for example
aria-label,aria-labelledbyandaria-describedby.
The native-first rule
The first rule of ARIA is to use a native HTML element when one exists with the semantics and behavior you need. Native elements come with a role, focusability, keyboard handling and state, all maintained by the browser.
The div and button example shows the gap. The div needs a role, a tabindex, key handlers for Enter and Space, and state management. The button needs none of these.
<!-- ARIA can imitate a button, but you must supply everything else -->
<div role="button" tabindex="0" id="save-div">Save</div>
<script>
const el = document.getElementById("save-div");
el.addEventListener("click", () => console.log("saved"));
el.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
console.log("saved");
}
});
</script>
<!-- Native: role, focus and keyboard support are built in -->
<button type="button" onclick="console.log('saved')">Save</button>The role="button" on the div only changes how the element is announced. Without the tabindex and the key handler, keyboard users could not operate it at all.
Roles that duplicate native semantics
Adding a role that the element already has is redundant. <nav role="navigation">, <button role="button"> and <ul role="list"> add nothing. Overriding the native role is usually worse. <h2 role="tab"> removes the heading from the heading outline and makes it a tab, so the author has to supply tab behavior as well.
Two cases need care:
- Some CSS techniques, such as
list-style: nonein certain browsers, have historically caused a list to lose its list role in the accessibility tree. Check the tree in devtools before you addrole="list"as a workaround. - Using
role="presentation"orrole="none"removes an element's semantics. It is meant for cases such as layout tables. It does not hide content, and it has no effect on focusable elements.
The five rules of ARIA
The W3C guidance for using ARIA can be summarized as follows:
- Use a native element or attribute if one exists.
- Do not change native semantics unless you really have to.
- All interactive ARIA controls must be operable with the keyboard.
- Do not use
role="presentation"oraria-hidden="true"on a focusable element. - All interactive elements must have an accessible name.
Accessible names and descriptions
Every interactive element needs an accessible name, which is the text AT announces for it. The browser computes the name from several sources. For a rough order of priority:
aria-labelledby, which points to other elements byid.aria-label, a string on the element itself.- Native sources: the content of a
button, an associatedlabel, thealtof an image, thecaptionof a table. - The
titleattribute, as a last resort.
An accessible description adds supporting detail and comes from aria-describedby. A name says what the control is. A description says more about it, such as a format hint or an error message.
<button type="button" aria-label="Close dialog">×</button>
<label for="pw">Password</label>
<input id="pw" type="password" aria-describedby="pw-hint">
<p id="pw-hint">Use at least 12 characters.</p>Prefer visible text as the name. aria-label is invisible to sighted users, is not translated by some browser translation tools, and can drift out of sync with the visible label. When the visible text and the accessible name differ, speech-control users who say "click Search" may fail to activate a control named "Find". The name should contain the visible text.
Use aria-labelledby when the label already exists on the page. For example, a landmark can be named by its own heading:
<section aria-labelledby="news-title">
<h2 id="news-title">Latest news</h2>
<p>Stories appear here.</p>
</section>Naming works only on some elements. aria-label on a plain div or span with no role is not reliably announced, so do not use it to add hidden text to generic containers.
Landmark labels
When a page has more than one landmark of the same type, give each a distinct name. This is the most common justified use of ARIA in otherwise native pages.
<nav aria-label="Primary">...</nav>
<nav aria-label="Footer">...</nav>A screen reader user can then tell the two navigation regions apart in a landmarks list. Omit the word "navigation" from the label, because the role is announced already.
States and live regions
Native HTML has no way to express some states, and ARIA fills the gap. These are the most common:
| Attribute | Use | Who updates it |
|---|---|---|
| `aria-expanded` | A button that opens or closes a region | Your script, on each toggle |
| `aria-current` | The current link in a set, such as `aria-current="page"` | Your template or router |
| `aria-pressed` | A toggle button | Your script |
| `aria-invalid` | A form field with a failed validation | Your script |
| `aria-hidden` | Hides content from AT only | Your script or template |
State attributes are not self-updating. If a button has aria-expanded="false" and your script opens the panel without changing the attribute, AT users hear the wrong state. Keep the state attribute and the visible behavior in one code path.
A live region announces changes to content without moving focus. Use it for status messages such as "3 results found" or "Saved".
<p id="status" role="status"></p>
<button type="button" id="search">Search</button>
<script>
document.getElementById("search").addEventListener("click", () => {
document.getElementById("status").textContent = "3 results found";
});
</script>For live regions to work reliably, the container should already exist in the page when the content changes. Inserting a new region together with its text often goes unannounced. role="status" is polite, meaning it waits for the user to pause. role="alert" is assertive and interrupts, so reserve it for errors that need immediate attention.
Widget patterns that need ARIA
Some UI patterns have no native element, or the native element cannot be styled or scripted to match the design. Examples are tabs, tree views, comboboxes with custom listboxes, menus and menu bars, and modeless popovers. These need a role set, relationships and keyboard behavior, and the WAI-ARIA Authoring Practices describe each in detail.
A tab set shows the amount of work:
<div role="tablist" aria-label="Account sections">
<button role="tab" id="tab-profile" aria-selected="true"
aria-controls="panel-profile">Profile</button>
<button role="tab" id="tab-billing" aria-selected="false"
aria-controls="panel-billing" tabindex="-1">Billing</button>
</div>
<div role="tabpanel" id="panel-profile" aria-labelledby="tab-profile">
<p>Profile settings.</p>
</div>
<div role="tabpanel" id="panel-billing" aria-labelledby="tab-billing" hidden>
<p>Billing settings.</p>
</div>This markup is only the start. A script must still move aria-selected, toggle hidden on panels, set tabindex so only the active tab is in the tab order, and handle the arrow keys. If you cannot commit to that work, a simpler pattern is better, such as a list of links to separate pages or a group of details elements.
Common ARIA failures
aria-hidden="true"on a focusable element. The element stays in the tab order but disappears from the accessibility tree, so a keyboard user focuses something that has no name or role. Remove it from the tab order too, or usehiddenorinert.- Invalid role and attribute combinations.
aria-checkedon adivwithout a checkbox role does nothing useful. Each state belongs to specific roles. - Broken references.
aria-labelledby="titel"with a typo points to nothing, and the name silently falls back to something else. - Static state.
aria-expandedthat never changes, oraria-current="page"copied onto every link. - Overusing
aria-labelfor text that needs translation or visibility. - Roles without required children or parents. A
role="tab"outside atablistis invalid. Arole="listitem"outside a list is invalid. - Using
role="application"to "turn off" screen reader shortcuts. It changes how the screen reader handles the keyboard for the whole region and usually makes things harder.
How to verify ARIA
- In the browser's accessibility panel, select the element and confirm the computed role, name, description and states.
- Use only the keyboard. Tab to every control, activate it with Enter or Space, and check that arrow keys work where the pattern requires them.
- Run an automated checker such as the axe browser extension or Lighthouse. They catch broken references, invalid roles and missing names, but they cannot judge whether the experience makes sense.
- Try a screen reader (NVDA on Windows, VoiceOver on macOS and iOS, TalkBack on Android) for the complex widgets.
Exercise
A teammate writes <div role="button" aria-label="Delete">Delete</div> and says the element is now accessible. Name three problems and give a fix.
Show answer
(1) The div is not focusable, so keyboard users cannot reach it. (2) It has no Enter or Space handler, so keyboard users could not activate it even if they could focus it. (3) The aria-label repeats the visible text and adds nothing, so it is redundant and risks drifting out of sync. The fix is <button type="button">Delete</button>, which provides the role, focus, keyboard activation and the accessible name from its content.
Exercise
A page has a main navigation in the header and a navigation list in the footer. Both are <nav> elements. What should you add, and why?
Show answer
Give each nav a distinct accessible name, for example aria-label="Primary" and aria-label="Footer". Without names, a screen reader's landmark list shows two entries that are both just "navigation", and users cannot tell them apart. Leave the word "navigation" out of the label because the role is already announced.
Exercise
A "Show details" button toggles a panel. The script shows and hides the panel but never touches the button's attributes, which are aria-expanded="false" from the HTML. What happens for a screen reader user, and what are two ways to fix it?
Show answer
The screen reader announces the button as collapsed even after the panel opens, so the user gets the wrong state. One fix is to update aria-expanded to "true" or "false" in the same code that shows or hides the panel. The other is to replace the custom control with <details> and <summary>, which report their expanded state natively with no script.