← Back to Chapters

HTML label, fieldset, legend

? HTML <label>, <fieldset>, <legend>

⚡ Quick Overview

Use <label>, <fieldset>, and <legend> to make forms easier to understand and navigate—especially for screen reader and keyboard users. Labels connect text to inputs; fieldsets group related controls; legends title those groups so the purpose of each section is clear.

? Key Concepts

  • <label>: Clickable text tied to a control via for/id, or by wrapping the control inside the label.
  • <fieldset>: Semantic grouping of related inputs (great for addresses, preferences, or radio/checkbox sets).
  • <legend>: Short caption describing the group. Place it as the first child of the fieldset.
  • Accessibility: Proper labels, fieldsets, and legends help assistive technologies announce form controls with their full context.
  • Usability: Clicking labels to focus inputs makes forms faster and more keyboard-friendly.

? Syntax & Structure

Basic pattern for using these elements together:

? View Basic Pattern
<fieldset>
  <legend>Group title</legend>

  <label for="field-id">Field label</label>
  <input id="field-id" name="field-name">
</fieldset>

You can also wrap a control directly in a label (no for/id needed), which is common for radio buttons and checkboxes.

? Example Markup

? View Code Example
<!-- Group inputs using fieldset and legend -->
<fieldset>
  <legend>Personal Information</legend>

  <label for="name" class="req">Full Name</label>
  <input type="text" id="name" name="fullname" placeholder="Enter your full name" required
         aria-describedby="name-help">
  <small id="name-help" class="help">As shown on official documents.</small>

  <label for="email" class="req">Email Address</label>
  <input type="email" id="email" name="email" placeholder="name@example.com" required
         aria-describedby="email-help">
  <small id="email-help" class="help">We’ll send a confirmation email.</small>
</fieldset>

<!-- Radios/checkboxes belong in a fieldset with a legend -->
<fieldset>
  <legend>Contact Preference</legend>
  <label><input type="radio" name="contact" value="email" checked> Email</label>
  <label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>

? Live Output / Explanation

?️ Live Form Demo

This demo shows two fieldsets: one for personal information and one for contact preference. Each label is associated with its input, and help/error text is linked using aria-describedby.

Personal Information As shown on official documents. We’ll send a confirmation email.
Contact Preference

? Tips & Best Practices

  • Keep <legend> short and descriptive (a few words). Put it first inside the fieldset.
  • Associate help/error text using aria-describedby so assistive tech reads guidance with the control.
  • For radios/checkboxes, use a fieldset/legend to give the group a single question/context instead of repeating it in every label.
  • Either use for/id or wrap the control inside the label—both patterns create a clickable label.
  • <fieldset disabled> disables all form controls inside it (useful for read-only sections).
  • Ensure for/id values match and that id values are unique on the page.
  • Avoid using placeholders as labels; they disappear on typing and are not announced as accessible labels.
  • If you have longer explanations, keep the legend concise and move details into help text linked via aria-describedby.

? Try It Yourself

  • Convert an existing form section into a fieldset with a concise legend; link help and error text via aria-describedby.
  • Add client-side validation that writes messages into dedicated .error elements next to each field (similar to the live demo).
  • Create a <fieldset disabled> example for a “Billing (read-only)” section to see how all inner controls are disabled together.
  • Refactor a radio-question form so that the question is the fieldset’s legend rather than repeated inside each label.

? Summary

  • <label> connects text to inputs and improves click targets and accessibility.
  • <fieldset> groups related form controls into meaningful sections.
  • <legend> gives each fieldset a clear, concise title that screen readers announce.
  • Together, these elements make forms more structured, accessible, and user-friendly for everyone.