HTML Label Tag

The label tag gives a field a label. In the previous examples you have seen something like this:

Enter Name:

The "Enter Name" is just plain text, and the code for the above form is:

Enter Name:<input maxlength="8" name="name" type="text" />

With a label tag you can provide accessibility for users with disabilities. If you click on "Enter Name" above, nothing happens (you will probably just see your text cursor).

Label Tag

Now try it with the <label tag>:


<label for="id of field to link to">text</label>

The for attribute links the label to the field, it must be an ID. Thus, the field will need an ID attribute defined.


<label for="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname" maxlength="10"/>


Now if you click on the label the text box will get focus. This might or might not happen, as this depends on your browser. Most modern browsers will have this accessibility feature.