5 / 6 · Intermediate
Form labels
A text box next to the word "Email" is not labelled until the label element is tied to it; until then a screen reader may say only "edit text".
Jump to the 10 practice questionsYou will learn
- Tie every field to a label
- Group choices with fieldset and legend
- Give instructions and errors in words
Before this: Using a page with the keyboard
1Explore
What a field announces
When focus lands on a form field, a screen reader announces its label, its type and its state: "Email, edit text, required". The label comes from a label element tied to the field. There are two ways to tie them. Give the input an id and the label a matching for, or put the input inside the label. Either way, clicking the label text also moves focus into the field, which makes small checkboxes and radio buttons much easier to hit.
The email field is named by its label; the phone field has only a paragraph above it, so it is announced with no name.
<form>
<label for="email">Email</label>
<input id="email" type="email" required>
<p>Phone</p>
<input id="phone" type="tel">
<label><input type="checkbox"> Send me the weekly menu</label>
</form>2Problem
label and id
Hint
The attribute on label that names the input id.
Solution
Answer: for
for on the label matches id on the input.
3Problem
Clicking the label
What else happens when a label is tied to a checkbox?
Hint
Click "Send me the weekly menu" in the preview.
Solution
Answer: Clicking the label text ticks the box
The label becomes part of the click target.
4Problem
A correctly labelled field
Which is a correctly labelled field?
Hint
for and id must match.
Solution
Answer:
for and id match. The last one has a label with no for and the input is outside it.
5Explore
Placeholder is not a label
The placeholder attribute shows grey hint text inside an empty field. It is tempting to use it instead of a label, and many sign-up forms do. The trouble is that it disappears as soon as you type, so you cannot check what the field was for, and it is usually pale enough to fail the contrast rules from the previous unit. Use a visible label and, if you like, a placeholder with an example value as well.
The first field has only a placeholder: no name. The second keeps the hint and has a real label.
<input type="email" placeholder="you@example.com">
<label for="mail">Email</label>
<input id="mail" type="email" placeholder="you@example.com">6Problem
Why a placeholder is not enough
Why is a placeholder not enough as a label?
Hint
What happens when you start typing?
Solution
Answer: It disappears when you type and is often too pale
The hint vanishes and usually fails contrast.
7Explore
Groups and autocomplete
Radio buttons and related checkboxes need a group label too. Wrap them in a fieldset and put the question in its legend, so a screen reader says "Size, group" before "Small, radio button, 1 of 3". For personal details, the autocomplete attribute tells the browser what a field is for, using names from the HTML standard such as name, email, tel, new-password and one-time-code. Criterion 1.3.5 Identify Input Purpose, at level AA, asks for exactly this, because it lets browsers fill fields in and lets some tools show familiar icons next to them.
The legend becomes the name of the group, announced before each choice.
<fieldset>
<legend>Size</legend>
<label><input type="radio" name="size"> Small</label>
<label><input type="radio" name="size"> Medium</label>
<label><input type="radio" name="size"> Large</label>
</fieldset>8Problem
A question for radio buttons
Which elements give a group of radio buttons its question?
Hint
Look at the group line above.
Solution
Answer: fieldset and legend
The legend is announced as the group name.
9Problem
autocomplete for email
Hint
The value is the same word as the type.
Solution
Answer: email
Use autocomplete="email" so the browser recognises an email address field. The visible label identifies it to the person; the autocomplete value identifies its purpose to software.
10Problem
Identify input purpose
Which criterion asks fields for personal data to identify their purpose?
Hint
Criterion 1.3.5.
Solution
Answer: 1.3.5 Identify Input Purpose
Criterion 1.3.5, level AA, makes the purpose of personal-data fields available to software. Standard autocomplete values let browsers offer the right stored information.
11Explain
Errors in words
When something goes wrong, say so in words. Criterion 3.3.1 Error Identification asks that an input error is identified and described to the user in text, not only by turning the border red. WCAG 2.2 also added 3.3.7 Redundant Entry: information a person already entered in the same process should be filled in again for them or offered to select, instead of being typed twice. And 3.3.8 Accessible Authentication (Minimum), at level AA, says a login step must not depend on remembering or transcribing something unless help is available.
12Problem
A red border only
An error is shown only by a red border. What is missing?
Hint
Colour alone is not enough.
Solution
Answer: A text description of the error
3.3.1 asks for errors to be described in text, and 1.4.1 forbids colour as the only cue.
13Problem
The address again
A checkout asks for the delivery address again on the next step. Which criterion helps?
Hint
A WCAG 2.2 criterion about entering things twice.
Solution
Answer: 3.3.7 Redundant Entry
Criterion 3.3.7 avoids entering the same information again in one process. Reuse the earlier address or offer it for selection rather than making the person retype it.
14Problem
Blocking paste
A login form blocks paste in the password field. Which WCAG 2.2 criterion does that work against?
Hint
A criterion about logging in.
Solution
Answer: 3.3.8 Accessible Authentication (Minimum)
Criterion 3.3.8 supports help with authentication instead of requiring unaided recall or transcription. Allowing paste and password-manager filling provides that help; blocking paste removes one route.
15Practice
Labels and autocomplete
The two fields are labelled only by paragraphs. Replace each paragraph with a label tied to its input with for, and add autocomplete="name" and autocomplete="email".
- label for name
- label for email
- autocomplete="name"
- autocomplete="email"
<form>
<p>Name</p>
<input id="name" type="text">
<p>Email</p>
<input id="email" type="email" placeholder="you@example.com">
<button>Send</button>
</form>Hint
Two labels with for, and an autocomplete on each input.
A solution
<form>
<label for="name">Name</label>
<input id="name" type="text" autocomplete="name">
<label for="email">Email</label>
<input id="email" type="email" autocomplete="email" placeholder="you@example.com">
<button>Send</button>
</form>16Practice
A fieldset with a legend
Group the three size buttons in a fieldset and turn the "Size" paragraph into its legend.
- legend inside fieldset
- three radios inside
<form>
<p>Size</p>
<label><input type="radio" name="size" value="s"> Small</label>
<label><input type="radio" name="size" value="m"> Medium</label>
<label><input type="radio" name="size" value="l"> Large</label>
</form>Hint
legend goes inside fieldset, and the radio buttons too.
A solution
<form>
<fieldset>
<legend>Size</legend>
<label><input type="radio" name="size" value="s"> Small</label>
<label><input type="radio" name="size" value="m"> Medium</label>
<label><input type="radio" name="size" value="l"> Large</label>
</fieldset>
</form>17Explain
Common mistakes
<style>
.err { border: 2px solid #c01c28; }
</style>
<label for="pc">Postcode</label>
<input id="pc" class="err" value="AB1"><style>
.err { border: 2px solid #c01c28; }
</style>
<label for="pc">Postcode</label>
<input id="pc" class="err" value="AB1" aria-describedby="pc-err">
<p id="pc-err">Error: a postcode has 5 or more characters.</p>A red border alone does not say what is wrong. Describe the error in text next to the field.
18
Summary
- Tie each field to a label with for and id, or by wrapping.
- A placeholder disappears and is often too pale; it is not a label.
- Group radios with fieldset and legend; use autocomplete for personal data.
- Describe errors in text; do not make people retype or memorise to log in.