React Foundations · Accessibility

Labelling a form control

Accessible labels

Labelling a form control

An input with no label is a mystery box. A screen reader announces "edit text" and nothing more, so the person filling it in has no idea what it is for. The fix is a real <label> tied to the control. It also enlarges the click target and lets a test find the field by its name.

You make the association with htmlFor on the label and a matching id on the control:

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

Wrapping the control inside the label associates the two implicitly, so the id can go:

<label>
  Email
  <input type="email" />
</label>

Some controls have no visible text at all — an icon-only button, a search box with a magnifier. Give those an accessible name with aria-label:

<button aria-label="Search">
  <SearchIcon />
</button>

Either way, every control needs a name that says what it is for.

Your job is to associate a label with a text input.

Challenge

The word Email sits next to an input as plain text, so screen readers do not connect them. Export an App component from App.jsx that renders an Email label associated with a text input. You can tie them together with htmlFor and id, or by wrapping the input inside the label.

The Test finds the input by its label and expects it to be a text input. The starter shows the word Email as plain text, so nothing is associated and the Test fails.

App.jsx
export function App() {
  return (
    <div>
      <span>Email</span>
      <input type="email" />
    </div>
  );
}

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.