Sub topic 2
Labels: every control needs a visible, associated label
This chapter assumes you know basic HTML form elements and can read short CSS and JavaScript. It covers why every form control needs a label, how to connect a label to its control, and the mistakes that break that connection.
Why every control needs a label
A label tells the user what a field is for. It does three jobs:
- Sighted users read it to know what to type.
- Screen readers announce it when the field gets focus. Without it, the user hears only "edit text".
- Clicking or tapping the label moves focus to the field. This gives a larger target, which helps people with motor impairments and anyone on a phone.
A label must be visible and associated. Visible means it stays on screen while the user types. Associated means the browser knows which control it belongs to. Text that sits near a field looks like a label, but it is not one until the markup links them.
Associating a label with a control
The most reliable method is the label element with a for attribute. The for value must match the id of the control.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>Here the for and id values are both email. Clicking the text focuses the input, and a screen reader announces "Email address" when the field is reached.
You can also wrap the control inside the label. This needs no id, but the explicit for and id pairing is more robust with assistive technology and easier to style.
<label>
Email address
<input name="email" type="email" autocomplete="email">
</label>Common errors
Most label problems come from a broken link or a missing label. Check for these:
| Mistake | What goes wrong | Fix |
|---|---|---|
| `for` does not match any `id` | The label looks right but is not linked | Make the values identical |
| Duplicate `id` values on a page | The label links to the first match only | Give every control a unique `id` |
| Placeholder used as the only label | The text vanishes when the user types | Add a real `label` |
| Label hidden with `display: none` | Screen readers skip it too | Keep it visible |
| Plain `div` or `p` next to the field | No programmatic link exists | Use `label` |
Duplicate ids often appear when you copy a form block. A "Name" field in a billing section and a shipping section must not both use id="name".
> The placeholder row deserves attention because it is the most common shortcut. Chapter 4 covers hint text and why placeholders cannot replace labels.
Checking your work
You can test the link without any tools. Click the label text. If the cursor appears in the field, the association works. For a deeper check, open the browser's accessibility inspector and read the "Name" of the input. It should match the visible label text.
You can also audit a page with a short script that lists controls with no accessible label:
const controls = document.querySelectorAll('input:not([type="hidden"]), select, textarea');
controls.forEach((control) => {
const hasLabel = control.labels && control.labels.length > 0;
if (!hasLabel) {
console.warn('No associated label:', control);
}
});The labels property is a list of every label element linked to the control. An empty list means the control has no associated label.
Exercise
A form has <label for="user-name">Name</label> followed by <input id="username" name="name" type="text">. 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 two are not linked. Make them identical, for example change the label to for="username".
Exercise
A designer wants to remove visible labels and keep only placeholder text to save space. Give two reasons this fails users.
Show answer
The placeholder disappears once the user starts typing, so they can no longer see what the field asks for. A placeholder is also not a reliable accessible name for all assistive technology, and its low-contrast text is hard to read. Keep a visible label for every control.