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, native interactive elements and the accessibility tree.
What ARIA is
ARIA (Accessible Rich Internet Applications) is a set of attributes that change what the browser reports to assistive technology. It adds roles, states and properties to the accessibility tree. It does not add behavior. An element with role="button" is announced as a button, but it still has no focus, no Enter or Space handling, and no click on keypress.
ARIA has three kinds of attributes:
- Roles say what an element is, such as
role="tablist". - States describe something that changes, such as
aria-expanded="true". - Properties describe relationships or labels, such as
aria-labelledbyoraria-label.
The native-first rule
The first rule of ARIA is to use a native element when one exists. A native element gives you the role, keyboard support and focus for free. ARIA only changes the announcement, so you must write the rest yourself.
<!-- Avoid: ARIA copies the role, but you still owe focus and keys -->
<div role="button" tabindex="0" onclick="save()">Save</div>
<!-- Prefer: nothing extra to write -->
<button type="button" onclick="save()">Save</button>The div version still needs a keydown handler for Enter and Space. The button needs nothing.
When ARIA is justified
Use ARIA in three situations:
- No native element exists. HTML has no tabs, tree view, combobox or menu bar. ARIA roles describe these widgets.
- You need to distinguish repeated landmarks. Two
navelements need different names. - You need to expose state or a relationship. For example, a button that toggles a panel needs
aria-expanded.
<nav aria-label="Primary">
<a href="/articles">Articles</a>
</nav>
<nav aria-label="Footer">
<a href="/privacy">Privacy</a>
</nav>
<button type="button" aria-expanded="false" aria-controls="filters">
Filters
</button>
<div id="filters" hidden>
<p>Filter options go here.</p>
</div>
<script>
const button = document.querySelector("button[aria-controls]");
const panel = document.getElementById(button.getAttribute("aria-controls"));
button.addEventListener("click", () => {
const open = button.getAttribute("aria-expanded") === "true";
button.setAttribute("aria-expanded", String(!open));
panel.hidden = open;
});
</script>The script keeps aria-expanded in step with what the user sees. If the attribute and the visible state disagree, the page misleads screen reader users.
Common errors
- Redundant roles.
<nav role="navigation">and<button role="button">add nothing. - Conflicting roles.
<h2 role="button">removes the heading from the heading outline. - Stale state.
aria-expandedthat never updates is worse than none. - Hiding focusable content.
aria-hidden="true"on an element that can still receive focus leaves keyboard users on something a screen reader cannot announce. - Broken references.
aria-labelledbyoraria-controlspointing to an id that does not exist does nothing.
Checking your work
Open the Accessibility panel in browser developer tools and select the element. Confirm that the role, name and states match what you intended. Then try the widget using only the keyboard.
Exercise
A teammate writes <span role="button">Delete</span> and says it is now accessible. Name two things still missing and give the better fix.
Show answer
The span cannot receive keyboard focus, and it does not respond to Enter or Space. It also lacks the default button behavior of form submission. The better fix is <button type="button">Delete</button>, which provides the role, focus and keyboard activation natively.