label

< html‎ | elements
Jump to: navigation, search

label

This article is Almost Ready.


W3C Recommendation

Summary

Specifies a label for another element on the page.


Overview Table

DOM Interface HTMLLabelElement

The content of the label provides the caption for the input. The input can be specified in one of two ways:

  1. The input can be identified by the "for" attribute
  2. The input can be a child element of the label

User agents will often focus the cursor on the input element after clicking the associated label.

HTML Attributes

form = string
Associate the fieldset element with its form owner.
for = string
Specified to indicate a form control with which the caption is to be associated.
The attribute's value must be the ID of a labelable form-associated element in the same Document as the label element.


Compatibility

Do you think this data can be improved? You can ask to add by opening an issue or make a pull request.

Desktop

FeaturesChromeFirefoxInternet ExplorerOperaSafari
Basic support
?
Yes
?
Yes
?
Yes
?
Yes
?
Yes

Mobile

FeaturesAndroidFirefox MobileIE MobileOpera MobileSafari Mobile
Basic support
?
Yes
?
Yes
?
Yes
?
Yes
?
Yes

Examples

View live exampleThis example uses the LABEL element and the ACCESSKEY attribute to set focus on a text box.

HTML

<label for="oCtrlID" accesskey="1">
    #<span style="text-decoration:underline;">1</span>: Press Alt+1 to set focus to textbox
</label>
<input type="text" name="txt1" value="binding sample 1" 
       size="20" tabindex="1" id="oCtrlID"/>

Notes

Remarks

To bind a LABEL to another control, set the FOR attribute of the LABEL element equal to the ID of the control. Binding a LABEL to the NAME attribute of the control has no effect. However, to submit a form, you must specify a NAME on the control to which the LABEL element is being bound. There are two ways to underline the designated access key. The rich text support in the LABEL element makes it possible to wrap the U element around the character in the label text specified by the ACCESSKEY attribute. If you prefer to use cascading style sheets (CSS) to apply style formatting, enclose the designated character in a SPAN and set the style to "text-decoration: underline". If the user clicks the LABEL, the onclick event fires on the LABEL and then on the control specified by the htmlFor property. Pressing the access key for the LABEL sets the focus but does not fire the onclick event. Labels cannot be nested.


Related specifications

Specification Status Related Changes
HTML 5.1 W3C Working Draft
HTML 5 W3C Recommendation
HTML 4.01 W3C Recommendation

Attribution

This article contains content originally from external sources.

Portions of this content come from the Microsoft Developer Network: [Windows Internet Explorer API reference Article]