Back to HTML

Grouping Fields with fieldset and legend

Coding Tutorials Team 26 June 2026

Individual labels name individual fields. What they cannot do is name the question a group of fields answers together, and that gap is what the fieldset element fills.

The problem it solves

Consider a set of radio buttons offering delivery options. Each has a label, so a screen reader announces standard, express and next day. What it does not announce is that these are delivery options at all, because nothing in the markup connects the heading to the controls.

A sighted user infers the relationship from proximity and layout. A screen reader user encounters the controls one at a time with no context, and there is nothing to fall back on.

The structure

<fieldset> <legend>Delivery speed</legend> <input type="radio" id="std" name="delivery" value="standard" checked> <label for="std">Standard, 3 to 5 days</label> <input type="radio" id="exp" name="delivery" value="express"> <label for="exp">Express, next working day</label> </fieldset>

The legend must be the first child of the fieldset. Placing it anywhere else breaks the association, and it is the one structural rule the element imposes.

With this in place, a screen reader announces the legend alongside each option, so the user hears the question and the answer together.

Where it is required in practice

Radio groups are the clearest case, since a group with no legend is genuinely incomplete. Groups of related checkboxes answering one question are the same.

Beyond that, a set of fields forming a single logical unit benefits: a date split across three inputs, an address block, a name divided into parts. In each case the individual labels make sense only within a context the fieldset supplies.

Where it is unnecessary

A fieldset around every field, or around a form with a handful of unrelated inputs, adds announcements without adding meaning. A screen reader repeats the legend for each control inside, so an unnecessary group makes the form slower to work through rather than clearer.

The test is whether the fields answer one question together. A name field and an email field do not, so wrapping them proves nothing.

Nesting

Fieldsets can be nested, and each legend is announced in turn. Two levels is usually the practical limit before the repeated announcements become tiring. Where a form is complex enough to want more, headings and separate sections are often a better structure than deeper nesting.

Styling it

The default appearance is a border with the legend interrupting the top edge, which is distinctive and rarely matches a design. It can be reset.

fieldset { border: 0; margin: 0; padding: 0; } legend { padding: 0; font-weight: 600; }

Removing the visible border does not remove the semantic grouping, which is the important point. The relationship is in the markup rather than in the appearance, so you can style it however the design requires.

The legend has historically been awkward to position, since it participates in layout unusually and ignores some properties. Behaviour has improved considerably, though it remains the element most likely to resist a layout technique that works everywhere else. Wrapping the legend text in a span gives you something conventional to style when it does.

Hiding the legend

Where a design has no room for a visible group heading, hide it with the same visually hidden technique used for labels, which moves it out of view while keeping it in the accessibility tree.

Using display none removes it entirely and defeats the purpose. This is a frequent mistake, because the markup looks correct and the benefit has silently gone.

The disabled attribute cascades

Setting disabled on a fieldset disables every control inside it, which is a convenient way to switch off a whole section, for instance while an asynchronous request is in progress.

Remember that disabled controls are not submitted, so disabling a section removes its data from the request entirely. Where the values should still be sent but not edited, readonly on the individual controls is the attribute you want instead.

An alternative for custom groups

Where a fieldset genuinely will not fit the layout, the same relationship can be expressed with a group role on a container and an aria-labelledby attribute pointing at the id of the heading that names it. This produces a comparable announcement without the element's layout quirks.

Use it as a fallback rather than a first choice. The native element is better supported and communicates its purpose to anyone reading the markup, which the attribute combination does less clearly.

Fieldset and multi-step forms

On a long form split into steps, each step is a natural fieldset with the step title as its legend. This gives the structure meaning even when the visual presentation only shows one step at a time, and it keeps the form coherent if the steps are ever displayed together, such as on a review screen or in print.