Sub topic 7
Labels: naming form controls clearly
This chapter assumes you know basic HTML form elements, CSS selectors, and the type scale custom properties from earlier chapters. A label is the text role that names a form control. It sits low in the visual hierarchy, but a missing or broken label stops some users from completing a form at all.
What a label does
A <label> element gives a form control an accessible name. Screen readers announce that name when the control receives focus. The label also becomes a click target: clicking it focuses the field, or toggles a checkbox or radio button. That larger target helps people on touch screens and people with limited motor control.
Placeholder text is not a substitute. It disappears when the user types, it is often low contrast, and it is not reliably announced as a name.
Associating a label with a control
There are two ways to connect them. The explicit way matches 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 implicit way wraps the control inside the label:
<label>
Email address
<input name="email" type="email" autocomplete="email">
</label>Both work. The explicit form is easier to style and lay out, because the label and input can sit in separate elements. Whichever you use, each id must be unique on the page. If two inputs share an id, the label attaches to only one of them.
Styling labels
A label should read as a name for the field, not as a heading. Give it body size, a slightly heavier weight, and place it above the control:
label {
display: block;
font-size: var(--text-base);
font-weight: 600;
margin-bottom: 0.25rem;
}Keep the label at --text-base or larger. Shrinking labels to --text-sm to make a form look tidy makes them harder to read, and the label is the one piece of text the user must read to fill in the field. Use the same treatment for every label on the page so that the pattern is recognizable.
Labels above fields also reflow well on narrow screens. Labels placed beside fields need extra width, and they tend to wrap awkwardly when the viewport shrinks or the user zooms in.
Grouping controls
Radio buttons and related checkboxes need two kinds of naming. Each option has its own label, and the set has a shared question. Use <fieldset> and <legend> for the shared question:
<fieldset>
<legend>Preferred contact method</legend>
<input id="contact-email" name="contact" type="radio" value="email">
<label for="contact-email">Email</label>
<input id="contact-phone" name="contact" type="radio" value="phone">
<label for="contact-phone">Phone</label>
</fieldset>Style the legend like a label: the same size and weight. Per-option labels can use normal weight so the group question stands out from its choices.
Hints and error messages
Extra guidance, such as a format hint, is secondary text. Treat it like a caption: smaller, muted, and tied to the control. Link it with aria-describedby so assistive technology reads it after the label:
<label for="password">Password</label>
<input id="password" name="password" type="password"
autocomplete="new-password" aria-describedby="password-hint">
<p id="password-hint" class="hint">At least 12 characters.</p>.hint {
font-size: var(--text-sm);
color: #595959;
margin-top: 0.25rem;
}The #595959 color on a white background has a contrast ratio of about 7:1, which passes the WCAG AA minimum of 4.5:1 for small text. Keep hints at that level or darker.
Common errors
| Error | Result | Fix |
|---|---|---|
| `for` value does not match any `id` | Label is not connected | Make the two strings identical, including case |
| Duplicate `id` values | Label attaches to the wrong control or only one | Give each control a unique `id` |
| Placeholder used instead of label | No persistent name, often low contrast | Add a real `<label>` |
| Label hidden with `display: none` | Removed from the accessibility tree | Use a visually hidden class if the design requires it |
| Label text such as "Click here" or "Field 1" | Name is not meaningful out of context | Describe the data requested |
Checking your work
- Click each label. The matching control should receive focus or toggle.
- Tab through the form with a screen reader or the browser's accessibility inspector and confirm each control shows a name.
- Zoom the page to 200%. Labels should stay visible and not overlap their fields.
Exercise
A form has <label for="user-name">Name</label> followed by <input id="username" name="name">. Clicking the label does nothing. What is wrong, and how do you fix it?
Show answer
The for value ("user-name") does not match the input's id ("username"), so the label is not associated with the input. Make them identical, for example change the label to for="username". Clicking the label then focuses the field, and screen readers announce "Name" for it.
Exercise
Why should a group of radio buttons use fieldset and legend in addition to a label on each radio button?
Show answer
Each label names only its own option, such as "Email" or "Phone". The legend supplies the question that applies to the whole group, such as "Preferred contact method". Without it, a screen reader user hears the options but not what they are choosing.