Fieldset, Legend and Grouping
How fieldset and legend group related controls, why a group of radio buttons needs a group name, and how a disabled fieldset switches off everything inside it.
Fieldset, Legend and Grouping
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A `<fieldset>` groups related controls and its first child `<legend>` names the group. For radio buttons and checkbox sets, the legend is the question and each label is one answer.
- Without a group, a screen reader announces "Email, radio button" with no hint of what the choice is about. With a legend, the group has a name: in a test, the accessibility tree listed `group "Contact method"` containing the radios.
- `<fieldset disabled>` disables every control inside it. In a test, the controls and a button inside were `:disabled`, and the disabled controls were left out of the form data.
- The exception is the first `<legend>`: controls inside it stay enabled. In a test, an input in the legend was still submitted.
- Radio buttons with the same `name` form one choice. Putting `required` on any one makes the group required, and picking any one satisfies it.
A label names one control. What names a set?
A label names a single control: “Email” belongs to one input. Some questions are answered by a set of controls. “How should we contact you?” is answered by picking one of Email, Phone or Post. Each radio button has its own label, but none of them says what the choice is about.
A sighted user sees the question above the options. A screen reader user hears the controls one at a time, so without more help they hear “Email, radio button” and cannot tell what question they are answering. A <fieldset> and <legend> fix that: the legend is the group’s name, and the browser announces it with each control in the group.
The markup
<fieldset>
<legend>How should we contact you?</legend>
<label><input type="radio" name="contact" value="email"> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>
The <legend> must be the first child of the <fieldset>. In a test of the accessibility tree through Playwright, a fieldset with <legend>Contact method</legend> showed up as group "Contact method" containing the radios, in Chromium and Firefox. The group’s name comes from the legend, which is how a screen reader can announce “Contact method, group” on entry.
Browsers draw a fieldset with a border and the legend breaking the top edge. You can restyle or remove that: border: 0; padding: 0; is common. Do not replace the element with a <div> to avoid the default look, since you would lose the semantics.
Use a fieldset whenever a question has several controls as its answer: a set of radio buttons, a set of checkboxes (“Which topics interest you?”), or a group of related inputs like the parts of an address. Do not use one around every field, since a group around a single control just adds noise.
Radio buttons share a name
Radio buttons belong together because they have the same name. The browser lets one in the set be checked at a time, and the submission sends one name and value pair for it. Required works at group level: in a test, with two radios sharing a name and required only on the first, the first radio was valueMissing until either was checked, and after checking the second radio neither was missing. Setting required on every radio is common, and harmless.
Check boxes have no such grouping. Each one is separate, even if they share a name, and the server receives one pair per checked box. A set of checkboxes needs a fieldset all the more.
Disabling a whole group
disabled on a fieldset applies to everything inside. In a test, a <fieldset disabled> holding an input and a button made both :disabled, and the form data left out the input. That makes a fieldset a handy switch: toggle one attribute to turn off a whole section, such as a “billing address” block when “same as shipping” is checked, instead of toggling each control.
One quirk: controls inside the fieldset’s first <legend> are exempt. In the same test, an input inside that legend stayed enabled (:disabled was false) and was still submitted with the form. The reason is practical: a legend might hold a checkbox that turns the group on and off, which would be a trap if it disabled itself.
fieldset.elements lists the controls inside, which is useful in scripts. The test listed three for the disabled fieldset (the input in the legend, the other input and the button).
ARIA as a fallback
If you cannot change the markup, ARIA can give the same result: role="group" on a container, with aria-labelledby pointing at the element holding the question. For radios, role="radiogroup" is the more specific choice. Use the native elements when you can, since the browser supplies the role and name for free. The ARIA lesson covers the roles, and the accessible names lesson covers how aria-labelledby works.
What the video covers
The embed is a clip, 4:12 to 7:05, from Learndev’s “HTML Forms and Inputs in 7 Minutes!” (May 2024), the same video the forms and labels lesson clips from its opening. The chapter list for this range reads HTML radio buttons, HTML checkbox input, HTML date input and HTML fieldset and legend, so the fieldset itself is only the last of those, about a minute. This is the weakest pick in the forms group: it shows the markup but, going by the chapter list, does not make the accessibility argument. I could not read a transcript. The runner-up is a seven-minute overview of form accessibility that spends longer on labels and errors than on groups. The accessibility-tree results and the disabled-fieldset behavior in this lesson are mine, tested in headless Chromium and Firefox (the disabled behavior in Chromium and Firefox; the radio-group result in all three browsers). Screen reader announcements were not tested.
Where this fits
This follows textarea, select and datalist and is followed by buttons and submit behavior. Grouping returns when accessible names and descriptions covers how groups are named.
Check yourself
A form asks "How should we contact you?" with two radio buttons labelled Email and Phone, and the question is a plain paragraph above them. What does a screen reader user miss?
The question. They hear "Email, radio button, 1 of 2" and "Phone, radio button, 2 of 2" with no idea what the choice is for. Wrapping the radios in a `<fieldset>` with `<legend>How should we contact you?</legend>` gives the group a name, so it is announced as part of each radio.
What happens to the controls inside `<fieldset disabled>`, and what is left out of the submitted data?
They are all disabled, they match `:disabled`, and the browser leaves them out of the submission, just like any disabled control. The one exception is anything inside the fieldset's first `<legend>`, which stays enabled.
Two radio buttons share `name="cm"` and only the first has `required`. The user picks the second one. Is the group valid?
Yes. A radio group is satisfied once any button in it is checked, and one `required` is enough to make the group required. In a test, the first radio was `valueMissing` while none was checked and not after the second was checked.
You cannot use `<fieldset>` because of an existing layout. How else do you group controls for assistive technology?
Use `role="group"` (or `role="radiogroup"` for radios) on a container and name it with `aria-labelledby` pointing at the question's element. Native `<fieldset>` and `<legend>` is better when you can use it, since the browser already supplies the role and the name.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.