Course contentsShow
← Web Development
Lesson 103 of 2,9822. HTML FundamentalsFree lesson

The <fieldset> and <legend> Elements

Grouping related form controls with visible borders and descriptive legends.

The <fieldset> and <legend> Elements

What you'll learn: How to visually and semantically group related form controls together using <fieldset> and <legend>.

What Are They?

Think of a <fieldset> as a labeled box that groups related form controls together. Just like organizing physical files into folders with labels, <fieldset> creates a visual and semantic container for related inputs. The <legend> element acts as the title or label for that group, appearing as text embedded in the box's border.

Why Use Them?

Long forms can be overwhelming. When you have many inputs (like contact details, shipping address, and payment info), grouping them makes the form easier to scan and understand. Screen readers also use these elements to help users navigate forms more efficiently, announcing the group name when users enter that section.

How They Work

The <fieldset> wraps around your related form controls, while the <legend> must be the first child inside:

<form>
  <fieldset>
    <legend>Contact Information</legend>
    <label for="name">Name:</label>
    <input type="text" id="name">
    
    <label for="email">Email:</label>
    <input type="email" id="email">
  </fieldset>
  
  <fieldset>
    <legend>Preferences</legend>
    <label><input type="checkbox"> Newsletter</label>
    <label><input type="checkbox"> Updates</label>
  </fieldset>
</form>

Browsers automatically draw a border around the <fieldset> with the <legend> text positioned in the top-left corner, creating a clear visual separation.

When to Use Them

Use <fieldset> for logically related controls: contact sections, groups of radio buttons, shipping vs billing addresses, or preference checkboxes. Don't overuse them for every single input—only when grouping adds clarity.

Key Takeaway: Use <fieldset> to group related form controls with a visible border, and <legend> to provide a descriptive title for that group, improving both visual organization and accessibility.