← Back to React, page 3

react-0060 · Easy fresher

What are the accessibility basics every React developer should apply, and why is semantic HTML better than adding ARIA to a div?

Use the right HTML element first: button for actions, a for navigation, label with input, headings in order, nav and main landmarks, and alt text on images. Native elements bring keyboard support, focus and roles for free. ARIA only adds meaning and never adds behaviour, so a div with role="button" still needs tabIndex, key handlers and styling. Use ARIA when no native element fits.

Full answer

Accessibility means people using keyboards, screen readers, zoom or voice control can use your app. In React it is mostly the same as in HTML, because JSX renders real elements.

A native <button> is focusable, activates with Enter and Space, announces itself as a button, and can be disabled. A <div onClick> has none of that. To imitate it you need role, tabIndex, key handlers and focus styles, and you will usually miss one. So choose the element by what it does: button for actions, <a href> for navigation, <input> with a <label> for fields, <ul> for lists, <table> for data.

Page structure matters. Use landmarks (header, nav, main, footer) so screen reader users can jump around, and keep heading levels in order. Give images meaningful alt text, or alt="" when they are decorative. Make sure text has enough colour contrast and the layout works at 200 percent zoom.

Keyboard use: every interactive element must be reachable with Tab in a sensible order, and the focus indicator must be visible. Avoid positive tabIndex values. Do not trap focus except in a modal.

ARIA fills gaps: aria-label names an icon-only button, aria-pressed shows a toggle state, aria-expanded shows an open menu, and aria-live announces updates. The first rule of ARIA is to use a native element when one exists.

In React, use htmlFor, aria-* attributes in kebab-case and tabIndex in camelCase. Lint with eslint-plugin-jsx-a11y and test with a screen reader as well.

jsx
import { useState } from 'react';

export default function Toolbar() {
  const [bold, setBold] = useState(false);
  const [menuOpen, setMenuOpen] = useState(false);

  return (
    <div>
      <header>
        <nav aria-label="Main">
          <a href="/courses">Courses</a>
        </nav>
      </header>
      <main>
        <h1>Notes editor</h1>
        <button aria-pressed={bold} onClick={() => setBold(!bold)}>
          Bold
        </button>
        <button aria-expanded={menuOpen} aria-controls="more" onClick={() => setMenuOpen(!menuOpen)}>
          More options
        </button>
        {menuOpen && <ul id="more"><li>Italic</li><li>Underline</li></ul>}
        <button aria-label="Delete note">
          <svg aria-hidden="true" width="12" height="12"><path d="M0 0L12 12" /></svg>
        </button>
        <img src="/chart.png" alt="Bar chart of marks by subject" />
        <label>
          Title <input />
        </label>
      </main>
    </div>
  );
}

Every control is a native element with a name. State is exposed with aria-pressed and aria-expanded, and the decorative icon is hidden. The check pressed each button once. Rendered result: <div><header><nav aria-label="Main"><a href="/courses">Courses</a></nav></header><main><h1>Notes editor</h1><button aria-pressed="true">Bold</button><button aria-expanded="true" aria-controls="more">More options</button><ul id="more"><li>Italic</li><li>Underline</li></ul><button aria-label="Delete note"><svg aria-hidden="true" width="12" height="12"><path d="M0 0L12 12"></path></svg></button><img alt="Bar chart of marks by subject" src="/chart.png"><label>Title <input></label></main></div>

Key points

  • Choose the element by purpose: button, a, label and input, lists, tables.
  • Native elements give keyboard behaviour, focus and role for free.
  • Landmarks, ordered headings and alt text structure the page.
  • ARIA adds meaning, not behaviour. Use it only when no native element fits.
  • Test with the keyboard, a linter and a screen reader.

Common mistakes

  • Using div or span with onClick for buttons.
  • Removing the focus outline without a replacement.
  • Skipping heading levels for visual size.
  • Putting aria-label on elements that already have visible text, which can conflict.
  • Adding ARIA roles that contradict the native role.

Follow-ups

  • What is the first rule of ARIA?
  • How would you make an icon-only button accessible?
  • How do you test accessibility automatically, and what can't tools catch?

Tips

  • Lead with "native element first". It is the best one-line answer.
  • Mention keyboard and focus, not only screen readers.
  • Name one linter or tool to show real practice.