← Back to HTML & CSS, page 2

html-css-0033 · Medium mid

When should you use ARIA, and what are the basic rules for using it correctly?

ARIA adds roles, states and properties to the accessibility tree but gives no behaviour. The first rule is to use a native element when one exists. Use ARIA for what HTML lacks, such as aria-expanded on a disclosure or a live region, keep states in sync with script, and make every ARIA control keyboard operable and named.

Full answer

ARIA is a set of attributes that adds meaning to the accessibility tree: roles (what something is), states (aria-expanded, aria-checked) and properties (aria-label, aria-controls). It changes only what assistive technology hears. It does not add behaviour, focus or keyboard support.

The first rule of ARIA: if a native HTML element already has the meaning and behaviour you need, use it. A button is better than div role="button", because the native one is focusable, reacts to Enter and Space, and is announced correctly with no extra work.

The basic rules are:

  1. Prefer native elements to ARIA.
  2. Do not change native semantics without a reason (no role="heading" on a button).
  3. Every interactive ARIA control must work with the keyboard.
  4. Never put aria-hidden="true" or role="presentation" on something focusable.
  5. Every interactive element needs an accessible name (visible text, aria-label, or aria-labelledby).

ARIA is the right tool when HTML has no equivalent: a disclosure that reports whether it is open, a live region that announces updates, a custom tab list or combo box.

The toggle button is announced as "Details, button, collapsed" and "expanded" after a click, because the script keeps aria-expanded in sync with the visible state. The icon button gets the name "Save note" from aria-label. The role="status" paragraph is a live region, so "Note saved" is announced without moving focus.

Wrong or stale ARIA is worse than none, so test with a screen reader or the browser's accessibility panel.

html
<button id="toggle" type="button" aria-expanded="false" aria-controls="details">Details</button>
<div id="details" hidden>Fees are payable in two instalments.</div>

<button id="save" type="button" aria-label="Save note">
  <svg width="16" height="16" aria-hidden="true"><rect width="16" height="16" fill="black"/></svg>
</button>

<p id="status" role="status"></p>
<script>
  const toggle = document.getElementById("toggle"), details = document.getElementById("details");
  toggle.addEventListener("click", () => {
    const open = toggle.getAttribute("aria-expanded") === "true";
    toggle.setAttribute("aria-expanded", String(!open));
    details.hidden = open;
  });
  document.getElementById("save").addEventListener("click", () => {
    document.getElementById("status").textContent = "Note saved";
  });
</script>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Native HTML first: a button beats div role="button".
  • ARIA changes only what assistive technology hears, not behaviour or focus.
  • States like aria-expanded must be updated from script.
  • Never use aria-hidden on focusable content.
  • Every interactive element needs an accessible name.

Common mistakes

  • adding aria-label to a div or span with no role. Most screen readers ignore it, because the element has no semantic meaning to name.
  • setting aria-expanded in HTML and never updating it from JavaScript, so it reports the wrong state.

Follow-ups

  • what is the difference between aria-label, aria-labelledby and aria-describedby?
  • What is a live region and when would you use role="status"?
  • Why is aria-label on a plain div often useless?

Tips

  • Use the browser accessibility tree view in DevTools to see the computed role, name and state of an element.
  • Describe the disclosure button with aria-expanded as your example; it is short and complete.
  • Say that you test with the accessibility tree or a screen reader.