Skip to content
Tutorials Center
My learning

Topics

Lesson 65 of 122 ยท Forms

Labels, Fieldsets and Legends

Give every form control a proper name with label, group related fields with fieldset and legend, and learn how browsers work out a control's accessible name.

A text box on its own is a mystery. Is it for your name? Your email? Your favorite fruit? Sighted people can usually guess from nearby text, but a screen reader only announces what the HTML connects to the control. The <label> element makes that connection, and <fieldset> with <legend> does the same for whole groups of controls.

Labels are not a nice-to-have. A form without them is hard or impossible to use for many people, and it is also more fiddly for everyone else. In this lesson you will learn both ways to write a label, how to group fields, and what an accessible name is.

Why labels matter

A properly connected label does three jobs at once:

  1. It names the control for assistive technology. When a screen reader lands on the field, it reads the label: "Email address, edit text."
  2. It makes the label clickable. Clicking or tapping the label focuses the text box, or toggles the checkbox. That turns a tiny checkbox into a large, easy target.
  3. It helps voice control. People who operate their computer by voice can say "click Email address" to reach the field.

None of this happens if the text merely sits next to the control. The connection has to be in the markup.

Two ways to connect a label

Explicit labels with for

Give the control an id, and give the label a for attribute with the same value:

HTML
<label for="email">Email address</label>
<input type="email" id="email" name="email">

The label and the control can sit anywhere on the page, which gives you full freedom over layout. The id must be unique on the page, as explained in IDs, and for must match it exactly, including upper and lower case.

Implicit labels by wrapping

Put the control inside the label, and no id is needed:

HTML
<label>
  Email address
  <input type="email" name="email">
</label>

Both forms are valid HTML. The explicit version is the most widely recommended, because a few older assistive technologies handled wrapped labels less reliably, and it keeps working when you move things around. Many developers combine the two: they wrap the control and add for. That is allowed too, as long as for points at the control inside.

Clickable labels

Click the words "Full name": the cursor jumps into the box. Click "Remember me on this device": the checkbox toggles. Then click the text after the third checkbox: nothing happens, because that text is not a label. (We gave that checkbox an aria-label only so the example itself stays accessible.)

Rules for good labels

  • One control per label. A label points at exactly one control. A control may have more than one label, but that is rarely useful.
  • Keep labels visible. People should be able to read the label while they type, and after they have finished.
  • Write short, specific text. "Email address" beats "Please enter your email address in the box below."
  • Put labels before text boxes and after checkboxes and radio buttons. That is where people expect them.
  • Do not put other interactive things inside a label, such as links or buttons. Clicking them becomes ambiguous. Put a "Forgot password?" link outside the label.

Placeholders are not labels

The placeholder attribute shows hint text inside an empty box. It is tempting to use it instead of a label, but that causes real problems:

  • The hint disappears as soon as someone starts typing, so they can forget what the field was for.
  • Placeholder text is usually low-contrast gray, which is hard to read.
  • Browsers and screen readers do not treat a placeholder as a reliable label.

Use a real <label>, and keep placeholder for an optional example of the expected format, such as placeholder="e.g. 221B Baker Street". Input Attributes covers it in detail.

Help text with aria-describedby

Often a field needs more than a name: a hint about the format, or a rule about length. Keep the label short and put the extra text in a separate element. Then link it with aria-describedby, which takes the id of the help text:

HTML
<label for="username">Username</label>
<input type="text" id="username" name="username" aria-describedby="username-help">
<p id="username-help">3 to 20 letters, numbers or underscores.</p>

A screen reader now announces the name first and the description after a short pause: "Username, edit text. 3 to 20 letters, numbers or underscores." You will meet more ARIA attributes in ARIA Roles, States and Properties.

Grouping with <fieldset> and <legend>

Some questions are made of several controls: a set of radio buttons, the three parts of an address, or a shipping section of a checkout. <fieldset> groups them, and <legend> gives the group a caption:

HTML
<fieldset>
  <legend>Preferred contact method</legend>

  <input type="radio" id="by-email" name="contact" value="email">
  <label for="by-email">Email</label>

  <input type="radio" id="by-phone" name="contact" value="phone">
  <label for="by-phone">Phone</label>
</fieldset>

The legend matters most for groups of radio buttons and checkboxes. Without it, a screen reader user hears "Email, radio button, 1 of 2" with no idea what the question was. With it, they hear "Preferred contact method, group", followed by the options.

A few rules:

  • The <legend> must be the first child of the <fieldset>.
  • Keep legends short. Screen readers may repeat the legend for every control in the group.
  • Fieldsets can be nested, but do not overdo it. One level is usually enough.
  • By default, browsers draw a border around the fieldset with the legend sitting in its top edge. You can restyle or remove that with CSS.

Disabling a whole group

A <fieldset> with the disabled attribute disables every control inside it at once. Disabled controls cannot be focused or edited, and they are not submitted. That makes it easy to switch off a section of a form until it is needed:

Fieldsets, legends and disabled groups

Press Send with the checkbox ticked: the shipping fields are missing from the output. Untick it, fill in the shipping fields, and send again.

Accessible names, briefly

Every control has an accessible name: the text assistive technology uses to identify it. Browsers work it out from several sources, in roughly this order of priority:

  1. aria-labelledby, which points to the id of other elements whose text becomes the name.
  2. aria-label, a name written directly as an attribute.
  3. A connected <label> (or, for buttons, their own text content).
  4. As a last resort, the title or placeholder attribute.

The first two override everything else, which is exactly why you should use them sparingly. Use a visible <label> whenever you can. Reach for aria-label only when a visible label truly makes no sense, such as a search box next to a button that already says "Search". The rule of thumb from Accessibility Basics applies: native HTML first, ARIA second.

Test it yourself. In your browser's DevTools, select a form field and open the Accessibility pane. It shows the computed accessible name and where it came from. An empty name means a missing label.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which markup correctly connects a label to <input id="city" name="city">?

    The label's for attribute must match the control's id. The name is only for submission.
  2. 2. Why is a placeholder a poor replacement for a label?

    Placeholder text vanishes as soon as someone types and is often hard to read. Keep a visible label and use the placeholder only for an optional example.
  3. 3. Where must the <legend> go?

    The <legend> is the fieldset's caption and must be its first child.
  4. 4. What happens to the controls inside <fieldset disabled> when the form is submitted?

    Disabling a fieldset disables every control inside it, and disabled controls are left out of the form data.

Summary

  • A <label> names a control for screen readers and voice control, and makes the label text clickable.
  • Connect labels explicitly with for and a matching id, or implicitly by wrapping the control.
  • Keep labels visible and short; placeholders are not labels.
  • Link extra help text with aria-describedby.
  • Group related controls with <fieldset> and caption the group with a <legend> as its first child; disabled on a fieldset disables everything inside.
  • A visible label is the best source of a control's accessible name; use aria-label and aria-labelledby only when you must.

Next up: Checkboxes and Radio Buttons, where labels and fieldsets really pay off.