More HTML answers

by Danny
Published 3 min read

Clicking a label should focus or toggle its control. Learn why the for and id link breaks and how to fix it with matching ids, nesting or htmlFor in React.

The short answer

#

A <label> is only linked to a control when its for attribute matches the control's id exactly, or when the control is nested inside the label. If neither is true, clicking the label does nothing. Check that the id exists, is unique on the page and matches for letter for letter.


_10
<label for="email">Email</label>
_10
<input id="email" name="email" type="email" />

Why it happens

#

When a label is associated with a form control, the browser forwards clicks on the label to the control. Text inputs get focus, checkboxes and radio buttons toggle, and screen readers read the label text as the control's name. That association is made in one of two ways: explicitly, with for pointing at an id, or implicitly, by putting the control inside the label.

The link breaks quietly. The browser does not warn you when for points at nothing, so the label looks fine but behaves like plain text. The usual causes are:

  • for matches the control's name instead of its id.
  • A typo or a difference in case, such as for="Email" and id="email".
  • The same id is used twice on the page, often because a component is rendered more than once. The label links to the first match, which may be a different field.
  • The id is on a wrapper <div> rather than on the input itself.
  • The target is not a labelable element. Only input (except type="hidden"), select, textarea, button, meter, output, progress and form-associated custom elements can be labelled.
  • In React, writing for instead of htmlFor.

How to fix it

#

Match for to a unique id

Put the id on the control itself and use the same value in for. Make sure no other element on the page shares it. You can check in the console:


_10
document.querySelectorAll("#email").length; // should be 1
_10
document.querySelector("label[for='email']").control; // should be the input

The control property on a label returns the element it is linked to, or null if the link is broken. It is a quick way to test any label.

Nest the control inside the label

Wrapping the control removes the need for an id, which helps when you cannot guarantee a unique one. It is common for checkboxes and radio buttons.


_10
<label>
_10
<input type="checkbox" name="newsletter" />
_10
Send me the monthly newsletter
_10
</label>

Both methods are valid. Some older assistive technology handled explicit for and id more reliably, so many teams use both together: nest the input and add a matching for and id.

Generate unique ids in React with useId

In React, the attribute is htmlFor. For reusable components, use the useId hook so every instance gets its own id, which also matches between server and client rendering.


_11
import { useId } from "react";
_11
_11
function TextField({ label, ...props }: { label: string } & React.ComponentProps<"input">) {
_11
const id = useId();
_11
return (
_11
<>
_11
<label htmlFor={id}>{label}</label>
_11
<input id={id} {...props} />
_11
</>
_11
);
_11
}

Avoid hard-coded ids such as id="email" inside components that may appear more than once, like a sign-up form in both the header and the footer.

Check for CSS or overlays blocking the click

If the association is correct and it still does not work, something may be intercepting the click. An absolutely positioned element on top of the label, or pointer-events: none on the label, will stop the click from reaching it. Inspect the element under the pointer in your browser's developer tools.

Common pitfalls

#
  • Putting more than one control inside a single label. The label links to the first one only.
  • Nesting a link or button inside a label. Clicks on it may activate the label's control too, which is confusing. Keep interactive content out of labels.
  • Using aria-label instead of a visible label. It names the field for screen readers but gives sighted users nothing to click and nothing to read once they start typing.
  • Using a placeholder as the label. It disappears on input and is not a replacement for a <label>.
  • Styling a custom checkbox by hiding the input with display: none. The label may still toggle it, but the control can no longer receive keyboard focus. Hide it visually instead, keeping it in the accessibility tree.
#

Was this resource helpful?