Skip to: content, navigation

by Ian Lloyd

for (HTML attribute)

Browser support full matrix
IE5.5+ FF1+ Saf3+ Op9.2+
Full Full Full Full
Spec
Depr. Version
No HTML 4
for="field ID"

Example

This for attribute points to a form control with the id of "b":

<input type="radio" name="radFriendship" value="Not_Applicable"
    id="b"/>
<label for="b" accesskey="b">Not_Applicable</label>

Description

The for attribute is the glue that binds the text contained inside the label element to the form control to which it relates. There’s another way that descriptive text can be linked to a form control: instead of using a for attribute, the opening <label> and closing </label> tags can be wrapped around the form control. In that instance, the example code would look like this:

<label accesskey="b"><input type="radio" name="radFriendship"
    value="Not_Applicable" id="b"/> Not_Applicable</label>

Finally, you could take the belt-and-braces approach and do both: wrap the label around the form control and link it using the for attribute:

<label for="b" accesskey="b"><input type="radio" 
    name="radFriendship" value="Not_Applicable" id="b"/> 
    Not_Applicable</label>

Note that the former method—using the for attribute—is by far the safer option (at least one screen reader, Window Eyes, is not able to make the correct association when the label is wrapped around the related text).

Value

This attribute takes as its value a reference to an existing (and valid) id of a form control.

Compatibility

IE5.5Full
6.0Full
7.0Full
Firefox1.0Full
1.5Full
2.0Full
Safari1.3None
2.0None
3.0Full
Opera9.2Full

The for attribute is more widely supported than the approach that involves simply wrapping the label around the control, and is the preferred method. It’s also more flexible, as the text and control may be separated by other HTML structural elements that would make it impossible to wrap the inline label around the control and the associated text.

User-contributed notes

There are no comments yet.

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters