Codectionary / Developer documentation / HTML

<fieldset>, <legend>

The <fieldset> element groups related form controls together, typically rendered with a visible border. The <legend> element provides a caption for the group, describing what the enclosed fields are collectively for. This is especially useful for grouping radio buttons, related checkboxes, or sections of a longer form like "Billing Address" versus "Shipping Address".

Syntax

<fieldset>
  <legend>Group Title</legend>
  <!-- form controls -->
</fieldset>

Examples

Grouped Radio Buttons

Using fieldset to group a set of related radio options.

<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>

Multi-Section Form

Breaking a longer form into clearly labeled groups.

<form>
  <fieldset>
    <legend>Personal Details</legend>
    <input type="text" placeholder="Full Name">
    <input type="email" placeholder="Email">
  </fieldset>
  <fieldset>
    <legend>Address</legend>
    <input type="text" placeholder="Street">
    <input type="text" placeholder="City">
  </fieldset>
</form>

Disabling a Whole Group

The disabled attribute on fieldset disables every control inside it at once.

<fieldset disabled>
  <legend>Payment Details (complete Step 1 first)</legend>
  <input type="text" placeholder="Card Number">
</fieldset>

Attributes

  • disabled (boolean): Disables every form control nested inside the fieldset
  • name (string): Associates the fieldset with the form for scripting purposes

Best practices

  • Always include a <legend> as the first child of a <fieldset> — it is what gives the group its accessible name
  • Use fieldset to group related radio buttons or checkboxes so screen readers announce the group context
  • Use the disabled attribute on a fieldset to disable a whole section at once instead of each input individually
  • Avoid using fieldset purely for visual styling — use a <div> if there is no genuine grouping of controls

At a glance

Purpose
Structure and meaning for web content
File extension
.html · .htm
Runs in
Web browsers
Usually used with
CSS and JavaScript

Specifications & further reading

Related HTML documentation