Fieldset & Legend — HTML

<fieldset> wraps related fields in a form into one group. <legend> is that group's title, and you write it as the first thing inside the fieldset. All the…

Picture a form with three radio buttons: S, M and L. A screen reader user (someone using a program that reads pages aloud) only hears "S, radio button", then "M, radio button". Are S, M and L T-shirt sizes or shoe sizes? From those words alone, the user can't tell.

What is missing is a title for that group of choices. <fieldset> groups related fields inside a form. <legend> is that group's title. Think of a paper form divided into boxes with lines around them, where each box has a title: "Personal Details", "Address". The browser draws a fieldset like one of the boxes on that paper form: a border around the fields, with the legend's text on the top line.

Group fields with a fieldset

<form>
  <fieldset>
    <legend>Personal Details</legend>
    <label for="name">Name</label>
    <input type="text" id="name" name="name">
    <label for="email">Email</label>
    <input type="email" id="email" name="email">
  </fieldset>
</form>

From the outside to the inside:

So the order is: the form, inside it the fieldset, inside the fieldset the legend first, then the fields.

The result: a box with "Personal Details" on its top line, which contains the Name and Email fields. A screen reader also says "Personal Details" when the user moves into a field inside that box. A long form can have several fieldsets, one for each group, for example Personal Details, Address and Payment.

Radio buttons need a title

<fieldset>
  <legend>T-shirt size</legend>
  <label><input type="radio" name="size" value="S"> S</label>
  <label><input type="radio" name="size" value="M"> M</label>
  <label><input type="radio" name="size" value="L"> L</label>
</fieldset>

The labels S, M and L only name each choice. They don't say the question: what size is this? Without a legend, a screen reader user only hears "S, radio button". With a legend, they hear "T-shirt size, S". So every group of radio buttons or checkboxes should be wrapped in a fieldset that has a legend.

In this example, each <label> wraps its input directly. This also connects the label to the input, so you don't need for and id.

Turn off a whole group with disabled

Write <fieldset disabled> and every field inside it is turned off at once. The fields can't be clicked, can't be filled in, and their data isn't sent. disabled also works on a single input, but this way you don't need to put it on each input one by one.

⚠️ Fieldsets that look right but aren't yet

TL;DR: <fieldset> wraps related fields in a form into one group. <legend> is that group's title, and you write it as the first thing inside the fieldset. All the group's fields go before </fieldset>. Groups of radio buttons or checkboxes need a fieldset and a legend, so a screen reader reads the question aloud too, for example "T-shirt size". <fieldset disabled> turns off every field inside it at once: the fields can't be filled in, and their data isn't sent.

⚡ One group, one title

Write:

<form>
  <fieldset>
    <legend>Personal Details</legend>
    <label for="name">Name</label>
    <input type="text" id="name" name="name">
  </fieldset>
</form>

Meaning: A <fieldset> groups related fields, and the browser draws it as a box with a border. The <legend> is its title, written as the first thing inside the fieldset, before any field.

⚡ Radio buttons need a title

Write:

<fieldset>
  <legend>T-shirt size</legend>
  <label><input type="radio" name="size" value="S"> S</label>
  <label><input type="radio" name="size" value="M"> M</label>
</fieldset>
Guess first: without a legend, what does a screen reader say?

Only "S, radio button". T-shirt size or shoe size? It isn't clear. With a legend, the user hears "T-shirt size, S".

Meaning: Every group of radio buttons or checkboxes should be wrapped in a fieldset that has a legend, so a screen reader reads the question aloud too.

⚡ Turn off a whole group

Meaning: Write <fieldset disabled> and every field inside it is turned off at once: the fields can't be filled in, and their data isn't sent.

What you will learn