Labels, fieldsets and accessible names
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Associate labels with controls using for and id or wrapping.
- Group related controls with fieldset and legend.
Overview
#Every control needs an accessible name. The name is what a screen reader announces when the control receives focus, and it is how speech input users target the field. The most robust way to provide it is a <label> associated with the control, either by matching for and id or by wrapping the control inside the label.
<label for="q">Search</label>
<input id="q" name="q" type="search" />
<label>
Subscribe to updates
<input name="subscribe" type="checkbox" />
</label>
Placeholder text is not a label. It disappears as soon as the user types, it is often low contrast, and it is not reliably announced. Use a visible label, and use the placeholder only for an optional example.
When several controls belong to one question, wrap them in a <fieldset> with a <legend>. Radio groups and checkbox groups are the classic case: the legend names the group and each control has its own label.
<fieldset>
<legend>Delivery speed</legend>
<label><input type="radio" name="speed" value="standard" /> Standard</label>
<label><input type="radio" name="speed" value="express" /> Express</label>
</fieldset>
If you cannot use a visible label, aria-label or aria-labelledby can supply a name, but a visible label is better because it helps everyone. Never rely on title for a name: it is not shown on touch, is hard to trigger, and support is inconsistent.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Debug
This field has no accessible name. Fix it without using a placeholder.
starter.html<input type="text" name="card" placeholder="Card number" />Reveal the reference answer
reference.html<label for="card">Card number</label> <input id="card" type="text" name="card" inputmode="numeric" autocomplete="cc-number" />Authored expected output: The input has an associated label element and the placeholder is no longer the only name.
A label element provides a persistent accessible name that survives typing, unlike a placeholder.
Exercise 2Implement
Build a radio group named 'plan' with two options, Basic and Pro, grouped under a legend 'Plan'.
starter.html<fieldset> <!-- add legend and radios --> </fieldset>Reveal the reference answer
reference.html<fieldset> <legend>Plan</legend> <label><input type="radio" name="plan" value="basic" /> Basic</label> <label><input type="radio" name="plan" value="pro" /> Pro</label> </fieldset>Authored expected output: A fieldset with a legend and two labelled radio inputs sharing the same name.
The shared name groups the radios so only one can be selected, and the legend names the group for assistive technology.
Common misconceptions
#- Placeholder text is an acceptable substitute for a label.
- Each radio button in a group needs its own fieldset.
Follow-up practice
#- html-css-08-input-types-validation
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.