The HTML <label>
element represents a caption for an item in a
user interface.
Associating a <label>
with an input element offers some major
advantages:
- The label text is not only visually associated with its corresponding text input; it is programmatically associated with it too. This means that, for example, a screenreader will read out the label when the user is focused on the form input, making it easier for an assistive technology user to understand what data should be entered.
- You can click the associated label to focus/activate the input, as well as the input itself. This increased hit area provides an advantage to anyone trying to activate the input, including those using a touch-screen device.
To associate the <label>
with an <input>
element, you need to give
the <input>
an id
attribute. The <label>
then needs a for
attribute whose value is the same as the input's id
.
Alternatively, you can nest the <input>
directly inside the <label>
,
in which case the for
and id
attributes are not needed because the
association is implicit:
<label>Do you like peas?
<input type="checkbox" name="peas">
</label>
Attributes
This element supports the global attributes as well as the following element-specific attributes:
for
: StringThe
id
of a labelable form-related element in the same document as the<label>
element. The first element in the document with anid
matching the value of thefor
attribute is the labeled control for this label element, if it is a labelable element. If it is not labelable then thefor
attribute has no effect. If there are other elements which also match theid
value, later in the document, they are not considered.Note: A
<label>
element can have both afor
attribute and a contained control element, as long as thefor
attribute points to the contained control element.form
: String
Usage notes
- The form control that the label is labeling is called the labeled control of the label element. One input can be associated with multiple labels.
- Labels are not themselves directly associated with forms. They are only indirectly associated with forms through the controls with which they're associated.
- When a
<label>
is clicked or tapped and it is associated with a form control, the resultingclick
event is also raised for the associated control.
Accessibility concerns
Interactive content
Don't place interactive elements such as
anchors
or
buttons
inside a label
. Doing so makes it difficult for people to activate the
form input associated with the label
.
Don't
<label for="tac">
<input id="tac" type="checkbox" name="terms-and-conditions">
I agree to the <a href="terms-and-conditions.html">Terms and Conditions</a>
</label>
Do
<label for="tac">
<input id="tac" type="checkbox" name="terms-and-conditions">
I agree to the Terms and Conditions
</label>
<p>
<a href="terms-and-conditions.html">Read our Terms and Conditions</a>
</p>
Headings
Placing heading elements within a
<label>
interferes with many kinds of assistive technology, because
headings are commonly used as a navigation aid. If the
label's text needs to be adjusted visually, use CSS classes applied to
the <label>
element instead.
If a form, or a section of a form
needs a title, use the
<legend>
element placed within a
<fieldset>
.
Don't
<label for="your-name">
<h3>Your name</h3>
<input id="your-name" name="your-name" type="text">
</label>
Do
<label class="large-label" for="your-name">
Your name
<input id="your-name" name="your-name" type="text">
</label>
Buttons
An <input>
element with a type="button"
declaration and a valid value
attribute
does not need a label associated with it. Doing so may actually
interfere with how assistive technology parses the button input. The
same applies for the
<button>
element.
Examples
Simple example
HTML
<label>Click me <input type="text"></label>
Result
Using the "for" attribute
HTML
<label for="username">Click me</label>
<input type="text" id="username">
Result
Desktop | Mobile | |||
---|---|---|---|---|
Chrome | Edge | Chrome | Edge | |
video | 3 | Yes | ? | No |
See also
Other form-related elements:
AutoSponge, mdnwebdocs-bot, mfuji09, wbamberg, jswisher, boxcleverliam, chrisdavidmills, Foluso, OakAsteroid, SphinxKnight, ericwbailey, obyford, rogerpence, bradleyflood, teoli, david_ross, danielcompton, sideshowbarker, erikadoyle, Sheppy, Krischan, GunnarSturla, vitaly-zdanevich, Sebastianz, marfire, javier1nc, SuperGeekJake, Jeremie, liborvalenta, mkolodny, mikemaccana, kscarfone, torbasow, ethertank, tregagnon, tshinnic, zuzusik, medicdude, metusf, fscholz, chrisjshull, cers, karbassi, McGurk, osdm, Crash