A field without a label is a field that some people cannot use at all. Labels are not decoration, and they are not optional polish on top of a working form.
A screen reader announces an unlabelled input as "edit text", with no clue about what belongs in it. A sighted visitor who cannot see the surrounding text has exactly the same problem.
Labels are the difference between a form that works and a form that quietly excludes people.
The previous lesson in this module covered "How Do Forms Collect Data With Modern Input Types?", and the next lesson in this module is about "How Do Selects, Checkboxes and Radios Differ?".
How does a label work?
A label is a piece of text tied to a control. The text is what the visitor reads, and the tie to the control is what makes it do its job.
The tie is made with the for attribute, which holds the id of the control. The control has the matching id.
<label for="email">Email address</label>
<input type="email" id="email" name="email">Two useful things happen once the two are tied together.
Clicking the label moves the focus to the control. That is a small convenience, but a real one for anybody aiming at a small target.
The accessible name of the control becomes the text of the label, and that is what a screen reader announces.
Does the label have to sit outside the input?
It does not. A label can wrap the control that it labels, and then the for attribute is unnecessary.
<!-- the for/id pair -->
<label for="email">Email address</label>
<input type="email" id="email" name="email">
<!-- the wrapping form -->
<label>Email address
<input type="email" name="email">
</label>Both approaches work.
The explicit pair is easier to style, easier to place separately from the control, and easier to check with a validator. That is why it is the one you reach for by default.
What you must never do is use both approaches on the same control with different text, because the browser resolves that conflict in a way that surprises people.
Why is placeholder text not a label?
This is the mistake every beginner makes once. You put the hint in the placeholder, you skip the label, and the field looks finished. It is not, and there are three separate reasons.
<!-- wrong: no accessible name at all -->
<input type="email" name="email" placeholder="Email address">
<!-- right -->
<label for="email">Email address</label>
<input type="email" id="email" name="email">First, the placeholder disappears as soon as the visitor types, so the hint is gone exactly when somebody with low vision or a memory problem needs it most.
Second, screen readers treat placeholder text inconsistently, and several of them do not announce it at all. The field is announced with no name.
Third, the contrast between a light grey placeholder and a white background is usually far below the accessibility minimum, so the hint is hard to read even for sighted users.
A placeholder is fine as a format example on top of a real label, such as "[email protected]" for an email field. The label still has to exist, and the placeholder must not be the only description of the field.
How do you label the other controls?
Every control needs the same treatment, and the for and id pair works for all of them.
<label for="plan">Choose a plan</label>
<select id="plan" name="plan">
<option value="free">Free</option>
<option value="pro">Pro</option>
</select>
<fieldset>
<legend>Notifications</legend>
<label for="email-updates">
<input type="checkbox" id="email-updates" name="notifications" value="email">
Email me about new lessons
</label>
<label for="sms-updates">
<input type="checkbox" id="sms-updates" name="notifications" value="sms">
Text me about new lessons
</label>
</fieldset>There are two things to note.
A group of related checkboxes or radios belongs in a fieldset with a legend. That gives the group a name, and it lets a screen reader announce the group before each option.
A legend is not a label. It names the group, while each option still needs its own label.
What about the aria-label?
The aria-label attribute sets an accessible name directly, and it works. But it is the second-best tool.
A visible label helps everybody. It is what a sighted user reads, it is what a screen reader reads, and it is what survives a translation.
An aria-label is invisible on the page, so it adds a name that only the assistive technology sees. It also means the visible label and the spoken label can drift apart.
<!-- visible label, tied to the control -->
<label for="search">Search the tutorials</label>
<input type="search" id="search" name="q">
<!-- aria-label: only when there is no visible text to use -->
<button aria-label="Close dialog">×</button>Reach for the aria-label when the control is an icon with no text, such as a close button or a search icon, and there is genuinely nothing visible to label.
In every other case, use a real label element.
How do you group related fields?
Some forms ask for the same kind of thing several times.
A delivery address might need a street, a city, and a postcode. A phone number might need a country code and a local number.
Wrapping those in a fieldset with a legend gives the group a name that every screen reader announces before the first field inside it.
<fieldset>
<legend>Delivery address</legend>
<label for="street">Street and number</label>
<input id="street" name="street" autocomplete="street-address">
<label for="city">City</label>
<input id="city" name="city" autocomplete="address-level2">
<label for="postcode">Postcode</label>
<input id="postcode" name="postcode" autocomplete="postal-code">
</fieldset>The fieldset element is not only an accessibility tool.
Browsers style it with a border and a legend by default, so it is a quick way to draw a labelled group of controls. You can remove the default border in the CSS when the design does not want one.
The autocomplete attributes in that example are worth adding to every field you can.
They let a browser fill the form from a saved address or a stored card. That is faster for everybody, and it removes a whole class of typing errors.
For a set of radio buttons that belong together, the grouping comes from a shared name attribute and not from a fieldset. You still want a fieldset around them, because the group needs a label that explains what the choice is for.
Quick reference
- You tie a label to a control with a matching for and id, or you wrap the control in the label.
- A placeholder is a hint and never a label. It vanishes, some screen readers ignore it, and its contrast is usually too low.
- Groups of checkboxes and radios need a fieldset and a legend.
- You prefer a visible label over the aria-label, and you use the aria-label for icon-only controls.
- A click on the label focuses the control, which is a usability win as well as an accessibility one.
Try it yourself: you build a form where every control has a visible label, then you check the accessibility tree in the developer tools and read the computed name of each control. You remove one label and you watch the name disappear. Then you replace a label with a placeholder and you check the name again. It will still be missing.