ARIA roles, states and properties: when to use them
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Decide when a native element is available and when ARIA is required.
- Apply aria-expanded, aria-controls and aria-live correctly.
Overview
#ARIA is a set of attributes that describe roles, states and properties when no native element fits. The first rule is to prefer native HTML. The second rule is not to change native semantics unless you must. The third rule is that all interactive ARIA controls must be keyboard operable.
States change while the user interacts. aria-expanded reports whether a disclosure is open. aria-controls points at the element being controlled. aria-selected marks the active tab. aria-checked reports a toggle. These are not styling hooks; they are the source of truth for assistive technology.
<button aria-expanded="false" aria-controls="panel" id="toggle">
Shipping details
</button>
<div id="panel" hidden>
<p>Ships in 2 to 4 days.</p>
</div>
Live regions announce changes that happen without a page reload. aria-live="polite" waits for a pause, which suits status messages. aria-live="assertive" interrupts, which suits errors. role="status" is a polite live region and role="alert" is assertive. Use them sparingly, because every announcement competes for the user's attention.
Two traps are common. First, aria-hidden="true" on a focusable element creates a control that keyboard users can reach but screen readers cannot describe. Second, adding role and aria-* without the matching keyboard behavior produces a widget that claims to be a tab or menu but cannot be operated. If you add a composite role, you own its full keyboard contract.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Build a disclosure button that controls a panel. The button must report its expanded state and point at the panel it controls.
starter.html<button id="toggle">Details</button> <div id="panel"><p>Hidden content.</p></div>Reveal the reference answer
reference.html<button id="toggle" aria-expanded="false" aria-controls="panel">Details</button> <div id="panel" hidden><p>Hidden content.</p></div> <!-- on toggle: set aria-expanded to the new state and toggle the hidden attribute -->Authored expected output: The button exposes aria-expanded and aria-controls, and the panel starts hidden.
aria-expanded reports state and aria-controls associates the button with the region it shows or hides.
Exercise 2Debug
This code hides a focusable link from assistive technology. Explain the bug and fix it.
starter.html<div aria-hidden="true"> <a href="/terms">Terms</a> </div>Reveal the reference answer
reference.htmlRemove aria-hidden from the container, or remove the link from the tab order as well. The safe fix is to not hide a container that contains focusable content: <div> <a href="/terms">Terms</a> </div>Authored expected output: The container no longer hides focusable content from assistive technology, or the content is removed from the focus order too.
aria-hidden removes content from the accessibility tree but not from the tab order, so a hidden container with a link creates an invisible focus stop.
Common misconceptions
#- aria-hidden="true" is safe on any element, including focusable ones.
- Adding role=button is enough to make a div behave like a button.
Follow-up practice
#- html-css-15-landmarks-skip-links
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.