CodePark/ questions / Forms & inputs
START PRACTICINGPRACTICE
Frontend question · CSS & HTML › Forms & inputs

Which markup correctly associates a label with its input?

<label for="email">Email</label> <input id="email">

The label's for must match the input's id, not its name.

The version with
for="email" with only name="email" on the input is the tempting wrong answer. for never looks at name, so the two are not linked. The <label name=...> version has no for at all, and aria-for does not exist.

Wrapping the input inside the
label also works. A linked label gives the input its accessible name, which screen readers announce. Clicking the label also focuses the input, so the clickable area gets bigger.