Sub topic 6
Grouping related fields with fieldset and legend
This chapter assumes you can write basic HTML forms, know what a label element does, and have read the earlier chapters on labels and field order. It covers how to group related controls so that every user, including people using a screen reader, understands which controls belong together and what question they answer.
Why a label is not always enough
A label names one control. Some questions need more than one control to answer, and some controls only make sense inside a shared question.
Consider a set of radio buttons. Each option has its own label, such as "Email" or "Phone". Neither label tells the user what is being chosen. Sighted users see a heading or a line of text above the options and infer the question. A screen reader user tabbing into the first radio button hears only "Email, radio button, 1 of 2". The question is missing.
The fieldset element groups related controls, and the legend element gives the group a name. Browsers expose this pairing to assistive technology as a group with an accessible name. When focus moves into the group, a screen reader typically announces the legend along with the control's own label, for example "Preferred contact method, group. Email, radio button, 1 of 2".
The basic pattern
<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>Three rules make this work:
- The
legendmust be the first child of thefieldset. A legend placed elsewhere is not treated as the group's name. - Every control inside still keeps its own
label. The legend adds context. It does not replace labels. - Radio buttons in one group share the same
namevalue. This is what makes them mutually exclusive, and it is separate from thefieldsetgrouping.
When to use a fieldset
Use a fieldset and legend when one question is answered by several controls, or when a set of controls shares a meaning that the individual labels do not carry.
| Situation | Use fieldset? | Reason |
|---|---|---|
| Radio buttons for one question | Yes | Each option label needs the shared question |
| Checkboxes for one question ("Which topics interest you?") | Yes | Same reason as radio buttons |
| Date split into day, month, and year fields | Yes | "Day" alone is meaningless without "Date of birth" |
| Address block with street, city, and postcode | Often | Useful if the block is distinct, such as "Delivery address" versus "Billing address" |
| A single checkbox, such as "I agree to the terms" | No | Its label already carries the full meaning |
| A single text input | No | The label is enough |
| Two unrelated fields that happen to sit near each other | No | Grouping implies a relationship that does not exist |
The date example shows the value well. Three number inputs labelled "Day", "Month", and "Year" are ambiguous when you reach them by tabbing, because you do not know whose date it is. A legend of "Date of birth" resolves that.
<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" size="2">
<label for="dob-month">Month</label>
<input id="dob-month" name="dob-month" type="text" inputmode="numeric" autocomplete="bday-month" size="2">
<label for="dob-year">Year</label>
<input id="dob-year" name="dob-year" type="text" inputmode="numeric" autocomplete="bday-year" size="4">
</fieldset>Writing good legends
A legend works like the question in a spoken conversation, so write it as one.
- Make it short and specific. "Preferred contact method" is better than "Options".
- Make it unique on the page. If two groups both say "Address", add the purpose: "Delivery address" and "Billing address".
- Do not repeat the legend in each label. Write "Email", not "Contact method: email".
- Do not end with a long instruction. Screen readers repeat the legend for each control in the group, so a 30-word legend becomes tiresome.
- Put required or optional status in the legend only when it applies to the whole group, and keep to the convention you chose for the rest of the form.
Hint text and errors on a group
A group can have hint text and an error message, just like a single field. Because the legend names the group, the hint and error should be linked to the fieldset through aria-describedby. A screen reader then reads the legend first and the description afterwards.
<fieldset aria-describedby="dob-hint dob-error">
<legend>Date of birth</legend>
<p id="dob-hint">For example, 27 3 1985</p>
<p id="dob-error">Error: Date of birth must be in the past</p>
<!-- day, month, and year inputs -->
</fieldset>Support for aria-describedby on a fieldset is good in current browsers and screen readers, but the announcement is not identical everywhere. Some combinations read the description only when focus first enters the group. Test with the screen readers your users use. If you need the message announced on every control, also attach the error id to each input's aria-describedby.
When a group fails validation, mark the problem controls and not the whole group. If only the year is wrong, set aria-invalid="true" on the year input and style that input, so the user knows exactly which part to change. Error messaging in full is covered in later chapters.
Styling and layout gotchas
Browsers give fieldset a default border, padding, and a legend that sits across the top border. Most designs reset this. The legend element is also unusual to style, and a few older quirks still affect it.
fieldset {
border: 0;
margin: 0;
padding: 0;
min-width: 0;
}
legend {
padding: 0;
margin-bottom: 0.5rem;
font-weight: 600;
}Points to know:
- Do not replace the legend with a styled heading to avoid the default look. Restyle the legend itself. Removing it removes the accessible group name.
min-width: 0on the fieldset fixes a long-standing default where afieldsetrefuses to shrink below the width of its content. This causes horizontal overflow in narrow layouts, especially with flex or grid parents.- Legends do not wrap well in some older browsers and are hard to lay out with flexbox or grid. If you need a complex layout, a common approach is to keep the
legendas a plain block and apply layout to adivinside thefieldset. - Do not hide the legend visually unless you must. Visible text helps everyone. If a design truly needs it hidden, use a visually hidden technique that keeps it available to assistive technology, and not
display: none, which removes it from the accessibility tree. - A disabled
fieldsetdisables every control inside it. This is useful for turning off a whole section, such as a billing address when "same as delivery" is checked. Disabled controls are also not submitted with the form, so check that you do not lose data you needed.
Alternatives and when they apply
Not every group needs fieldset. The ARIA pattern role="group" with aria-labelledby gives similar semantics on a div. For radios, role="radiogroup" does the same. Prefer native fieldset and legend when you can, because they need no extra attributes and work without JavaScript. Use the ARIA version only when a native element cannot be used, such as in a custom component library that wraps controls in non-form elements.
Avoid heavy nesting. A fieldset inside a fieldset makes the screen reader read two legends at each control. This is acceptable for a real hierarchy, such as "Passenger 1" containing "Date of birth", but is noise when added only for visual structure. When the hierarchy is real, keep both legends short.
For long forms, grouping helps in a second way. A form split into clearly named sections lets users scan, and lets screen reader users jump between groups. Do not use fieldset as a general layout wrapper for every section, though. A page section with a heading is often the better choice when the section contains many independent questions rather than one question answered by several controls.
Checking your work
A quick test: tab through the form with a screen reader on, and listen to what is announced when you land on each control. If any control makes you ask "what is this for?", it probably needs a group name. You can also inspect the accessibility tree in browser developer tools to confirm that each group shows the expected name.
Exercise
A form asks "Which of these do you use?" with three checkboxes labelled "Phone", "Laptop", and "Tablet". Each checkbox has a correct label with matching for and id. A screen reader user says they do not know what the question is when they reach "Laptop". What is missing, and how would you fix it?
Show answer
The checkboxes have no group name. Wrap them in a fieldset and put the question in a legend as its first child, for example <legend>Which of these do you use?</legend>. Keep each checkbox's own label. The screen reader will then announce the legend together with "Laptop, checkbox".
Exercise
A page has two address blocks, each in a fieldset with the legend "Address". Why is this a problem, and what is the fix?
Show answer
Both groups have the same accessible name, so a screen reader user cannot tell which one they are in. Give each legend a distinct purpose, such as "Delivery address" and "Billing address".
Exercise
A developer wraps a single "Full name" text input in a fieldset with the legend "Full name", and also keeps the label "Full name". What does this change for the user, and what should the developer do?
Show answer
The screen reader announces "Full name" twice, once for the group and once for the control, which adds noise and no information. A single input needs only its label. The developer should remove the fieldset and legend.