One of the most common issues I see with forms is the improper (or lack of) use of label elements. Admittedly, I too am guilty of misuse, but even after becoming aware of them, I was very confused about how to do radio and checkbox elements. Why? I learned incorrectly that the
for attribute of the label referred to the
name attribute of the input. Wipe that from your memory this instant. The label’s
for attribute is a reference to the input’s
Wait, back up. What is the label element?
The label element in HTML provides a way to give semantic meaning to the text describing or labeling form control elements. Using the element not only gives you an element to style around your labels, but also provides accessibility and functional advantages.
By using a properly constructed label element along with your input, textarea, or select element, you give
- screen readers information on which form control the label belongs to,
- manually maladroit visitors an increased click area (clicking on the label focuses on the element), and
Add this element and its use to your list of best practices for forms.
How do I use the label element?
The label element is simple to use and shouldn’t cause any additional headaches while setting up forms. Its usage is simple:
<label for="visitor-email">E-mail address</label> <input type="text" name="email" id="visitor-email" size="32" />
It can go either before or after your element. In a real form, you would most likely have the entire line wrapped in a list item or paragraph element, but I left those off for purposes of demonstration.
For checkboxes and radio buttons, the usage is very similar. You provide a label for each individual element, keyed to that element’s
<label for="contact-by-email">e-mail</label> <input type="radio" name="contact-how" id="contact-by-email" checked="checked" /> <label for="contact-by-phone">phone</label> <input type="radio" name="contact-how" id="contact-by-phone" />
Incorrect use, a demonstration
The following are improper uses of the label element:
<label>E-mail address</label> <input type="text" name="email" id="visitor-email" size="32" />
- In this example, the label does not have a
forattribute declared, so its semantic value and aid in accessibility is lost.
<label for="contact-how">Contact by</label> phone <input type="radio" name="contact-how" id="contact-by-phone" checked="checked" /> e-mail <input type="radio" name="contact-how" id="contact-by-email" />
- Here, the
forattribute is referring to the
nameattribute of the inputs. This is incorrect. The
forattribute is a reference to the
idattribute of the input.
<label>Phone <input type="radio" name="contact-how" id="contact-by-phone" checked="checked" /></label>
- While technically correct according to W3C recommendations, this method should not be used. The reason being, certain browsers, particularly mobile browsers, handle this code incorrectly and will often cause the element to be un-clickable. You are better off using the method of labeling checkboxes/radio buttons I described above.
For detailed information on the label element and other form information, check out the W3C Recommendations on Forms.