Sub topic 7
Labels: naming form controls clearly
A label tells the user what a form control is for. In the text hierarchy it sits next to body text: it is short, it is read often, and it must stay legible at every size. This chapter covers how to attach labels to controls in HTML, when each technique applies, and how to style labels so they read as names for fields rather than as headings.
This chapter assumes you know basic HTML form elements (input, button, form), basic CSS selectors, and the custom properties and rem type scale from the earlier chapters.
What a label does
A label element gives a form control an accessible name. The accessible name is the text that browsers expose to assistive technology such as screen readers and voice control software. When a screen reader user tabs to a field, it announces the accessible name, such as "Email address, edit text". If the field has no name, the user hears only "edit text" and has to guess.
A correctly associated label gives you three things:
- A screen reader announces the label when the control receives focus.
- Clicking or tapping the label focuses the control. For checkboxes and radio buttons, it toggles them. This enlarges the target, which helps people with motor impairments and anyone on a touch screen.
- Voice control users can say "click Email address" to reach the field.
A paragraph or span placed next to an input looks like a label but provides none of these. Visual proximity is not an association. The browser needs a programmatic link.
Explicit labels: for and id
The most common technique links a label to a control by matching the label's for attribute to the control's id.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email">The for value must equal the id exactly, including case. The name attribute is unrelated to labeling. It controls the key used when the form is submitted.
Implicit labels: wrapping
You can also place the control inside the label. No for or id is needed.
<label>
Email address
<input name="email" type="email" autocomplete="email">
</label>Both forms work. Many authors still add for and id on wrapped controls, because some assistive technology combinations handle the explicit link more reliably. Wrapping is convenient for checkboxes and radio buttons, where the label text sits beside the control and the whole row becomes clickable.
<label>
<input type="checkbox" name="newsletter">
Send me the monthly newsletter
</label>Choosing a naming technique
HTML offers several ways to name a control. Prefer the first one that fits.
| Technique | Visible | Click target | Use when |
|---|---|---|---|
| `label` with `for` | Yes | Yes | Default for nearly every control |
| Wrapping `label` | Yes | Yes | Checkboxes, radios, simple layouts |
| Visually hidden `label` | No | Yes, if the label is positioned over the control | A nearby heading or button already makes the purpose clear, such as a search field |
| `aria-labelledby` | Depends on the referenced text | No | The name comes from text already on the page |
| `aria-label` | No | No | No text is available to reference |
| `placeholder` | Until typing starts | No | Never as the only name |
The first rule of ARIA is to use native HTML when it can do the job. aria-label supplies a name to assistive technology but shows nothing on screen. It is also not reliably translated by browser page-translation tools, which do translate visible label text.
Why placeholder is not a label
The placeholder attribute shows hint text inside an empty field.
<!-- Avoid: the placeholder is the only name -->
<input type="email" placeholder="Email address">This pattern has several problems:
- The text disappears when the user starts typing. They can no longer check what the field asks for, which hurts anyone who is interrupted or reviewing a long form.
- Placeholder text is usually styled in light gray and often fails contrast requirements. Making it dark enough makes it look like real input.
- Some assistive technology treats the placeholder as a fallback name, but support and announcements vary.
- An empty-looking field with placeholder text is harder to tell apart from a filled one.
A placeholder is acceptable as an extra example of format, such as [email protected], when a real label is also present. Even then, hint text placed outside the field is more robust.
Hint text and error messages
A label should be short: a noun or noun phrase such as "Email address" or "Postal code". Longer guidance belongs in separate hint text. Connect it to the control with aria-describedby, so screen readers announce it after the name.
<div class="field">
<label for="password">Password</label>
<p id="password-hint" class="hint">At least 12 characters. Spaces are allowed.</p>
<input id="password" name="password" type="password"
autocomplete="new-password" aria-describedby="password-hint">
</div>The accessible name is "Password". The accessible description is the hint. Keeping them separate prevents the screen reader from reading a long sentence as the field's name every time the user lands on it.
Error messages use the same mechanism. When validation fails, add the error element's id to aria-describedby and set aria-invalid="true".
<label for="age">Age</label>
<input id="age" name="age" type="number" inputmode="numeric"
aria-invalid="true" aria-describedby="age-error">
<p id="age-error" class="error">Enter a whole number between 18 and 120.</p>The hint and error text are secondary text, like captions. Style them with the small size from your scale, and keep their color at a contrast ratio of at least 4.5:1.
Groups: fieldset and legend
A single label names a single control. A set of related radio buttons or checkboxes needs a name for the group as well as for each option. Use fieldset with legend.
<fieldset>
<legend>Preferred contact method</legend>
<label>
<input type="radio" name="contact" value="email" checked>
Email
</label>
<label>
<input type="radio" name="contact" value="phone">
Phone
</label>
</fieldset>A screen reader announces the legend together with each option, for example "Preferred contact method, Email, radio button, 1 of 2". Without the legend, the user hears only "Email" and has no context for the question.
The legend is a group label, so it should read as a question or category, not as a section heading. Do not wrap a legend in an h3 just to make it larger. If you want it to look like a heading, use CSS on the legend element. If the group is a real section of the page, put the heading outside the fieldset.
Required and optional fields
Mark required fields with the required attribute so that the browser and assistive technology know about it. Also say so in visible text, because an asterisk alone has no meaning to some users.
<label for="name">Full name <span class="req">(required)</span></label>
<input id="name" name="name" autocomplete="name" required>Choose one convention and apply it across the whole form. Many teams mark the optional fields instead when most fields are required. Either way, explain the convention once near the top of the form, for example "All fields are required unless marked optional".
Styling labels
Labels should be clear without competing with headings. The spine's style gives them body size and slightly heavier weight, and places them above the control.
label {
display: block;
font-size: var(--text-base);
font-weight: 600;
margin-bottom: 0.25rem;
}The label is not larger than body text, because it names a field and does not introduce a section. Weight alone separates it from the input's own text, which stays at normal weight.
There is an exception for checkbox and radio rows. Making their labels display: block with a bold weight can look heavy, so give them their own layout.
.choice {
display: flex;
align-items: flex-start;
gap: 0.5rem;
font-weight: 400;
margin-bottom: 0;
}
.hint {
font-size: var(--text-sm);
color: #595959;
margin: 0 0 0.25rem;
}
fieldset {
border: 0;
padding: 0;
margin: 0 0 1.5rem;
}
legend {
font-size: var(--text-base);
font-weight: 600;
padding: 0;
margin-bottom: 0.5rem;
}To use .choice, add class="choice" to the wrapping labels in the fieldset example. Because .choice has the same specificity as label but comes later in the stylesheet, it overrides the weight and spacing. Note that legend styling is set to match label, which keeps group names and field names visually consistent.
Placement
Put labels above the control by default. Labels above are fast to scan, work well on narrow screens, and leave room for long label text and translations. Labels to the left save vertical space on wide desktop forms but break down on small screens and with longer text. If you need side-by-side labels, switch the layout to stacked below a breakpoint.
Visually hidden labels
When the design shows no label, keep the label in the DOM and hide it visually without removing it from the accessibility tree. Do not use display: none or visibility: hidden, which remove it for everyone.
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}<form role="search" action="/search">
<label for="q" class="visually-hidden">Search the guide</label>
<input id="q" name="q" type="search">
<button type="submit">Search</button>
</form>Use this sparingly. Sighted users with cognitive or memory difficulties also benefit from visible labels, so hide a label only when the context is unmistakable.
Common errors
- Mismatched or duplicate ids. If
for="email"points to anidthat does not exist, or two elements share oneid, the association fails or attaches to the wrong control. Check the browser developer tools accessibility panel to see the computed name. - Labels on non-labelable elements.
forworks with form controls such asinput,select,textarea,button,meter, andprogress. It does nothing for adivorspan. - Interactive content inside a label. Do not place links or buttons inside a label. A click on them conflicts with the label's own activation behavior.
- Using a heading element as a label. An
h3before a field adds an entry to the document outline and still does not name the control. - Generic names. Several fields labeled "Name" or buttons labeled "Submit" in different forms are hard to distinguish. Make each name specific, such as "Billing postal code".
- Label text that differs from the visible text in
aria-label. Voice control users say what they see. If the visible text is "Search" and thearia-labelis "Find items", the command "click Search" fails. The accessible name should contain the visible text. - Low-contrast labels. Light gray labels are common in minimal designs. Label text is normal-size text and needs a 4.5:1 contrast ratio against its background.
A complete example
This form combines a standard label, a hint, a group, and a checkbox, using the type tokens from earlier chapters.
<form action="/signup" method="post">
<h2>Create an account</h2>
<div class="field">
<label for="signup-email">Email address</label>
<input id="signup-email" name="email" type="email"
autocomplete="email" required>
</div>
<div class="field">
<label for="signup-password">Password</label>
<p id="signup-password-hint" class="hint">At least 12 characters.</p>
<input id="signup-password" name="password" type="password"
autocomplete="new-password" minlength="12" required
aria-describedby="signup-password-hint">
</div>
<fieldset>
<legend>Preferred contact method</legend>
<label class="choice">
<input type="radio" name="contact" value="email" checked>
Email
</label>
<label class="choice">
<input type="radio" name="contact" value="phone">
Phone
</label>
</fieldset>
<label class="choice">
<input type="checkbox" name="terms" required>
I accept the terms of service
</label>
<button type="submit">Sign up</button>
</form>The visual hierarchy reads in order. The h2 is the largest text, the legend and labels are body size with heavier weight, the hint is small and gray, and the user's typed input is regular weight. Each role has its own treatment, and the HTML names every control.
Exercise
A login form has an input with <input type="text" id="user" placeholder="Username"> and no other text. Name two problems this causes, then write the fix.
Show answer
Problems: the name disappears once the user types, and the placeholder is not a reliable accessible name, so some screen readers announce the field without a name. Clicking the placeholder text also does not focus anything. Fix: <label for="user">Username</label><input type="text" id="user" name="user" autocomplete="username">. A placeholder with a format example may remain as an extra.
Exercise
You have three radio buttons for "Small", "Medium", and "Large". Each has a correct wrapping label. A screen reader user hears only "Small, radio button" and does not know what is being chosen. What element is missing, and where does it go?
Show answer
A fieldset around the radio buttons with a legend such as "T-shirt size" as its first child. The screen reader then announces the group name together with each option.
Exercise
A designer wants a search box with no visible label, only a magnifier button beside it. How do you name the input without displaying text, and why not use display: none on the label?
Show answer
Keep a label with for matching the input's id, and hide it with a visually hidden CSS technique (absolute position, 1px size, clipped) so it stays in the accessibility tree. display: none removes the label from the accessibility tree too, so the input would have no accessible name.