These elements can be unsupported in older browsers like Netscape 4, but add accessibility and usability features to newer browsers and screen readers.
labelforandidattribute association.<label for="search">Search</label>
<input type="text" name="q" id="search" />This method associates a form element with a text label that can be intercepted by a screen reader. Users of many GUI browsers get the added benefit of clickable text. When the text “Search” is clicked, the associated input receives keyboard focus.
Each
labelelement should have aforattribute that directly corresponds to theidattribute of a form element:input,select, ortextarea.Each
idattribute must be unique and must start with an alphabetical character or underscore (idmust not start with a numeric character). Theidattribute should not be confused with thenameattribute.nameis the variable name sent to the server on submission of a form and does not need to be unique.idis used for client-side identification only and is not sent to the server.- Wrapping element in label (doesn’t work completely as expected).
<label>
Search
<input type="text" name="q" />
</label>This method is technically correct and provides a larger clickable area to sighted users, but doesn’t work in many user-agents, so it’s not a recommended solution.
- Combining both methods.
<label for="search">
Search
<input type="text" name="q" id="search" />
</label>This method is acceptable and achieves the benefits of both.
Tip:
You can use duplicate labels for the same form element. You may wish to put one in the form next to the field and use another in an error list returned from server-side validation. See the example listed later in this page.Update (October 2, 2004): Duplicate labels are problematic because of different implementations by Assistive Technology. Don’t use this method.fieldsetandlegendMultiple related form elements can be grouped with the
fieldsetandlegendelements.<fieldset>
<legend>Gender</legend>
<label for="m">Male</label>
<input type="radio" name="g" value="m" id="m" />
<label for="f">Female</label>
<input type="radio" name="g" value="f" id="f" />
</fieldset>optgroupMultiple related select list option elements can be grouped with the
optgroupelement.<label for="foods">Favorite foods</label>
<select name="f" id="foods">
<optgroup label="Fruits">
<option value="app">Apple</option>
<option value="ora">Orange</option>
<option value="pea">Pear</option>
</optgroup>
<optgroup label="Vegetables">
<option value="bro">Broccoli</option>
<option value="car">Carrots</option>
<option value="asp">Asparagus</option>
</optgroup>
</select>
Note:
optgroupdoes not even work in Internet Explorer version 5, but it degrades gracefully.titleOccasionally, you may run across a form element that doesn’t fit well with a label, or needs some additional information displayed. In these cases, you can use the
titleattribute to supplement information about any element.There is more info on title attributes in AIR’s Accessibility 101 course.
See an example of label, fieldset, and optgroup in use.