Sub topic 6
Grouping related fields with fieldset and legend
This chapter assumes you can write basic HTML forms and know how a label is tied to an input with matching for and id values. If that is new, review the labels chapter first.
Why grouping matters
A label names one control. Some questions need more than one control to answer. A set of radio buttons is the standard case. Each option has its own label ("Email", "Phone"), but nothing says what the user is choosing. A sighted user sees the question above the options. A screen reader user who tabs into the group hears only "Email, radio button" and may not know what is being asked.
The fieldset element groups related controls. The legend element gives the group a name. Browsers expose the pair to assistive technology as a group with an accessible name, and screen readers typically announce the legend when focus enters the group.
A basic group
<fieldset>
<legend>Preferred contact method</legend>
<div>
<input type="radio" id="contact-email" name="contact" value="email">
<label for="contact-email">Email</label>
</div>
<div>
<input type="radio" id="contact-phone" name="contact" value="phone">
<label for="contact-phone">Phone</label>
</div>
</fieldset>When focus lands on the first radio button, a screen reader typically reads something like "Preferred contact method, group. Email, radio button, 1 of 2." The legend supplies the question and each label supplies the answer.
Rules to follow:
- Make the
legendthe first child of thefieldset. - Keep legend text short and phrased as the question or topic.
- Keep each option's
labelas well. The legend does not replace it.
When to use a fieldset
Use a fieldset when several controls answer one question together.
| Situation | Use a fieldset? | Reason |
|---|---|---|
| Radio buttons with a shared `name` | Yes | The options need a shared question. |
| A list of related checkboxes ("Which topics interest you?") | Yes | Each checkbox label alone is ambiguous. |
| Date split into day, month, year fields | Yes | The legend ("Date of birth") names all three fields. |
| Billing address and shipping address blocks | Yes | The legend separates two sets of repeated field names. |
| A single text input | No | Its `label` is enough. |
| Two unrelated fields side by side | No | Grouping them would be misleading. |
Avoid wrapping a whole long form in one fieldset. A legend that is announced for every control adds noise and gives no help.
Grouping that matches the user's mental model
Grouping also helps sighted users. Related fields placed together under a clear heading make a form easier to scan. This connects to field order: group first, then order the groups the way the user thinks about the task, such as "Your details", then "Delivery address", then "Payment".
For multi-part groups, the legend should name the group and each field's label should name only its part:
<fieldset>
<legend>Date of birth</legend>
<label for="dob-day">Day</label>
<input id="dob-day" name="dob-day" type="text" inputmode="numeric" autocomplete="bday-day">
<label for="dob-month">Month</label>
<input id="dob-month" name="dob-month" type="text" inputmode="numeric" autocomplete="bday-month">
<label for="dob-year">Year</label>
<input id="dob-year" name="dob-year" type="text" inputmode="numeric" autocomplete="bday-year">
</fieldset>Common errors
- Using a
divand a bold paragraph instead offieldsetandlegend. It looks the same, but assistive technology gets no group. - Putting the legend outside the fieldset or after the controls. The legend must be the first child.
- Repeating the legend in every label. "Preferred contact method: Email" is announced twice.
- Removing the legend visually with
display: none. This also hides it from screen readers. If the design needs it hidden, use a visually-hidden CSS technique, and prefer keeping it visible. - Resetting styles and losing the layout. Browsers draw a border and padding on
fieldset, and legends are positioned oddly by default. Restyle them deliberately with CSS and do not drop the elements.
fieldset {
border: 0;
margin: 0 0 1.5rem;
padding: 0;
}
legend {
padding: 0;
margin-bottom: 0.5rem;
font-weight: 600;
}Errors and groups
When a group has an error, such as no radio option chosen, the message belongs to the group, not to one radio button. Link the message to the fieldset with aria-describedby. A later chapter covers this in detail.
Exercise
A form asks "Which newsletters do you want?" and lists three checkboxes labelled "Weekly digest", "Product news", and "Events". The question is currently a bold p above a div. What markup change helps screen reader users, and why?
Show answer
Replace the div with a fieldset and replace the bold p with a legend as the first child of the fieldset. Keep each checkbox's own label. The legend is announced with the checkboxes, so users hear the question along with each option. A bold paragraph is not connected to the controls.
Exercise
A page has one fieldset around the entire checkout form, with the legend "Checkout". Why is this a poor use of the element, and what would be better?
Show answer
Screen readers may announce the legend as users move between controls, so "Checkout" adds repeated noise without explaining any single question. Use separate fieldsets for specific groups, such as "Delivery method" or "Date of birth", and leave single fields with only their labels.