React / 96
Accessibility: semantic HTML first
Use the right HTML element and your app works with a keyboard and a screen reader for free. Buttons, links, labels, alt text and page structure.
What a screen reader sees
Browsers build an accessibility tree from your HTML. Every interactive thing in it has three facts:
| Fact | Example | Comes from |
|---|---|---|
| Role: what it is | button, link, checkbox, heading | the element: <button>, <a href>, <input type="checkbox">, <h2> |
| Name: what it's called | "Add to cart" | its text, its <label>, or its alt |
| State: how it is now | checked, disabled, expanded | attributes like checked and disabled |
<button>Add to cart</button> is announced as "Add to cart, button". The same text in a <div> is just "Add to cart", and nobody knows it can be pressed.
Buttons are buttons
Try it without a mouse:
Put the mouse away
Try itfocus: nothing in this demo
<div onClick>
Looks like a button. Isn't one.
mouse 0 · keyboard 0
<div role tabIndex onKeyDown>
Patched by hand to act like one.
mouse 0 · keyboard 0
<button>
The real thing. Zero extra code.
mouse 0 · keyboard 0
Don't
A div pretending
<div className="btn" onClick={addToCart}>
Add to cart
</div>Do
A real button
<button type="button" className="btn" onClick={addToCart}>
Add to cart
</button>A <button> is focusable with Tab, fires onClick on Enter and Space, has the button role, and respects disabled. Making a <div> match needs role="button", tabIndex={0} and key handling, and it's still easy to get wrong. Need it to look different? Style the button. CSS can make it look like anything.
Button or link? If it takes you somewhere (a new URL), it's a link: <a href>, or the router's <Link>, which renders one. If it does something on this page, it's a <button>. Screen reader users rely on the difference: links are listed separately, and they expect a link to change the page.
Every input needs a label
import { useId } from "react";
export function EmailField() {
const id = useId();
return (
<div>
<label htmlFor={id}>Email</label>
<input id={id} type="email" autoComplete="email" />
</div>
);
}htmlFor (JSX's name for for) links the label to the input's id. Now a screen reader says "Email, edit text", and clicking the label focuses the input. useId makes an id that's unique even if the component appears twice. Wrapping works too: <label>Email <input type="email" /></label>.
A placeholder is not a label. It disappears as soon as you type, it's often low contrast, and some screen readers skip it.
Alt text for images
| Image | alt | Why |
|---|---|---|
| Shows information (a dish photo on a menu) | alt="Masala dosa with chutney" | Say what a sighted user learns from it |
| Pure decoration (a background swirl) | alt="" | Empty means "skip me" |
| The only content of a link or button | alt="Go to cart" | Describe the action, not the picture |
Never leave alt out: some screen readers then read the file name, like "IMG underscore 4032 dot jpg".
Structure with headings and landmarks
Screen reader users jump around a page the way you skim it: by headings and regions.
- Wrap areas in landmarks:
<header>,<nav>,<main>,<footer>. Each can be reached with one shortcut. - Use one
<h1>, then<h2>,<h3>in order. Pick a level by structure, never for its size; change the size with CSS. - Lists are
<ul>/<ol>with<li>, so a screen reader says "list, 12 items".
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
