References

Beginner-friendly references for web development, with live, editable examples.

The HTML for attribute

Attribute All modern browsers
In short

The HTML for attribute associates the label (or output) with a form control by its id. It is used on the <label> and <output> elements.

Overview

The for attribute links a label to its form control by id. It is used on the <label> and <output> elements.

It is a form-control attribute: it configures how a control behaves, what it accepts, or its initial value, working alongside the control's <label> and parent <form>. Constraint attributes also feed the browser's built-in validation.

Syntax

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

Values

Value
A single element id (on <label>) or a space-separated list of ids (on <output>).

Best practices

  • Aim for at a labelable element, such as an <input> other than type hidden, <select>, <textarea>, <button>, <meter>, <output> or <progress>. Pointed at a <div> or a hidden input, it leaves the label with no control.
  • A for value can't reach into another shadow tree, because the label looks for the id in its own document or shadow root. If the id repeats, the earliest element with it is used, and if that one can't be labeled, the label gets no control.
  • A label that wraps one control can't name a different one in for. That markup is invalid, and the wrapped control gets no label from it.
  • On <output>, for takes a space-separated list of ids for the fields that fed the result. It records that relationship but doesn't name the output, so give the output its own <label> if it needs one.
  • The script property is htmlFor, a string on a label and a DOMTokenList on an output. To see which element a label resolved to, check label.control, which is null when nothing matched.

Accessibility

A <label for> pointing to an input's id gives the control an accessible name and makes the label clickable, a fundamental accessibility pattern.

Frequently asked questions

What does the for attribute do?
Links a label to its form control by id.
Are HTML form attributes enough for validation?
They give instant feedback, but client-side checks can be bypassed, so always validate on the server too.
Do form controls still need a label?
Yes. Every control needs a <label> for an accessible name, whatever attributes you set.
Which elements use the for attribute?
It is an element-specific attribute, used on the <label> and <output> elements.