Summary
The HTML Label Element (<label>
) represents a caption for an item in a user interface. It can be associated with a control either by placing the control element inside the <label>
element, or by using the for
attribute. Such a control is called the labeled control of the label element.
- Content categories Flow content, phrasing content, interactive content, form-associated element, palpable content.
- Permitted content Phrasing content, but no descendant
label
elements. No labelable elements other than the labeled control are allowed. - Tag omission {{no_tag_omission}}
- Permitted parent elements Any element that accepts phrasing content.
- DOM interface {{domxref("HTMLLabelElement")}}
Attributes
This element includes the global attributes.
- {{htmlattrdef("accesskey")}} {{HTMLVersionInline("4")}} {{HTMLVersionInline("5")}}
- A shortcut key to access this element from the keyboard.
- {{htmlattrdef("for")}}
- The ID of a labelable form-related element in the same document as the label element. The first such element in the document with an ID matching the value of the
for
attribute is the labeled control for this label element.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. - {{htmlattrdef("form")}} {{HTMLVersionInline("5")}}
- The form element that the label element is associated with (its form owner). If specified, the value of the attribute must be the ID of a {{HTMLElement("form")}} element in the same document. This attribute enables you to place label elements anywhere within a document, not just as descendants of their form elements.
Examples
Simple label example
<label>Click me <input type="text" name="username"></label>
{{ EmbedLiveSample('Simple_label_example', '200', '50', '') }}
Using the "for" attribute
<label for="username">Click me</label> <input type="text" id="username" name="username">
{{EmbedLiveSample('Using_the_.22for.22_attribute', '200', '50', '')}}
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', 'forms.html#the-label-element', '<label>')}} | {{Spec2('HTML WHATWG')}} | |
{{SpecName('HTML5 W3C', 'forms.html#the-label-element', '<label>')}} | {{Spec2('HTML5 W3C')}} | |
{{SpecName('HTML4.01', 'interact/forms.html#h-17.9.1', '<label>')}} | {{Spec2('HTML4.01')}} |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}}[1] | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}}[1] | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
[1] Starting with Gecko 8.0 {{geckoRelease("8.0")}}, a bubbling click event triggers at most one <label>
, and the synthetic click event cannot trigger additional <label>
s. In Gecko, a click event will still bubble up past a <label>
, while in WebKit or Internet Explorer the click event will stop at the <label>
. The behavior prior to Gecko 8.0 (triggering multiple <label>
s) caused Firefox to stop responding (see {{bug(646157)}}).
See also
- Other form-related elements: {{HTMLElement("form")}}, {{HTMLElement("button")}}, {{HTMLElement("datalist")}}, {{HTMLElement("legend")}}, {{HTMLElement("select")}}, {{HTMLElement("optgroup")}}, {{HTMLElement("option")}}, {{HTMLElement("textarea")}}, {{HTMLElement("keygen")}}, {{HTMLElement("fieldset")}}, {{HTMLElement("output")}}, {{HTMLElement("progress")}} and {{HTMLElement("meter")}}.