Sub topic 3
Labeling every control
This chapter assumes you can write a basic form with input elements and a submit button, and that you know what id and name attributes are. It covers how a label connects to a control, what a browser does with that connection, and the mistakes that break it.
Why every control needs a label
A label is the visible text that tells a person what a control is for. The browser also uses it to give the control an accessible name. The accessible name is the string that screen readers announce and that voice-control software matches when a user says "click Email address".
A correctly connected label gives you three things:
- A screen reader announces "Email address, edit text" instead of just "edit text".
- Clicking the label text focuses a text input, or toggles a checkbox or radio button. This gives small controls a much bigger click target.
- Voice and switch users can target the control by its visible words.
A form can look complete and still fail on all three. Visual proximity does not create an association. A <span> placed next to an input is only decoration.
Connecting a label to a control
The label element can be connected to a control in two ways.
Explicit association with for and id
The label's for attribute holds the id of the control it describes.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email">The two attributes refer to each other, but they are different things. for and id link the label to the control. name decides what is submitted. Changing one does not affect the other.
Implicit association by nesting
You can wrap the control inside the label. No for or id is needed.
<label>
Email address
<input name="email" type="email" autocomplete="email">
</label>Both patterns work with current assistive technology. Explicit association is the safer default for text fields. It lets you lay out the label and the input in separate grid cells, and it works in every older screen reader. Nesting is a good fit for checkboxes and radio buttons, where the label text sits right next to the control.
You may combine both. The browser resolves them to the same control, so nothing breaks.
<label for="agree">
<input id="agree" name="agree" type="checkbox" value="yes">
I accept the terms
</label>What can be labeled
Only labelable elements can be the target of a label. These are input (except type="hidden"), textarea, select, button, meter, output, and progress. Custom elements that are form-associated can also be labeled.
A for that points at a div, a span, or a type="hidden" input does nothing. The browser does not report an error. You can check the result in JavaScript with the control property of a label, or the labels property of a control:
const label = document.querySelector('label[for="email"]');
console.log(label.control); // the input element, or null
console.log(document.getElementById('email').labels.length); // 1 when linkedIf control is null, the label is not connected.
How the browser finds a name
When a control has no label, the browser looks for other sources. The accessible name comes from the first source in this list that produces text:
| Order | Source | Notes |
|---|---|---|
| 1 | `aria-labelledby` | Points to the `id` of one or more elements. Overrides everything else. |
| 2 | `aria-label` | A string on the control itself. Not visible to sighted users. |
| 3 | Associated `label` | The normal, preferred approach. |
| 4 | `title` | Weak. Often shown only on hover. |
| 5 | `placeholder` | Last resort. Disappears on input. |
The order has a consequence that surprises people. An aria-label on an input silently overrides a perfectly good visible label. If the two differ, voice-control users who say the visible words may fail to match the control. Use ARIA names only when no visible text exists.
Labels that are not visible
Sometimes a design has no room for visible text, for example a search box next to a magnifier button. Prefer a visible label where you can. When you cannot, keep a real label and hide it visually, so it still works with assistive technology.
<style>
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
</style>
<form action="/search" method="get" role="search">
<label class="visually-hidden" for="q">Search the site</label>
<input id="q" name="q" type="search">
<button type="submit">Search</button>
</form>Do not use display: none or the hidden attribute on the label. Both remove it from the accessibility tree, and the control loses its name.
Hints and extra text
A label should be short and name the field. Longer help, such as format rules, belongs in a separate element that aria-describedby links to the control. The label gives the name. The description is read afterward.
<label for="username">Username</label>
<input id="username" name="username" type="text"
autocomplete="username" aria-describedby="username-hint">
<p id="username-hint">Use 3 to 20 letters or digits.</p>The same mechanism will carry error messages in a later chapter.
Common errors
- Duplicate ids. Two controls with
id="email"makeforpoint at the first one only. This often happens when a form component is rendered twice on a page. Generate unique ids. - Mismatched spelling.
for="e-mail"withid="email"links to nothing. Checklabel.controlas shown above. - Several labels on one control. This is allowed, and the names are joined in order. It is rarely what you want, and it can produce confusing announcements.
- Interactive content inside a label. A link or button inside a label competes with the label's own click behavior. Keep labels to plain text and light inline markup.
- Using
forwith a group. A single label cannot name a set of radio buttons. Each radio gets its own label, and the group is named withfieldsetandlegend, which a later chapter covers. - Labeling by position. Text such as "Name" in a table cell next to an input is not a label unless you link it.
- Vague wording. Repeating "Enter value" on every field gives no information. Say what the data is.
A complete example
This form uses each technique from the chapter.
<form action="/signup" method="post">
<label for="full-name">Full name</label>
<input id="full-name" name="fullName" type="text" autocomplete="name" required>
<label for="email-2">Email address</label>
<input id="email-2" name="email" type="email" autocomplete="email"
aria-describedby="email-hint" required>
<p id="email-hint">We only use it to send your receipt.</p>
<label>
<input type="checkbox" name="terms" value="yes" required>
I accept the terms
</label>
<button type="submit">Create account</button>
</form>The button needs no label. Its text content, "Create account", is its accessible name.
Exercise
A form has <label for="mail">Email</label> and <input id="email" name="mail" type="email">. Clicking the label does nothing, and a screen reader announces only "edit text". What is wrong, and what is the smallest fix?
Show answer
The label's for value (mail) does not match the input's id (email). The name attribute is not used for association. Change the label to for="email", or change the input's id to mail. Afterward, label.control returns the input and the accessible name is "Email".
Exercise
A search field has a magnifier icon button and no room for visible text. Which markup keeps the field named for screen readers, and why is display: none on the label a bad choice?
Show answer
Keep a real label connected with for and id, and hide it with a visually-hidden CSS class that keeps it in the layout flow but takes up almost no space. display: none removes the label from the accessibility tree, so the input loses its accessible name.