Fieldset and legend: which form fields to group, and why a heading above them isn't enough


On Tab, a screen reader usually skips the <h2> above a group of radio buttons. It announces the focused button and its group's name, taken from the <legend> of a <fieldset>. NVDA, a free screen reader, says that name once, as the focus enters the group, followed by “grouping”, so the question comes before the choices. The <h2> can even move inside the legend and stay a heading.
Naming a group of form fields
#Does a heading above radio buttons replace fieldset and legend?
No. On Tab, a screen reader announces the focused button and the name of the group the keyboard focus enters. A heading above the buttons is neither. WebAIM's guide to forms warns that text placed between form fields is usually skipped.
The <fieldset> element wraps fields that belong together, such as the choices for a single question. Its <legend> gives the group its name, the same way a <label> names a field. Someone filling in the form with the keyboard and a screen reader then hears the question along with the first choice. Without a <legend>, the group has no name. The W3C, which writes web standards, also advises a visual grouping on screen.
In Firefox with NVDA, the free screen reader for Windows, a group of radio buttons behaves in four ways:
- all the buttons in the group make up a single Tab stop;
- on the way in, NVDA says the legend, “grouping”, then the button that has focus;
- moving between buttons with the arrow keys doesn't bring the legend back;
- without a legend, the
fieldsetisn't announced.
On the checkout page of Quill Stationery, our demo shop, “Shipping method” is a 22 px <h2> sitting above the three buttons. In Firefox, starting from the “Email address” field, we counted a single Tab stop for all three.
Firefox's accessibility inspector shows the accessibility tree, the version of the page that screen readers receive. There, the heading sits level with the buttons' labels. NVDA announces the containers of the focused field as the focus moves into them, as one of its developers explained in 2011. A heading next to the buttons doesn't contain them. As a result, the listener hears “Pick up at the workshop”, its radio button role and its position among the three, with no “Shipping method”.
Marker 1. The row of the “Shipping method” heading, at the same level as the labels of the buttons. Marker 2. The three labels and their radio buttons, with no named group above them.#When should you use fieldset and legend, and which fields don't need them?
A fieldset goes around fields whose labels, read on their own, don't say what they answer: the radio buttons of one question, or blocks of fields with identical labels. In WCAG, the international accessibility guidelines, this grouping falls under the success criterion Info and Relationships: a testable rule at level A, the minimum. Technique H71 shows how to meet it with these two elements. In the EU, EN 301 549 requires every web page to meet that criterion. It's the voluntary standard drafted to support the European Accessibility Act.
The number of fields doesn't matter. The W3C's rule of thumb is that a group of fields inside a larger form gets a fieldset and legend when it needs its own heading to describe it. WebAIM adds that the pair should only be used when the fields need a shared label, so a single checkbox whose label makes sense alone goes without one. A date split into day, month and year is a group, and the British government's GOV.UK Design System puts it in a fieldset.
Marker 1. The three shipping method radio buttons, under their heading. Marker 2. The Shipping address block, four fields. Marker 3. The Billing address block, with the same four labels. Marker 4. The Billing address same as shipping address checkbox, on its own. Marker 5. The Promo code field, alone in its form.| Group | What the labels are missing | Legend |
|---|---|---|
| marker 1 Three radio buttons | The question being asked | Shipping method |
| marker 2 Four address fields | The block they belong to | Shipping address |
| marker 3 The same four labels | The block they belong to | Billing address |
| Field | What's enough |
|---|---|
| marker 4 “Billing address same as shipping address” checkbox | Its label |
| marker 5 “Promo code” form, a single field | Its label |
For radio buttons, the W3C tutorial says to always group them. The two address blocks repeat “Full name”, “Street address”, “Postal code” and “City”. Without a fieldset, the two “Postal code” fields sound alike. With one fieldset per block, NVDA names the block on the way in, then doesn't repeat it on the fields that follow.
The single checkbox and the one-field form keep their label. No fieldset goes around the whole form either: it would contain the other three, and the W3C advises against nesting fieldsets when there's no need.
Accessible forms in HTML: required fields, error messages and validation
#How do you style fieldset and legend without changing how the form looks?
Put the <h2> inside the <legend>, then strip the <fieldset> of its border and spacing: the heading keeps its look, and the group gets its name. Three CSS rules are enough.
#How do you keep the heading's look inside the legend?
The HTML specification allows a heading inside a legend: the legend names the group, and the heading places it in the outline of the page. In Firefox's inspector, we found the <h2> under the legend, still marked as a heading. A screen reader still lists it among the page's headings.
Marker 1. The “Shipping method” heading, above the three radio buttons.Avoid
<div class="block">
<h2>Shipping method</h2>
<div class="choices">
<label><input type="radio" name="shipping" value="pickup"> Pick up at the workshop</label>
<label><input type="radio" name="shipping" value="home"> Home delivery</label>
<label><input type="radio" name="shipping" value="locker"> Parcel locker</label>
</div>
</div>Correct
<fieldset class="block">
<legend><h2>Shipping method</h2></legend>
<div class="choices">
<label><input type="radio" name="shipping" value="pickup"> Pick up at the workshop</label>
<label><input type="radio" name="shipping" value="home"> Home delivery</label>
<label><input type="radio" name="shipping" value="locker"> Parcel locker</label>
</div>
</fieldset>By default, the browser draws the legend across the top border and gives the fieldset a border, margins and padding. The three rules below come from the base stylesheet of Bootstrap 5.3.9. They remove that frame and float the legend, which lifts it off the border and lays it out like an ordinary block. That's why, by design, a Bootstrap 5 legend no longer sits on the fieldset's border.
fieldset { min-width: 0; margin: 0; padding: 0; border: 0; }
legend { float: left; width: 100%; padding: 0; }
legend + * { clear: left; }The legend can also be hidden from view. Shrunk to 1 px by Bootstrap's .visually-hidden class, it still names the group: we saw this in the accessibility trees of both Firefox and Chrome. A 22 px <h2> above the buttons, by contrast, usually stays silent on Tab.
Hidden with display: none, though, the legend drops out of the accessibility tree, and under the W3C's rules the group then loses its name. Chrome follows those rules. Firefox keeps the name: a gap reported to Mozilla in 2018 that is still open. We reproduced it on our demo, so check in both browsers.
Two patterns meet the Info and Relationships criterion, and we recommend the first:
- The
<h2>inside the legend: one text, the same look, and a heading that still appears in the list of headings; it takes the three CSS rules. - A visible
<h2>and a hidden legend: acceptable but not recommended. A screen reader user hears both texts when reading the page, as the GOV.UK Design System warns, and the two have to be kept identical.
The aria-hidden attribute: when to use it, and what to do when Tab reaches what it hides
#How do you check that the group has a name?
In Firefox's accessibility inspector, select a radio button: it should sit inside a group that carries the text of the legend. On the fixed page, we found “Shipping method” there.
Marker 1. The row of the group named “Shipping method”. Marker 2. The three labels and their radio buttons, inside that group.With NVDA and Firefox, start from the “Email address” field and press Tab. NVDA should say the legend, “grouping”, then the button, and on Down arrow nothing more about the group. We expect this from the change log of NV Access, the company that makes NVDA, and from an issue in its code repository. Our guides explain how to install NVDA and run it on a Mac in a virtual machine.
From “Email address”, NVDA says the legend and the word “grouping”, then the first button.
The arrow checks the next button; NVDA doesn't repeat the legend.
The address block announces itself in turn, with its legend.
On the Quill Stationery page, three fieldset elements were enough. The checkbox and the promo code keep their label. In Chrome, our before and after screenshots don't differ by a single pixel, on a wide screen or on a phone. Firefox's inspector counts three named groups. On Tab, the <h2> that the screen reader used to skip above the radio buttons has become the name of their group.
Page by page, the Includdy scan and its manual guides for forms find radio button and checkbox groups that have no name. For each one, Includdy's guided fix shows the faulty HTML beside a fixed version with a legend. Its AI suggests the fix, and you review it before it goes into your code.


