HTML Fieldset and Legend

HTML Fieldsets and Legends: Structuring Forms with Meaning

Forms often collect a variety of information — names, preferences, comments, agreements. To make forms more user-friendly and accessible, we can group related inputs using the <fieldset> and <legend> tags. These tags provide both structure and clarity.

What is a <fieldset>?

The <fieldset> element is used to group related form fields. It creates a visual box around those fields and tells screen readers that they belong together.

What is a <legend>?

The <legend> element is placed inside a fieldset. It acts as a caption, describing the purpose of the grouped fields.

Basic Example: Grouping Fruit Preferences

<fieldset>
  <legend>Favorite Fruits</legend>

  <input type="checkbox" id="apple" name="fruit[]" value="apple">
  <label for="apple">Apple</label><br>

  <input type="checkbox" id="banana" name="fruit[]" value="banana">
  <label for="banana">Banana</label><br>

  <input type="checkbox" id="cherry" name="fruit[]" value="cherry">
  <label for="cherry">Cherry</label>
</fieldset>
[ Favorite Fruits ]
☐ Apple  
☐ Banana  
☐ Cherry

The fieldset creates a visual grouping, and the legend acts like a label for the group.

Why Use Fieldsets and Legends?

  • Clarity: Visually separate sections of a form
  • Accessibility: Screen readers use the legend to describe grouped inputs
  • Usability: Helps users understand which inputs are related

Example: Contact Information Section

<fieldset>
  <legend>Contact Info</legend>

  <label for="name">Name:</label><br>
  <input type="text" id="name" name="name"><br>

  <label for="email">Email:</label><br>
  <input type="email" id="email" name="email">
</fieldset>
[ Contact Info ]
Name:  [             ]  
Email: [             ]

Multiple Fieldsets in a Single Form

You can use more than one fieldset to organize different parts of a longer form.

<form action="/submit" method="post">

  <fieldset>
    <legend>Fruit Selection</legend>
    <input type="radio" name="fruit" id="apple" value="apple">
    <label for="apple">Apple</label><br>
    <input type="radio" name="fruit" id="banana" value="banana">
    <label for="banana">Banana</label>
  </fieldset><br>

  <fieldset>
    <legend>Your Details</legend>
    <label for="name">Name:</label><br>
    <input type="text" id="name" name="name" required>
  </fieldset><br>

  <input type="submit" value="Submit">

</form>
[ Fruit Selection ]
◯ Apple  
◯ Banana

[ Your Details ]
Name: [           ]

[Submit]

Styling Tip

By default, fieldsets have a border and some padding. You can style them with CSS for a more modern look — but even unstyled, they add tremendous structure and accessibility value.

Best Practices

  • Use <fieldset> to group related controls (like contact info or choices)
  • Use <legend> to describe the purpose of each group
  • Avoid nesting fieldsets unless you have a very specific reason

Summary

<fieldset> and <legend> help structure your forms logically and semantically. You’ve now learned how to:

  • Group related inputs using <fieldset>
  • Describe each group using <legend>
  • Improve both visual structure and accessibility

Check your understanding 1

What is the primary purpose of using the <fieldset> tag in an HTML form?
Choose one answer. You can try again.

Answer and explanation

To visually and semantically group related form fields

The <fieldset> tag groups form inputs that are logically connected, improving both layout and accessibility.

Check your understanding 2

The <legend> element must be used inside a <fieldset>.
Choose one answer. You can try again.

Answer and explanation

True

Correct. <legend> provides a caption for the grouped fields and must be the first child of a <fieldset> element.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. Grouping student personal information
  2. Labeling grouped input fields with <legend>
  3. Styling text input fields with <legend>
  4. Separating parent/guardian details from student data
Which of the following are valid uses of <fieldset> and <legend> in a school admission form?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 4

Grouping and labeling related fields like student info and guardian info improves clarity. <legend> isn’t used for styling inputs directly.

Check your understanding 4

Which code snippet correctly implements a fieldset with a label?
Choose one answer. You can try again.

Answer and explanation

&lt;fieldset&gt;&lt;legend&gt;Student Info&lt;/legend&gt;...&lt;/fieldset&gt;

The correct order is <fieldset> followed by <legend> inside it. This is valid syntax for grouped and captioned form sections.

Check your understanding 5

Using <fieldset> and <legend> improves accessibility for screen readers.
Choose one answer. You can try again.

Answer and explanation

True

Yes. These elements help assistive technologies understand the context of grouped form controls, making forms more accessible.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. <fieldset><legend>Guardian Details</legend><input name="parent_name"></fieldset>
  2. <fieldset><legend>Address</legend><textarea></textarea></fieldset>
  3. <legend>Exam Choices</legend><fieldset><input name="subject"></fieldset>
  4. <fieldset legend="Academic Info">...</fieldset>
Which of the following code snippets demonstrate appropriate use of <fieldset> and <legend>?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2

Options 0 and 1 correctly use <fieldset> with a nested <legend>. Option 2 places <legend> outside the fieldset. Option 3 uses an invalid attribute.

What’s Next?

Ready to style your form for better UX? In the next tutorial, we’ll explore Styling Form Elements with CSS to make your HTML forms look just as polished as they work.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Form Advanced Controls →