A browser's built-in button (<button>) can take focus with Tab, be pressed with Enter or Space, and be announced by a screen reader as a button. A home-made button built from a <div> can do none of that until you write it all yourself.
Think of the light switch in Mrs. Sri's rooms. Every guest understands a standard switch right away: press it, and the light comes on. A home-made touch panel may look nicer, but Mrs. Sri has to explain it to each guest, and a guest who cannot see does not know the panel is there. On the web, native HTML elements are the standard switch. A component built from a <div> with ARIA and JavaScript is the home-made panel: everything Mrs. Sri explains to her guests, you have to write yourself as code.
An accessible component is one anyone can use: with a mouse, with the keyboard alone, or with a screen reader. It also tells the screen reader its role (what it is), its name (what it is called) and its state (how it is right now).
Three principles
Building a component to be accessible from the start is far cheaper than fixing it at the end, after it is used on dozens of pages. There are three principles, in this order:
- Semantic HTML first. If a native element has the role and behaviour you need (
<button>,<input type="radio">,<dialog>,<details>), use it. - ARIA only when needed. ARIA changes the accessibility tree, not behaviour (the lesson ARIA Roles & States Deep Dive). Use ARIA for what has no HTML element, such as photo tabs.
- Keyboard always. Every component must work with Tab, Enter, Space, Escape and the arrow keys, exactly as the ARIA Authoring Practices Guide (APG, W3C's guide to building accessible components) writes for its pattern.
Here are the five components of the Mist Homestay booking page, the same as the Lab on this page. The "Role, name, state" column is read from Chromium 147's accessibility tree:
| Component | Start from | Role, name, state | APG pattern |
|---|---|---|---|
| Night choice | <fieldset> + <input type="radio"> |
group "Length of stay", radio "2 nights" checked=true |
Radio Group |
| Room photos | ARIA: tablist, tab, tabpanel |
tablist "Terrace Room photos", tab "Room" selected=true |
Tabs |
| Booking confirmation | <dialog> + showModal() |
dialog "Book the Terrace Room?" modal=true |
Dialog (Modal) |
| "Booking sent" news | role="status" |
status live=polite atomic=true |
(live region, WCAG 4.1.3) |
| Cancellation policy | <details> + <summary> |
DisclosureTriangle "Cancellation policy" expanded=false |
Disclosure (Show/Hide) |
The night choice: start from real radios
Say Mrs. Sri's team builds the 1, 2, 3 nights choice from three <div>s, then adds role="radio" and aria-checked. Mr. Hadi presses Tab from the button above it. Does focus stop on the night choice? Guess first, then compare with the real radio version below:
<fieldset class="nights">
<legend>Length of stay</legend>
<label><input type="radio" name="nights" value="1"> 1 night</label>
<label><input type="radio" name="nights" value="2" checked> 2 nights</label>
<label><input type="radio" name="nights" value="3"> 3 nights</label>
</fieldset>
<legend>names the group,<label>names each radio, andcheckedbecomes the statechecked=true.- The shared
namemakes the three one group: one Tab stop, and the arrow keys choose.
This is the keyboard behaviour per the APG (Radio Group Pattern), and the browser does all of it:
- Tab goes to the checked radio.
- Right or Down Arrow moves to the next radio and checks it. Left or Up goes back.
- Space checks the radio that has focus.
The APG says the arrow from the last radio goes back to the first. Chromium and Firefox do that, but WebKit 26.4 stops at 3 nights.
The <div role="radio" aria-checked="true"> version gives a similar tree (radio "2 nights" checked=true), but without the focusable mark (can take focus). In Chromium, WebKit and Firefox, Tab from the button above it jumps straight to the booking button. So the answer to the guess: no. A role is only a promise. To make that div work, you still have to write tabindex, a roving tabindex, the arrow keys and Space yourself. A real radio gives all of it for free.
Room photos: tabs, because HTML has no tab element
There is no native element for the photo tabs (Room, Bathroom, Terrace), so this is where ARIA belongs. The full markup is in the lesson ARIA Roles & States Deep Dive, and the arrow-key script is in Keyboard Navigation Patterns. In short:
- Roles:
role="tablist"witharia-label="Terrace Room photos". Each tab is a<button role="tab">witharia-selectedandaria-controls. Each panel usesrole="tabpanel"witharia-labelledbyandtabindex="0". - Keyboard (APG Tabs Pattern): pressing Tab goes to the active tab, and pressing Tab once more goes to its panel. Right and Left Arrow move between tabs and show the photo right away, going round at the ends. Home and End go to the first and last tab.
- Roving tabindex: the active tab has
tabindex="0", the rest-1. Your script updatesaria-selected,tabindexandhiddenevery time the tab changes.
Confirmation: dialog with showModal()
When the booking button is pressed, this confirmation dialog opens with showModal():
<button class="book" id="book">Book the room, pay on arrival</button>
<dialog id="confirm" aria-labelledby="confirm-title">
<form method="dialog">
<h2 id="confirm-title">Book the Terrace Room?</h2>
<p id="summary">2 nights · $70, pay on arrival</p>
<button value="cancel">Back</button>
<button value="book">Yes, book</button>
</form>
</dialog>
This is the keyboard behaviour per the APG (Dialog (Modal) Pattern):
- When the dialog opens, focus moves into it.
- Tab and Shift+Tab go round inside the dialog.
- Escape closes the dialog.
- Once the dialog is closed, focus goes back to the button that opened it.
showModal() does all four (the lesson Modals & Overlays). In our test, Enter on the booking button moved focus to Back. Escape closed the dialog with an empty returnValue (the value of the button that closed it), and focus went back to "Book the room, pay on arrival".
When a team builds its own modal in React, that last step is the one most often missed: focus falls to the top of the page, and Mr. Hadi has to Tab from the start again. Save the element that has focus (document.activeElement) before the modal opens, and focus it again when the modal closes. In React this code goes inside useEffect, a React function that runs code every time open (whether the modal is open) changes:
useEffect(() => {
if (!open) return;
const before = document.activeElement; // the book button
modalRef.current.focus();
return () => before?.focus(); // runs when the modal closes
}, [open]);
aria-modal="true" does not do this. It only tells a screen reader that the page behind the modal is inactive.
"Booking sent" news: a live region
The "Booking sent" toast uses a live region. Its element is already there, empty, when the page loads, and then the function announce() fills in the text:
<div class="toast" role="status"></div> <!-- there from page load, empty -->
<script>
function announce() {
document.querySelector('.toast').textContent = 'Booking sent. Mrs. Sri will confirm on WhatsApp.';
}
</script>
A toast never takes focus and has no keys of its own. What matters is its role. role="status" means aria-live="polite" and aria-atomic="true", so the whole sentence is read once the screen reader finishes speaking, with no focus moving (WCAG 4.1.3 Status Messages). The region must exist before its text is filled in. A toast made from a plain <div> looks exactly the same, but stays silent for a screen reader.
The same goes for a button that is only an icon: what matters is what is in the tree. While the booking is being sent, the booking button shows an hourglass icon:
<button class="book" aria-disabled="true">
<svg aria-hidden="true" viewBox="0 0 24 24">...</svg>
<span class="sr-only">Processing...</span>
</button>
aria-disabled="true"marks the button as disabled (can't be pressed yet) in the tree.aria-hidden="true"takes the decorative icon out of the tree..sr-only(a class that turns the text into a 1px box cut off withclip) hides text from the eyes, not from a screen reader.
Chromium gives button "Processing..." disabled=true. Without that span the name is empty: button "".
Cancellation policy: disclosure with details
A disclosure is a part of the page that opens and closes. For the cancellation policy, <details> is enough:
<details class="policy">
<summary>Cancellation policy</summary>
<p>Free cancellation up to 3 days before check-in. Cancelling later than that? Tell Mrs. Sri on WhatsApp.</p>
</details>
Keyboard per the APG (Disclosure (Show/Hide) Pattern): Enter or Space opens and closes it. <summary> already takes focus, and the browser itself updates the expanded state. The ARIA version, <button aria-expanded="false" aria-controls="...">, is right too, but your script has to update its state.
Checked in three browsers
We ran this Lab's page, in its div version and its accessible version, in Chromium 147, WebKit 26.4 (Tab with Option, as in the lesson Keyboard Navigation Patterns) and Firefox 146:
| Div version | Accessible version | |
|---|---|---|
| Tab stops at | 1 place: the book button | 5 places: the active tab, the panel, the checked radio, Book, the cancellation policy |
| Arrows on the photo tabs and the night choice | nothing happens | move and choose |
| Escape in the confirmation | the box stays open | closes, focus goes back to Book |
| Enter and Space on the policy | cannot take focus | opens and closes |
| axe-core 4.14 | 0 violations | 0 violations |
The last row matters: axe finds nothing in the div version. The lesson Automated a11y Testing explains why: automated tools do not test keyboard behaviour.
A checklist per component
Before a component goes into the component library, check these:
- Role, name, state are right in the DevTools Accessibility tab, and the state changes as the component is used.
- Keyboard follows its APG pattern: Tab, Enter, Space, Escape, arrows.
- Focus is visible (WCAG 2.4.7) and not hidden behind elements that stay fixed on screen, such as a header that stays at the top when you scroll (2.4.11).
- Contrast of text at least 4.5:1 (large text 3:1), and component borders and the focus ring at least 3:1 (1.4.11).
- 200% zoom and a 320px width still work (1.4.4, 1.4.10).
- A screen reader announces state changes and news (the lesson Screen Reader Testing).
📌 Libraries that help
If you would rather not write the APG patterns yourself, there are unstyled libraries (no look of their own, only behaviour), such as Radix UI, React Aria (Adobe) and Headless UI (the Tailwind team). They already handle the roles, states and keyboard. Still run the checklist above: a library only helps when you use it as its docs say. For example, you still have to give a name to a dialog and to an icon button.
⚠️ Components that only look finished
<div onClick>with ARIA added later. The role adds a promise to the tree, but focus, the arrow keys and Space are still missing.- State that does not change along. An
aria-selectedoraria-expandednobody updates makes the screen reader report the wrong state. - A home-made modal that forgets to give focus back. Save
document.activeElementbefore opening, and focus it again on close. Or use<dialog>. - A live region created together with its text. The region must already be on the page before the text is filled in.
- An icon with no name. Hide the icon with
aria-hidden="true", and give the button.sr-onlytext or anaria-label.
TL;DR: There are three principles, in this order: semantic HTML first, ARIA only when needed, keyboard always. Every component needs the right role, name and state in the accessibility tree, and the keyboard behaviour of its APG pattern (the APG is W3C's guide to building accessible components). Night choice: <fieldset>, <legend>, <input type="radio"> (Radio Group: Tab to the checked radio, arrows move and check). A <div role="radio"> cannot be reached by Tab. Room photos: tablist, tab with aria-selected, tabpanel, a roving tabindex (only the active tab is in the Tab order), arrows and Home/End (Tabs). Confirmation: <dialog> + showModal() with aria-labelledby (Dialog (Modal): focus moves in, Tab stays inside, Escape closes, focus comes back). A home-made modal must save document.activeElement (the element that has focus) and focus it again on close. News: a role="status" that is there from the start (the whole sentence is read once the screen reader finishes speaking, WCAG 4.1.3). An icon in a button: aria-hidden="true" plus .sr-only text (text hidden from the eyes), so the button's name is "Processing...". Cancellation policy: <details>/<summary> (Disclosure: Enter and Space). All of it was tested in Chromium, WebKit and Firefox. axe gives 0 violations for both the div and the accessible version, so the keyboard still has to be tried by hand. Checklist: role, name, state; keyboard; visible focus; contrast 4.5:1 and 3:1; 200% zoom and 320px; a screen reader.
⚡ Three principles, in order
Guess first: your team wants a new dropdown component for the component library. What approach fits best?
Start from semantic HTML elements, add ARIA only when needed, and make sure the component always works with the keyboard.
Meaning: Native elements such as <button>, <input type="radio">, <dialog> and <details> already carry their own role, state and keyboard behaviour. ARIA is for what has no HTML element, such as the room's photo tabs.
⚡ The night choice from native radios
Write:
<fieldset class="nights">
<legend>Length of stay</legend>
<label><input type="radio" name="nights" value="1"> 1 night</label>
<label><input type="radio" name="nights" value="2" checked> 2 nights</label>
<label><input type="radio" name="nights" value="3"> 3 nights</label>
</fieldset>
Meaning: <legend> names the group, each <label> names its radio, and checked becomes its state. A version built from <div role="radio"> gives a similar accessibility tree but can't take focus. So Tab skips the night choice and jumps straight to the booking button below it.
⚡ A button that is only an icon
Write:
<button class="book" aria-disabled="true">
<svg aria-hidden="true" viewBox="0 0 24 24">...</svg>
<span class="sr-only">Processing...</span>
</button>
Meaning: While the booking is being sent, the booking button shows only an hourglass icon. aria-hidden="true" takes that decorative icon out of the accessibility tree, and aria-disabled="true" marks the button as disabled. .sr-only hides the text "Processing..." from the eyes, not from a screen reader. Chromium gives button "Processing..." disabled=true. Without that span the button has no name.