Skip to main content
Saved

Pattern Accessibility Beginner

ARIA Roles and Attributes

ARIA attributes are for cases where native HTML can’t describe your widget. Roles say what a custom widget is, states such as aria-expanded report whether it’s open or selected, and properties describe how elements relate to each other.

Examples
native web
Read
5 min

The problem

You’ve built a custom dropdown menu with divs and CSS. It looks good and works with a mouse, but when you try it with a screen reader, you hear the text of each item and nothing else. The screen reader doesn’t mention that it’s a menu, and doesn’t announce anything when the selection changes.

The pattern

Roles such as role="menu" or role="tab" say what kind of widget an element is, and attributes such as aria-expanded and aria-selected describe its state. Once the user opens your dropdown, your script sets aria-expanded to "true". As they select a tab, the active tab gets aria-selected="true" and the rest get "false". Screen readers announce these values, so users hear that the dropdown is expanded or which tab is selected.

Problem

I’ve tested interfaces where tab panels and tree views were completely unusable with screen readers. Users couldn’t tell which tab was selected, or which items in a tree were nested inside others.

Native HTML doesn’t have an element for every kind of widget. There’s no <combobox> or <tabpanel> element, so when you build custom widgets, you need a way to tell assistive technologies what they are and how they behave.

Solution

You can use aria-label when an element’s visible text isn’t enough to describe it. For relationships between elements, aria-describedby connects an element to its description, and aria-controls indicates which element a button controls.

ARIA should be a last resort, though. Elements such as <button> and <dialog> come with their roles and keyboard behavior built in, so you only need ARIA when HTML doesn’t have an element for what you’re building.

Example

The first version below is the div-only dropdown from the Problem. The second adds ARIA to it. The button uses aria-haspopup, aria-expanded, and aria-controls to say that it opens a menu, whether that menu is open, and which element the menu is. The list uses the menu and menuitem roles, so a screen reader announces something like “Actions, menu button, collapsed” on the button, and “Rename, menu item, 1 of 3” inside the list.

Divs only
<div class="dropdown-button">Actions</div>
<div class="dropdown-list">
  <div>Rename</div>
  <div>Duplicate</div>
  <div>Delete</div>
</div>
Screen readers announce the text and nothing else
With ARIA
<button
  id="actions-button"
  aria-haspopup="menu"
  aria-expanded="false"
  aria-controls="actions-menu"
>
  Actions
</button>

<ul id="actions-menu" role="menu" aria-labelledby="actions-button" hidden>
  <li role="menuitem" tabindex="-1">Rename</li>
  <li role="menuitem" tabindex="-1">Duplicate</li>
  <li role="menuitem" tabindex="-1">Delete</li>
</ul>
Screen readers announce a menu button and its menu items

If you use role="menu", screen reader users expect the keyboard to work the way it does in a menu. ARIA doesn’t add any of that behavior, so the markup above isn’t a working menu until your script provides it. Focus should move into the menu when it opens, arrow keys should move between items, and Escape should close the menu and return focus to the button. The script below does those three things and keeps aria-expanded up to date. A complete menu also handles Home, End, Enter, and type-ahead, as described in the APG menu button pattern.

const actionsButton = document.getElementById('actions-button');
const actionsMenu = document.getElementById('actions-menu');

function toggleMenu() {
  const isExpanded = actionsButton.getAttribute('aria-expanded') === 'true';

  actionsButton.setAttribute('aria-expanded', String(!isExpanded));
  actionsMenu.hidden = isExpanded;

  if (!isExpanded) {
    actionsMenu.querySelector('[role="menuitem"]').focus();
  }
}

actionsButton.addEventListener('click', toggleMenu);

actionsMenu.addEventListener('keydown', (event) => {
  const items = [...actionsMenu.querySelectorAll('[role="menuitem"]')];
  const index = items.indexOf(document.activeElement);

  if (event.key === 'ArrowDown') {
    event.preventDefault();
    items[(index + 1) % items.length].focus();
  } else if (event.key === 'ArrowUp') {
    event.preventDefault();
    items[(index - 1 + items.length) % items.length].focus();
  } else if (event.key === 'Escape') {
    toggleMenu();
    actionsButton.focus();
  }
});

Disclosure Instead of a Menu

The menu roles are for a list of actions, like the one above. A dropdown that opens a simple list of links, such as a Settings or account dropdown in a site’s navigation, doesn’t need them. I’d use a disclosure there instead, which is a button with aria-expanded and no menu roles. The links stay ordinary links that users move through with Tab, so the only script you need is the one that sets aria-expanded and hidden.

<button aria-expanded="false" aria-controls="settings-links">Settings</button>

<ul id="settings-links" hidden>
  <li><a href="/profile">Profile</a></li>
  <li><a href="/preferences">Preferences</a></li>
  <li><a href="/logout">Log out</a></li>
</ul>

Tab Interface with ARIA

In a set of tabs, role="tab" and aria-selected make a screen reader announce something like “Profile, tab, selected, 1 of 2”, and aria-controls and aria-labelledby connect each tab to its panel. Only the selected tab has tabindex="0", so pressing Tab moves focus to that one tab and then on to the panel, and the arrow keys move focus between the tabs. This is called a roving tabindex, and Keyboard Navigation has the script for it.

<div role="tablist" aria-label="Account settings">
  <button role="tab" aria-selected="true" aria-controls="profile-panel" id="profile-tab" tabindex="0">
    Profile
  </button>
  <button role="tab" aria-selected="false" aria-controls="security-panel" id="security-tab" tabindex="-1">
    Security
  </button>
</div>

<div role="tabpanel" id="profile-panel" aria-labelledby="profile-tab" tabindex="0">
  <!-- Profile content -->
</div>

<div role="tabpanel" id="security-panel" aria-labelledby="security-tab" tabindex="0" hidden>
  <!-- Security content -->
</div>

A more complicated widget, such as a combobox, needs more than this, because your script has to update several attributes on each keypress. The APG combobox pattern has complete examples that you can start from.

Toggle Button

With aria-pressed, a screen reader announces a button as a toggle button that’s pressed or not pressed. The emoji is hidden with aria-hidden, so aria-label gives the button its name.

<button aria-pressed="false" aria-label="Dark mode" onclick="toggleDarkMode(this)">
  <span aria-hidden="true">🌙</span>
</button>

<script>
function toggleDarkMode(button) {
  const isPressed = button.getAttribute('aria-pressed') === 'true';
  // The label stays the same, and aria-pressed reports the on/off state
  button.setAttribute('aria-pressed', String(!isPressed));
}
</script>

Unnecessary ARIA

Native HTML elements like <button>, <nav>, <dialog>, and <select> already have the right semantics, so you don’t need ARIA to rebuild them. The div below needs a role, a tabindex, and a keydown handler to do what the <button> does without any of them.

ARIA on a div
<div role="button" tabindex="0" onclick="save()" onkeydown="saveOnEnterOrSpace(event)">
  Save
</div>
Announced as a button, but needs its own key handling
Native element
<button onclick="save()">Save</button>
Role, focus, and Enter and Space handling are built in

The same goes for landmarks, because the <header>, <nav>, <main>, <aside>, and <footer> elements already have the roles below. The explicit roles are only needed for markup that you can’t change.

<div role="banner"><!-- header --></div>
<div role="navigation" aria-label="Main"><!-- nav links --></div>
<div role="main"><!-- primary content --></div>
<div role="complementary"><!-- sidebar --></div>
<div role="contentinfo"><!-- footer --></div>

Benefits

  • Screen reader users can use your custom widgets, because a tab panel is announced as a tab panel instead of as a set of unrelated divs.
  • Screen readers can report state, such as whether the dropdown is open or which tab is selected.
  • You can build complex components, such as comboboxes and tree views, that work for screen reader users too.
  • Attributes such as aria-controls and aria-describedby let screen reader users know how elements relate to each other.

Tradeoffs

  • A misused role gives screen reader users false information about the element, which is worse than having no ARIA at all.
  • The specification is complex and takes time to learn. It has strict rules about parent and child roles, such as role="menuitem" needing a parent with role="menu", and about which attributes each role supports.
  • It’s easy to overdo ARIA. Adding role="button" to a <button> is redundant, and adding ARIA where HTML would do the job gives you more code to maintain for no benefit.
  • ARIA doesn’t add any behavior. With role="button" on a div, screen readers announce it as a button, but it won’t respond to Enter or Space until you add JavaScript for that.
  • Automated tests can catch invalid or missing attributes, but they can’t tell you whether a widget makes sense to someone using it. You still need to test with real screen readers, and support varies between NVDA, JAWS, and VoiceOver.
  • aria-hidden="true" hides an element and everything inside it from screen readers. If you put it on a container, it’s easy to hide a button or a heading inside it by accident.
  • Screen readers usually announce a state change, such as a new aria-expanded value, only when it happens on the element that has focus. If the change happens somewhere else on the page, users might not hear about it unless you also use a live region or move focus.

Summary

ARIA lets you describe custom widgets, such as tabs and menus, to screen readers when HTML doesn’t have an element for them. Roles say what each widget is, and states and properties report whether it’s open or selected and which elements it controls. ARIA doesn’t add any keyboard behavior, so your script still has to provide that, and wherever a native HTML element does the job, it’s the better choice.

See also

Next in Accessibility Focus Management Moving keyboard focus to the right place when the UI changes, such as when a modal dialog opens or the route changes.

A monthly email
from Den Odell.

What I'm thinking about frontend patterns, plus news when the site changes.

No spam. Unsubscribe anytime.