Fieldset and Legend
You have been building forms where all the inputs just sit one after another in a single long list. That works fine when a form is short. But forms in the real world can get long. A job application. A checkout page. A user settings page. When a form has fifteen or twenty fields, just stacking them all together starts to feel overwhelming for the user.
That is the problem that <fieldset> and <legend> solve.
What is a Fieldset
A <fieldset> is a way to group related inputs together inside a form. You wrap it around a set of
inputs that belong to the same idea, and the browser draws a box around them automatically.
That box is not just decorative. It is a signal to the user that these inputs are connected to each other. They are one group. They belong together.
Think about a long checkout form. You have personal details at the top, then a shipping address section, then a
payment section. Each of those is a natural group. Wrapping each group in a <fieldset> makes it
visually obvious where one section ends and the next begins.
What is a Legend
A <legend> is the label for the whole fieldset. It sits at the very top of the group and tells the
user what this group of inputs is about.
The legend text appears right at the top edge of the box the fieldset draws. It works like a heading for that group. The user can immediately see that everything inside this box belongs to the Personal Details section.
The <legend> must always be the very first thing inside a <fieldset>. Not
after the first input, not somewhere in the middle. Always first.
Why This Matters Beyond Just Looks
Grouping inputs with fieldset does something important for screen readers too. When a visually impaired user navigates into a fieldset, the screen reader announces the legend first. So instead of just hearing "First Name, text box" they hear "Personal Details, First Name, text box." They always know which section they are in.
This is the same principle you learned with labels. HTML gives you the tools to build forms that work for every single user, not just the ones who can see the screen perfectly. Using fieldset and legend is part of that.
A Real Example with Multiple Groups
Here is where fieldset really starts to show its value. Imagine a shipping form broken into three clear sections.
Look at how much clearer this is compared to just dumping all those inputs in a row. A user landing on this form instantly understands the structure. Fill in your details, then your address, then pick a delivery option. Three groups, three fieldsets, three legends. The form tells its own story.
Fieldset Works Great for Radio Buttons and Checkboxes
One of the most common places you will see fieldset used in the wild is around radio button groups and checkbox groups. And it makes total sense when you think about it. A group of radio buttons is literally a group of related inputs. They are all answering the same question.
Without the fieldset and legend, a screen reader would just say "Beginner, radio button. Intermediate, radio button." The user has no idea what question these options are answering. With the fieldset and legend, they hear "What is your experience level, Beginner, radio button." Now it makes complete sense.
This is one of those patterns that professional developers use on every single form they build. Now you know it too.
The disabled Attribute on a Fieldset
Here is a bonus that most beginners do not know about. You can put disabled on an entire fieldset and
every single input inside it gets disabled at once. You do not have to go through and add disabled to
each one individually.
Every input inside that fieldset is now greyed out and unclickable. This comes in handy when you have a section of a form that should only become active after the user completes a previous step. You would use JavaScript to remove the disabled attribute at the right moment, but the HTML structure that makes it possible is right here.
Up next you will learn about the <input type="file"> which lets users upload files through a form,
and a few other specialist input types that come in very handy in real projects.
Have anything to say about this lesson?
Your feedback helps improve these tutorials. If something was confusing or missing, let us know.