Sub topic 3
Labeling every control
This chapter assumes you can write basic HTML, know how a text input and a form work, and have seen the name attribute on a control. Every control needs a label. A label tells sighted users what to type. It also gives screen readers a name to announce, and it makes the control easier to click.
What a label does
A label element describes one control. When the label is connected to the control, three things happen:
- A screen reader announces the label text when the control gets focus.
- Clicking or tapping the label moves focus to a text input, or toggles a checkbox or radio button.
- The label's text becomes the control's accessible name, which is the string assistive technology uses to identify it.
Two ways to connect a label
The first way uses for and id. The label's for value must equal the control's id.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email">The second way wraps the control inside the label. No for or id is needed.
<label>
<input type="checkbox" name="newsletter" value="yes"> Send me the newsletter
</label>Both work. Wrapping is common for checkboxes and radio buttons. The for and id form is easier to style when the label and control sit in different parts of the layout. You can use both at once.
Common errors
- Placeholder as label. The
placeholderattribute disappears once the user types. It also has low contrast in many browsers and is not a reliable name. Use it only for a short example value, never instead of a label. - Mismatched
forandid. A typo such asfor="e-mail"againstid="email"silently breaks the link. Nothing warns you. Test by clicking the label and checking that the input gains focus. - Duplicate
idvalues. Anidmust be unique in the page. With duplicates, the label connects to the first match only. - Label text next to the control but not in a label. A plain
<p>or<span>looks the same but gives no accessible name. - Hiding the label with
display: none. This removes it from the accessibility tree too. If the design has no visible label, use a visually hidden CSS class, oraria-labelas a last resort.
<input type="search" name="q" aria-label="Search the site">Prefer a visible label whenever the design allows. aria-label is not visible, so it does not help sighted users and is often missed by translation tools.
Checking the result
Open your browser's developer tools and inspect the control in the accessibility panel. The "Name" field should show your label text. If it is empty, the label is not connected.
Exercises
Exercise
This markup has a labeling bug. What is it, and how do you fix it? <label for="user-name">Name</label> <input id="username" name="name" type="text">
Show answer
The label's for value ("user-name") does not match the input's id ("username"), so the two are not connected. Clicking the label does nothing and screen readers announce no name. Make them identical, for example for="username" with id="username".
Exercise
A designer wants a search box with only a magnifier icon and no visible text. Which two options give the input an accessible name, and which one is preferred?
Show answer
Use a visually hidden label connected with for and id, or add aria-label to the input. The hidden label is preferred because it is real text that translation tools and other assistive features can use. Do not use display: none on the label, because that removes it from the accessibility tree.