Example
An simple HTML form with two input fields and related labels :<form> <label for="male">Male</label> <input type="radio" name="sex" id="male" /> <br /> <label for="female">Female</label> <input type="radio" name="sex" id="female" /> </form> |
Definition and Usage
The <label> tag defines a label for an input element.The label element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the label element, it toggles the control.
The for attribute of the <label> tag should be equal to the id attribute of the related element to bind them together.
Browser Support
The <label> tag is supported in all major browsers.
Note: Not supported in Safari 2 or earlier versions.
Differences Between HTML and XHTML
NONEOptional Attributes
DTD indicates in which HTML 4.01/XHTML 1.0 DTD the attribute is allowed. S=Strict, T=Transitional, and F=Frameset.Attribute | Value | Description | DTD |
---|---|---|---|
for | element_id | Specifies which form element a label is bound to | STF |
Standard Attributes
The <label> tag supports the following standard attributes:Attribute | Value | Description | DTD |
---|---|---|---|
accesskey | character | Specifies a keyboard shortcut to access an element | STF |
class | classname | Specifies a classname for an element | STF |
dir | rtl ltr | Specifies the text direction for the content in an element | STF |
id | id | Specifies a unique id for an element | STF |
lang | language_code | Specifies a language code for the content in an element | STF |
style | style_definition | Specifies an inline style for an element | STF |
title | text | Specifies extra information about an element | STF |
xml:lang | language_code | Specifies a language code for the content in an element, in XHTML documents | STF |
Event Attributes
The <label> tag supports the following event attributes:Attribute | Value | Description | DTD |
---|---|---|---|
onblur | script | Script to be run when an element loses focus | STF |
onclick | script | Script to be run on a mouse click | STF |
ondblclick | script | Script to be run on a mouse double-click | STF |
onfocus | script | Script to be run when an element gets focus | STF |
onmousedown | script | Script to be run when mouse button is pressed | STF |
onmousemove | script | Script to be run when mouse pointer moves | STF |
onmouseout | script | Script to be run when mouse pointer moves out of an element | STF |
onmouseover | script | Script to be run when mouse pointer moves over an element | STF |
onmouseup | script | Script to be run when mouse button is released | STF |
onkeydown | script | Script to be run when a key is pressed | STF |
onkeypress | script | Script to be run when a key is pressed and released | STF |
onkeyup | script | Script to be run when a key is released | STF |