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 share a name. You should also know how a checkbox and a radio button submit values. Here you will learn how fieldset and legend group controls, what that grouping does for assistive technology, and which mistakes cause trouble in practice.
Why grouping matters
A label names one control. Some questions need a name that applies to several controls at once. Take the question "Plan" with the answers "Free" and "Pro". Each radio button has its own label, but neither label says what is being chosen. A sighted user sees the heading above the options. A screen reader user tabbing into "Pro" hears only "Pro, radio button", unless the group has a name.
The fieldset element groups controls. The legend element gives the group a caption. Browsers expose the pair to assistive technology as a group with an accessible name. When focus enters the group, a screen reader typically announces the legend along with the focused control, for example "Plan, group. Pro, radio button, 2 of 2".
<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>Grouping and naming are separate jobs. The shared name attribute makes the radio buttons mutually exclusive and decides what is submitted. The fieldset and legend only describe the group to the user. Removing the fieldset does not change the submitted data.
flowchart TD
F[form] --> FS[fieldset]
FS --> L[legend: Plan]
FS --> R1[label + radio: Free]
FS --> R2[label + radio: Pro]The diagram above is illustrative only. The next block is the one that follows the required format.
When to use a fieldset
Use a fieldset when several controls answer one question or belong to one topic. Common cases:
- Radio button groups. Nearly always. The legend is the question and each label is an answer.
- Checkbox groups. A set of related checkboxes such as "Notify me about" with options for email, SMS, and push.
- Multi-part values. A date split into day, month, and year fields, or an address block.
- Sections of a long form. "Billing address" and "Shipping address" can each be a fieldset.
Do not wrap a single control in a fieldset. A label already names it. Do not use a fieldset only to get a visual border. Use a div and CSS for that.
<fieldset>
<legend>Notify me by</legend>
<label><input type="checkbox" name="notify" value="email"> Email</label>
<label><input type="checkbox" name="notify" value="sms"> SMS</label>
<label><input type="checkbox" name="notify" value="push"> Push</label>
</fieldset>When the user checks two boxes, the form sends notify twice, once per checked box. The grouping has no effect on that.
Rules for legend
The legend has a strict position. It must be the first child of the fieldset. If other content comes before it, browsers do not treat it as the group caption, and the group loses its accessible name in some browsers.
Keep the legend short and write it as the question or topic. Screen readers repeat it for every control in the group, so a long legend becomes tiresome. If you need longer instructions, put them in a paragraph and connect it with aria-describedby on the fieldset.
<fieldset aria-describedby="plan-help">
<legend>Plan</legend>
<p id="plan-help">You can change plans later in your account settings.</p>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>A legend can contain phrasing content, but a heading inside a legend is allowed in current HTML and can cause inconsistent announcements across screen readers. Prefer plain text. If the form needs a section heading and a group caption, use both: a heading outside, a short legend inside.
Disabling a whole group
A fieldset accepts the disabled attribute. It disables every form control inside it, apart from controls inside the first legend. Disabled controls cannot be focused or edited, and they are not submitted.
<fieldset id="shipping" disabled>
<legend>Shipping address</legend>
<label for="street">Street</label>
<input id="street" name="street" type="text">
<label for="city">City</label>
<input id="city" name="city" type="text">
</fieldset>
<label>
<input type="checkbox" id="same" checked> Shipping is the same as billing
</label>
<script>
const shipping = document.getElementById('shipping');
const same = document.getElementById('same');
same.addEventListener('change', () => {
shipping.disabled = same.checked;
});
</script>This is shorter than setting disabled on each input. It also affects validation: disabled controls are barred from constraint validation, so a required field inside a disabled fieldset does not block submission. The reverse also matters. Data from a disabled group is missing from the request, so the server must not assume those fields exist.
The disabled attribute does not cascade to the legend text itself in the sense of hiding it. The legend stays visible, but a browser may grey it out through default styles.
The fieldset and the form attribute
A fieldset has a form attribute and a name attribute. The form attribute associates the fieldset with a form by id even when the fieldset sits outside that form's markup. The name attribute is used by scripts: the fieldset appears in form.elements, and its own elements collection lists the controls inside it.
const form = document.querySelector('form');
const group = form.elements.namedItem('plan-group');
console.log(group.elements.length);For that example, the fieldset needs name="plan-group". A fieldset is never part of the submitted data. It has a name, but it contributes no name/value pair.
Styling and layout gotchas
Browsers give a fieldset a default border, padding, and a min-inline-size: min-content. The last rule causes the most trouble. A fieldset containing wide content, such as a long unbroken string or a wide table, refuses to shrink with its container and can overflow on small screens. The fix is a reset:
fieldset {
min-inline-size: 0;
border: 1px solid #999;
padding: 0.75rem 1rem;
margin: 0 0 1rem;
}The legend is also unusual to lay out. It is rendered in the fieldset's border, with the border interrupted around it. Many layout properties, including display: flex and display: grid on the fieldset itself, were unreliable in older browsers. Current browsers handle them, but check your support targets. If you want a fully custom caption layout, a common technique is to make the legend float: left; width: 100% and place a wrapper after it. Another is to put the layout on an inner div instead of the fieldset.
Do not remove the legend with display: none. That also removes it from the accessibility tree. If you want it hidden visually but available to screen readers, use a visually hidden class:
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}Common errors
| Mistake | Effect | Fix |
|---|---|---|
| No fieldset around radio buttons | Screen reader users hear answers without the question | Wrap the radios in a fieldset with a legend |
| `legend` not the first child | Group may have no accessible name | Move it to the first position |
| Using a `label` or `p` as the group caption | No group semantics, and the caption is not announced with each control | Use `legend` |
| A fieldset around a single input | Extra announcements with no benefit | Use a label only |
| `display: none` on the legend | Group name removed for everyone | Use a visually hidden class |
| Expecting fieldset to affect submitted data | Nothing changes | Use `name` and `value` on the controls |
| `disabled` fieldset with required fields | Fields are skipped by validation and not sent | Make sure the server handles missing values |
| Nested fieldsets that repeat context | Verbose announcements | Nest only when the inner group needs the outer context |
Nesting
Fieldsets can be nested. A screen reader may read both legends, outer first. This suits cases such as "Billing details" containing a "Card expiry" group. Keep nesting to two levels at most, and make each legend meaningful in isolation.
Exercise
A form has three radio buttons named "size" with the labels Small, Medium, and Large, each wrapped in its own label. A screen reader user hears only "Medium, radio button". What should you add, and where must it go? Also, does the change alter the data submitted to the server?
Show answer
Wrap the three radios in a fieldset and add a legend such as "Size" as the first child of the fieldset. The screen reader then announces the group name with each radio. The submitted data does not change. The value still comes from the shared name and the value of the checked radio, for example size=medium.
Exercise
A fieldset with the disabled attribute contains an input with name="phone" and the required attribute. The user submits the form. Does validation block submission, and is phone included in the request?
Show answer
Validation does not block submission, because disabled controls are barred from constraint validation. The phone field is not included in the request, because disabled controls are not submitted. The server should treat the field as optional or absent.