
ARIA Doesn't Make a div a Button: Start with Semantic HTML
- كاتب موثّق
- WCAG 2.2
محتويات المقال
div with a click handler. Months ago a review found that screen readers read it as plain text, so someone added role="button". Screen readers started saying "button", the ticket was closed, and nobody tried the page without a mouse. Tab jumps straight past the control.
The common misconception
role="button" and a click handler to a div makes it as accessible as a real button.
div and a real <button> as the same entry: button "Place order". The click handler works with a mouse and with a tap. And MDN describes ARIA as a set of roles and attributes that make web content more accessible, so adding one feels like adding accessibility. If you only check what a screen reader announces, the job looks done.
What actually happens
ARIA edits the accessibility tree, nothing else
div and the button look identical to a screen reader. The difference is entirely in what they do.
What the div is still missing
div with role="button" and a real button on the same page in headless Chromium and used only the keyboard:
Focus: Tab skipped the divcompletely. It only joined the tab order after we addedtabindex="0".Activation: even when focused, Enter and Space didn't fire its clickhandler. MDN explains why: a native button firesclickfor Space and Enter, but on any other elementclickcomes only from the pointer, even with the role.Space: instead of activating anything, Space scrolled the page down by most of a screen (525px in our 600px-high viewport). Disabled: a disabled <button>can't be focused or pressed. Adivhas nodisabledattribute;aria-disabled="true"only announces a state and blocks nothing.
A role is a promise
role="button" promises that you also wrote the script for the keyboard interactions people expect from a button. Breaking it, the guide says, is similar to making a "Place Order" button that abandons an order and empties the shopping cart.
const fake = document.querySelector("#place-order");fake.tabIndex = 0; // join the tab orderfake.addEventListener("keydown", (event) => { if (event.key === "Enter") fake.click(); if (event.key === " ") event.preventDefault(); // don't scroll});fake.addEventListener("keyup", (event) => { if (event.key === " ") fake.click(); // like a native button});click on key down for Enter and on key up for Space, and this script copies that timing. It still has no disabled support and no form behavior, and every team that fakes a button writes its own slightly different copy.
role="button" needs a Space handler too. Choose the element by what it does: a button performs an action on the page, and a link takes the user somewhere.
The first rule of ARIA
<!-- Announced as a button, but Tab skips it and Enter or Space does nothing --><div class="btn" role="button" onclick="placeOrder()"> Place order</div><!-- In the tab order, Enter and Space click it, and disabled works --><button type="button" class="btn" onclick="placeOrder()"> Place order</button><div role="button"> | <button type="button"> | |
|---|---|---|
tabindex="0" | ||
click | ||
preventDefault() | ||
disabled blocks focus and clicks |
How to apply it in your project
Find the fakes Search your templates for role="button", for click handlers ondivandspan, and forcursor: pointeron elements that aren't links or buttons.Put the mouse away Tab through every flow you ship. Each control must be reachable, show where focus is, and work with Enter and Space for buttons or Enter for links. Swap the element, then restyle Use <button type="button">for actions and<a href>for navigation. Move the look into CSS, so the element keeps its built-in behavior.Keep ARIA for what HTML can't say Add ARIA on top of native elements only for meaning HTML lacks, such as aria-pressedon a toggle button oraria-expandedon a button that opens a panel.
Key takeaways
ARIA changes only the accessibility tree: role, name and state. It adds no behavior. role="button"on adivleaves out focus, Enter and Space activation anddisabled; you'd have to script each one.A native <button type="button">brings all of it, and standard HTML controls meet WCAG 4.1.2 when used as specified.Follow the first rule of ARIA: use the native element, and add ARIA only for what HTML can't express. Test with the keyboard alone: a control that announces "button" proves nothing about whether it works.
التعليقات (0)