HTML: fieldset
The <fieldset> element groups related form controls, while <legend> gives the group a name.
What you will learn
- How to group related controls
- Why every group should have a useful legend
- How grouping helps people understand a form
Radio button group
<fieldset>
<legend>Preferred contact method</legend>
<label><input type="radio" name="contact" value="email"> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>The legend explains what the choices have in common. The shared name makes the radio buttons one choice group.
When to use it
Use fieldset for related checkboxes, radio buttons, address sections, or other controls that form one logical group. It provides structure for both visual users and assistive technology.
Common mistakes
- Do not use fieldset only as a generic layout container; use a structural element or CSS when no form group exists.
- Do not omit the
legendwhen the group needs a name. - Keep each control’s individual
labelas well as the group’s legend.