AI Hints
Home›React Tutorial›Quality
React · Lesson 80 of 120

Accessibility

Labels and roles.

⏱ 2 min read▶ Video at 1:07:47🎓 Free
Watch this lesson in the React courseWatch on YouTube ↗Full React course

Accessible apps work for everyone. People using screen readers, keyboards, or zoom. Most of it comes free, if you use the right HTML elements.

  • Works for everyone
  • Screen readers, keyboards
  • Right elements

An accessible product

Images get alt text, describing what they show. Labels are linked to inputs with html for, so clicking the label focuses the input. Real buttons work with the keyboard for free. A button showing just a plus sign gets an aria label. And role status reads changes aloud to screen reader users. We click plus twice. Run it.

App.jsxjsx
import { useState } from "react";
export default function App() {
  const [qty, setQty] = useState(1);
  return (
    <form className="box" onSubmit={e => e.preventDefault()}>
      <img src="bike.svg" alt="Trail 500, a blue mountain bike" width="160" /><br />
      <label htmlFor="qty">Quantity</label>{" "}
      <input id="qty" type="number" min="1" value={qty} onChange={e => setQty(Number(e.target.value))} style={{ width: 70 }} />
      <button type="button" id="more" aria-label="Add one more" onClick={() => setQty(qty + 1)}>+</button>
      <p role="status">{qty} in your basket</p>
    </form>
  );
}
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}
Also uses bike.svg
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="90" viewBox="0 0 200 90"><rect width="200" height="90" rx="12" fill="#61dafb"/><g fill="none" stroke="#111" stroke-width="5"><circle cx="55" cy="58" r="20"/><circle cx="145" cy="58" r="20"/><path d="M55 58 L85 28 L120 28 L145 58 M85 28 L100 58 L120 28"/></g></svg>
Also uses actions.json
[{"click": "#more", "times": 2}]

Quick checklist

Use buttons for actions, and links for navigation, never clickable divs. Every input needs a label. Every meaningful image needs alt text. And try using your app with only the keyboard.

keyvalue
Actions<button>
Navigation<a> or <Link>
Inputsa <label>
Testkeyboard only

Summary

Use the right elements, label inputs, and describe images. Next, project structure.

Key takeaways
  • alt
  • htmlFor
  • aria-label
Next up: Project Structure — folders and files