Full answer
Assistive technology does not read pixels. It reads the accessibility tree, which the browser builds from the DOM and ARIA attributes. Each node has:
- a role, which says what the thing is: button, link, heading, textbox;
- a name, the text that identifies it: "Save";
- a state or value, such as checked, expanded or disabled.
Native elements provide all of this by default. A <button> has the role button, takes its name from the text inside, appears in the Tab order, and activates on Enter and Space. It also works with voice control, which looks for the visible name, and with the operating system's high contrast mode.
A <div onclick="..."> has the role generic, no name that says it can be activated, no tab stop, and no keyboard activation. To repair it, you have to add role="button", tabindex="0", key handlers for Enter and Space, and focus styles. That is more code than the native element and still misses some behaviour, such as the disabled state and form submission.
Rules to remember:
- Use the native element when one exists:
button,a href,input,select,details,dialog,nav,main,h1toh6. - Use a link for navigation, and a button for actions on the page.
- Use ARIA only to fill gaps that HTML cannot cover.
- Test with the keyboard alone, and look at the tree in the DevTools Accessibility panel.
The page below has a real button and a div with a click handler. The test reads the accessibility tree snapshot, which lists a button named Save, while the div appears only as plain text. It also presses Tab, and only the button receives focus. Pressing Enter triggers the button's handler.
<button id="real" type="button">Save</button>
<div id="fake" onclick="window.fakeClicked = true">Save</div>
<script>
window.clicks = 0;
real.addEventListener('click', () => { window.clicks += 1; });
</script>Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The check read the accessibility snapshot, pressed Tab and Enter with real key presses, and confirmed that only the button took focus and ran its handler. No screen reader was run.
Key points
- Assistive tools use the accessibility tree: role, name and state.
- Native elements supply role, focus and keyboard behaviour for free.
- A div with onclick has no role, no focus and no keyboard support.
- Use links for navigation and buttons for actions.
- Test with the keyboard and the Accessibility panel.
Common mistakes
- Adding role="button" to a div and forgetting tabindex and key handlers.
- Using a link with href="#" as an action button.
- Removing the focus outline without a replacement.
- Believing that ARIA changes behaviour. It only changes what is announced.
Follow-ups
- What does the first rule of ARIA say?
- How is the accessible name of a button computed?
- When is a link better than a button?
Tips
- Name role, name and state, and give one example of each.
- Say that you check by using only the keyboard.