Sub topic 10
Grouping related controls with fieldset and legend
This chapter assumes you can write a basic form, label controls with for and id, and know that radio buttons sharing a name form one group. Here you will learn how fieldset and legend give a set of controls a shared, accessible name.
Why labels alone are not enough
A label names one control. Some questions need a name for the whole set. Three radio buttons labeled "Free", "Pro", and "Team" make no sense to a screen reader user unless they also hear the question, such as "Plan". The fieldset element groups the controls. The legend element is the group's caption.
When focus enters the group, assistive technology typically announces the legend, then the control's own label. A user hears something like "Plan, group. Free, radio button, 1 of 3."
Basic structure
<form action="/signup" method="post">
<fieldset>
<legend>Plan</legend>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>
<button type="submit">Create account</button>
</form>Rules to follow:
- The
legendmust be the first child of thefieldset. - Each control inside still needs its own label.
- The
fieldsetandlegendadd no field to the submitted data. Only the controls'nameandvaluepairs are sent.
When to use a fieldset
Use one when several controls answer a single question or belong to one topic.
| Situation | Group with fieldset? |
|---|---|
| Radio buttons sharing a `name` | Yes. The legend is the question. |
| A set of related checkboxes, such as interests | Yes. |
| Parts of one value, such as day, month, and year | Yes. |
| Shipping address and billing address sections | Yes, one per section. |
| A single text input | No. A label is enough. |
| A single checkbox such as "I agree" | No. Its own label is enough. |
Disabling a whole group
Setting disabled on a fieldset disables every control inside it. This is useful when a section only applies after a choice is made.
<fieldset disabled>
<legend>Billing address</legend>
<label for="street">Street</label>
<input id="street" name="street" type="text">
</fieldset>Disabled controls are not focusable and are not submitted. Controls inside the legend are the exception and stay enabled.
Common errors
- Using a legend as a visual heading only. The legend is the group's accessible name. Do not replace it with a
divorpthat merely looks like a caption. - Repeating the legend in every label. Write "Free" rather than "Plan: Free". The legend already supplies the context.
- Nesting fieldsets deeply. Screen readers may repeat every legend in the chain. Keep nesting to one level when you can.
- Fighting default styles. Browsers draw a border and padding around a
fieldset, and the legend sits on the border. Reset them with CSS if needed, but keep the elements.
fieldset {
border: 0;
margin: 0;
padding: 0;
}If you set border: 0 and padding: 0, the legend still works for assistive technology. Add spacing back with margins so the group stays readable.
Exercise
A form asks "Preferred contact method" with three radio buttons labeled Email, Phone, and Post. How do you mark it up so a screen reader announces the question, and does the fieldset add anything to the submitted data?
Show answer
Wrap the three radio buttons in a fieldset, put a legend reading "Preferred contact method" as its first child, and give each radio its own label and the same name, such as contact. The fieldset and legend add nothing to the submitted data. Only the chosen radio's name and value, for example contact=email, are sent.
Exercise
What happens to the controls inside a fieldset that has the disabled attribute, and are their values submitted?
Show answer
All controls inside are disabled, so they cannot be focused or edited, and their values are not submitted. Controls inside the legend are not disabled.