Back to HTML Notes
Topic #169

fieldset

Learn how to use the HTML <fieldset> element to logically group related form controls and optionally disable them as a single unit.

What it is

The <fieldset> element creates a semantic grouping of related form elements, such as input fields, checkboxes, or radio buttons. It acts as a container that visually and programmatically binds these controls together. The associated <legend> element provides a caption for the group, which is crucial for accessibility tools like screen readers. When you apply the disabled attribute to a fieldset, all interactive controls within it become inactive simultaneously.

Why it matters

  • Accessibility: Screen readers announce the legend when focusing on any control inside the fieldset, providing context (e.g., "Shipping Address, Street Name").
  • Semantic Structure: It clarifies the relationship between inputs, making complex forms easier to parse for both humans and machines.
  • Bulk State Management: You can enable or disable an entire section of a form with one attribute change, rather than iterating through every input.
  • Visual Grouping: Browsers typically render a border around the fieldset, offering a default visual cue for grouped data.

Syntax or steps

  1. Open a <fieldset> tag.
  2. Immediately place a <legend> tag inside it to define the group's title.
  3. Add your form controls (inputs, selects, etc.) after the legend.
  4. Close the </fieldset> tag.
  5. To disable the group, add the boolean attribute disabled to the opening <fieldset> tag.

Example

<form>
  <fieldset disabled>
    <legend>Billing Information</legend>
    <label for="card">Card Number:</label>
    <input type="text" id="card" name="card_number">
    
    <label for="exp">Expiry Date:</label>
    <input type="text" id="exp" name="expiry">
  </fieldset>

  <fieldset>
    <legend>Contact Details</legend>
    <label for="email">Email:</label>
    <input type="email" id="email" name="user_email">
  </fieldset>
</form>

In this example, the "Billing Information" section is grayed out and uneditable because the parent <fieldset> has the disabled attribute. The "Contact Details" section remains active. The <legend> tags provide clear labels for each group.

Common mistakes

  • Omitting the Legend: Without a <legend>, assistive technologies may not convey the purpose of the group effectively. Always include one.
  • Nesting Fieldsets Incorrectly: While nesting is allowed, deep nesting can confuse users and screen readers. Keep groups flat where possible.
  • Forgetting Labels: A fieldset groups inputs, but individual inputs still need their own <label> elements linked via the for attribute.
  • Styling Conflicts: Default browser styles for fieldsets vary. Use CSS to normalize borders and padding if consistent design is required across browsers.

When to use it

Scenario Use Fieldset? Alternative
Grouping radio buttons or checkboxes Yes None (Fieldset is best practice)
Visually separating sections without logical relation No Use <div> with CSS
Disabling multiple inputs at once Yes JavaScript loop (less efficient)

Practice

Guided Exercise: Create a form with two fieldsets: "Personal Info" (Name, Age) and "Preferences" (Theme selection). Disable the "Preferences" fieldset by default.

Challenge: Write a small JavaScript snippet that removes the disabled attribute from the "Preferences" fieldset when a checkbox labeled "Enable Preferences" is checked.

Quick check

Question: What happens to the child inputs if you remove the disabled attribute from a <fieldset> using JavaScript?

Answer: All child inputs within that fieldset immediately become enabled and editable again.

Summary

The <fieldset> element is essential for creating accessible, well-structured forms by grouping related controls under a common label. It simplifies state management by allowing developers to enable or disable entire sections of a form with a single attribute.

Want to go beyond the notes?

Join Coding Now Tech Institute's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

fieldset – FAQs

Quick answers about learning fieldset in HTML.

This free note from Coding Now Tech Institute explains fieldset in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on Coding Now Tech Institute, including fieldset, is 100% free with no signup required.
With focused practice, most students grasp fieldset in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now