Accessibility

Topic 3 of 5 in Production patterns9 min readIntermediateFree course

Overview

Accessibility (often shortened to a11y) means people can use your app regardless of how they interact with it: with a screen reader, a keyboard only, voice control, high zoom, or reduced motion. It’s also often a legal requirement — and it makes apps better for everyone. React doesn’t make apps accessible or inaccessible by itself; the HTML you render does. The good news: most of it comes from choices you’ve already been learning.

Semantic HTML first

Browsers and assistive technologies build an accessibility tree from your HTML. Semantic elements come with roles, names, focus and keyboard behaviour built in:

Your JSX compared with what assistive technology sees. A div with an onClick and the text Save becomes a generic node: not focusable, no role, no keyboard. A button with the text Save becomes a button named Save: Tab-focusable, Enter and Space work. An img with no alt becomes an image with no name, read as “chart.png” or skipped. An img with alt “Sales rose 12% in May” conveys the information. Semantic HTML gives you roles, names, focus and keyboard support for free; ARIA fills the remaining gaps.

JSX
// ✗ A div pretending to be a button
<div className="btn" onClick={save}>Save</div>

// ✓ A real button: focusable, keyboard-operable, announced as "Save, button"
<button type="button" onClick={save}>Save</button>

Rules of thumb:

  • Actions → <button>. Navigation → <a href> (or a router <Link>).
  • Structure → headings in order (h1 → h2 → h3), lists for lists, tables for tabular data.
  • Landmarks → <header>, <nav>, <main>, <aside>, <footer> let screen-reader users jump around the page.
  • One <h1> per page describing it.

Accessible names

Every interactive element needs a name that says what it does:

JSX
<button>Enrol</button>                                   // name from its text

<button aria-label="Close dialog">✕</button>             // icon-only: name it explicitly

<label htmlFor="email">Email</label>
<input id="email" type="email" />                        // name from its label

<input aria-label="Search courses" type="search" />      // when a visible label isn't wanted
  • Placeholders are not labels — they disappear as soon as you type.
  • Use useId() to generate ids for htmlFor/aria-describedby in reusable components.
  • Hide decorative icons from assistive tech with aria-hidden="true".

Images

JSX
<img src={chart} alt="Enrolments grew from 120 to 410 between January and June" />  // informative
<img src={divider} alt="" />                                                          // decorative

Write alt text that conveys what the image means in context. An empty alt="" tells screen readers to skip a purely decorative image; leaving alt out entirely makes some read the file name.

Keyboard support

Many people navigate with Tab, Shift+Tab, Enter, Space and arrow keys. Test every flow without a mouse:

  • Everything you can click must be reachable by Tab and operable from the keyboard. Native buttons and links already are.
  • Never remove focus outlines without a replacement. Style them with :focus-visible, which shows only for keyboard focus:
CSS
:focus-visible { outline: 3px solid #2d5bd7; outline-offset: 2px; }
  • Tab order should follow the visual order. Avoid positive tabIndex values; tabIndex={-1} makes an element focusable from code only.
  • Provide a “Skip to content” link at the top of the page for keyboard users.

Managing focus

When React changes what’s on screen, keyboard and screen-reader users can lose their place. Move focus deliberately:

  • After route changes, focus the new page’s heading, so users hear where they are:
JSX
function PageHeading({ children }) {
  const ref = useRef(null);
  useEffect(() => {
    ref.current?.focus();
  }, []);
  return <h1 ref={ref} tabIndex={-1}>{children}</h1>;
}
  • Dialogs move focus in and return it on close (the native <dialog> does this — see Portals and modals).
  • After deleting an item, move focus to the next item or the list heading, not into the void.
  • After a form error, focus the first invalid field.

Announcing changes

Screen readers announce what has focus. Updates elsewhere — “Saved”, “3 results”, “Added to cart” — go unnoticed unless you put them in a live region:

JSX
<p role="status">{resultCount} courses found</p>         // polite: announced when the user pauses
<p role="alert">{error}</p>                               // assertive: interrupts — for errors only

The live region must already be in the DOM before its text changes; render the element always and change only its content. A visually hidden class keeps announcements out of sight but available to screen readers:

CSS
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

ARIA: when HTML isn’t enough

ARIA attributes add roles, states and properties that HTML can’t express:

JSX
<button aria-expanded={isOpen} aria-controls="faq-1" onClick={toggle}>What’s included?</button>
<div id="faq-1" hidden={!isOpen}>…</div>

<button aria-pressed={isFavourite}>Favourite</button>

<nav aria-label="Course chapters">…</nav>

<div role="progressbar" aria-valuenow={60} aria-valuemin={0} aria-valuemax={100} />

The first rule of ARIA: if a native element does the job, use it instead. ARIA changes what’s announced, but adds no behaviour — role="button" on a <div> doesn’t make it focusable or respond to Enter. Complex widgets (comboboxes, menus, tabs, date pickers) need careful keyboard handling; use a well-tested library like React Aria or Radix UI for them.

Colour, motion and text

  • Text needs enough contrast (WCAG AA: 4.5:1 for normal text, 3:1 for large text).
  • Don’t rely on colour alone — a red border needs an error message; a green dot needs a “New” label.
  • Respect prefers-reduced-motion for animations (see View transitions).
  • Make sure layouts work at 200% zoom and with larger text.

Checking your work

  1. Lint as you type — eslint-plugin-jsx-a11y flags missing alt, click handlers on non-interactive elements, invalid ARIA and more.
  2. Use Testing Library’s role queries — if getByRole('button', { name: 'Enrol' }) can’t find your button, neither can a screen reader.
  3. Run automated checks — the axe DevTools browser extension or Lighthouse’s accessibility audit catch many common issues. You can also run axe in tests (vitest-axe, @axe-core/playwright).
  4. Test manually — unplug the mouse and use Tab; try VoiceOver (macOS/iOS), NVDA (Windows) or TalkBack (Android) on your main flows.

Automated tools find only part of the problems — common estimates range from about a third to a half — so keyboard and screen-reader testing remain essential.

Accessible by default: a real <button> is less code than a div with role, tabIndex and key handlers — and it works better. Most accessibility wins come from using the right element.

Exercise

Fix an inaccessible course card

This CourseCard is a clickable <div>, has an icon-only favourite button, an image with no alt text, and a “New!” badge shown only by colour. Rewrite it so that: the title is a real link to the course; the favourite button has an accessible name that reflects its state (aria-pressed); the image has meaningful alt text (or empty alt if decorative); and the card announces “Added to favourites” / “Removed from favourites” in a live region when toggled.

JavaScript
import { useState } from 'react';

export function CourseCard({ course }) {
  const [fav, setFav] = useState(false);
  return (
    <div className="card" onClick={() => (location.href = `/courses/${course.slug}`)}>
      <img src={course.image} />
      <div className="title">{course.title}</div>
      {course.isNew && <span className="badge-green" />}
      <div className="icon-btn" onClick={(e) => { e.stopPropagation(); setFav(!fav); }}>♥</div>
    </div>
  );
}

Summary

  • Use semantic HTML — <button>, <a>, <label>, headings, landmarks — before reaching for ARIA.
  • Every interactive element needs an accessible name; every image needs alt (empty for decorative images).
  • Everything must work with a keyboard, with a visible focus indicator.
  • Manage focus when the UI changes: after navigation, opening dialogs and deleting items.
  • Announce dynamic updates with live regions, and check your work with linting, axe and a screen reader.