The HTML for attribute
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
forat 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
forvalue 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>,
fortakes 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, checklabel.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?
Are HTML form attributes enough for validation?
Do form controls still need a label?
Which elements use the for attribute?
<label> and <output> elements.






















