Sub topic 10
Forms: labels, fieldsets and legends
This chapter assumes you know basic HTML syntax (tags, attributes, nesting) and can open your browser's developer tools. CSS knowledge helps but is not required.
Why form semantics matter
A form control without a programmatic name is a problem. A screen reader user who tabs into an unlabeled text box hears only "edit text". A sighted user with a motor impairment has a tiny click target. Native form elements fix both problems with very little markup.
Labels
A label element ties a visible text string to one control. There are two ways to associate them.
<!-- Explicit: for matches the control's id -->
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
<!-- Implicit: the control sits inside the label -->
<label>
<input type="checkbox" name="weekly"> Weekly digest
</label>The association gives you three things:
- The label text becomes the control's accessible name.
- Clicking the label focuses a text field or toggles a checkbox.
- Voice-control users can say the label text to reach the control.
A placeholder is not a label. It disappears when the user types, often has low contrast, and is not reliably used as a name. Use it only for a short hint, and keep a real label as well.
Fieldset and legend
Some questions need a group name. "Small, Medium, Large" means nothing without "Shirt size". A fieldset groups related controls, and its first child legend names the group.
<form action="/order" method="post">
<fieldset>
<legend>Shirt 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>
<button type="submit">Order</button>
</form>Browsers expose the fieldset as a group. A screen reader typically announces the legend when focus enters the group, then each radio button's own label. Radio buttons that share a name form a mutually exclusive set. Checkbox groups benefit in the same way.
Putting it together
The newsletter form below combines these pieces. Each control has a label, the related controls share a fieldset, and the submit action is a real button.
<form action="/subscribe" method="post">
<fieldset>
<legend>Newsletter</legend>
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
<label>
<input type="checkbox" name="weekly"> Weekly digest
</label>
</fieldset>
<button type="submit">Subscribe</button>
</form>The type="email" and required attributes add built-in validation and the right mobile keyboard without any JavaScript.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| `for` value does not match any `id` | Label is not associated | Make the `for` and `id` values identical |
| Duplicate `id` values | Label attaches to the wrong control | Keep each `id` unique on the page |
| Placeholder used instead of a label | No lasting visible name | Add a `label` |
| Radio buttons with no shared `name` | Several can be selected | Give the group one `name` |
| Group of radios with no `legend` | Question is not announced | Wrap them in `fieldset` and `legend` |
Exercise
A signup form has three radio buttons named "Monthly", "Quarterly" and "Yearly". Each has its own label. A screen reader user hears only the individual options and does not know what is being chosen. What markup fixes this, and why?
Show answer
Wrap the radio buttons in a fieldset and add a legend as its first child, for example "Billing period". The browser exposes the fieldset as a named group, so assistive technology announces the legend when focus enters the group. Give the radios a shared name so only one can be selected.