Back to HTML Notes
Topic #170

legend

Learn how to use the <legend> element to provide accessible captions for grouped form controls, ensuring screen readers announce the purpose of each section.

What it is

The <legend> element defines a caption for its parent <fieldset>. It must be the first child of the fieldset. Semantically, it groups related form controls (like radio buttons or checkboxes) and provides a label for that group. For assistive technologies, this context is crucial: without a legend, a user might hear "Radio button 1" but not know what question they are answering.

Why it matters

  • Screen Reader Context: When a user navigates into a fieldset, most screen readers announce the legend text before reading individual inputs, providing immediate orientation.
  • Visual Clarity: Browsers typically render legends with a border around the fieldset, visually grouping related fields together.
  • Semantic Structure: It creates a logical hierarchy in the DOM, helping users understand complex forms with multiple sections.
  • Compliance: Using legends correctly helps meet WCAG guidelines for identifying input purposes and grouping controls.

Syntax or steps

  1. Create a <fieldset> element to wrap your related inputs.
  2. Add a <legend> as the very first child inside the fieldset.
  3. Place your form controls (inputs, labels) after the legend within the same fieldset.

Example

<form>
  <fieldset>
    <legend>Preferred Contact Method</legend>
    
    <input type="radio" id="email" name="contact" value="email">
    <label for="email">Email</label><br>
    
    <input type="radio" id="phone" name="contact" value="phone">
    <label for="phone">Phone</label>
  </fieldset>
</form>

Part-by-part explanation:

  • <fieldset>: Groups the two radio buttons logically.
  • <legend>Preferred Contact Method</legend>: Provides the title for the group. A screen reader will say "Preferred Contact Method, Radio Group" when entering this area.
  • <input> and <label>: Standard form elements linked via id and for attributes.

Common mistakes

  • Placing the legend later: The <legend> must be the first child of the <fieldset>. If placed elsewhere, browsers may ignore it or display it incorrectly.
  • Using divs instead: Developers often use <div> with CSS borders for visual grouping. This lacks semantic meaning and does not trigger screen reader announcements like a fieldset/legend pair does.
  • Empty legends: Avoid empty <legend> tags just for styling. If you need visual grouping without a caption, consider using CSS on the fieldset directly, but ensure accessibility isn't compromised.
  • Nesting legends: Do not place other block-level elements (like headings) inside the legend. Keep the content simple and textual.

When to use it

Use <legend> whenever you have a set of related inputs that answer a single question or belong to one category. Compare it with standard labels below:

ElementScopeBest For
<label>Single InputNaming one specific field (e.g., "Name").
<legend>Group of InputsNaming a collection of related fields (e.g., "Gender Options").

Practice

Guided Exercise: Create a form with a fieldset for "Shipping Address." Include inputs for Street, City, and Zip Code. Add a legend titled "Address Details."

Challenge: Modify the previous exercise by adding a second fieldset for "Billing Address" with its own legend. Ensure both legends are distinct and properly nested.

Hint: Check your browser's developer tools to see if the legend renders with the default border style, confirming correct placement.

Quick check

Question: Where must the <legend> element be placed within a <fieldset>?

Answer: It must be the first child element of the <fieldset>.

Summary

The <legend> element is essential for accessible form design, providing context for grouped inputs to assistive technologies. Always pair it with a <fieldset> and ensure it is the first child to maintain semantic integrity and visual consistency.

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

legend – FAQs

Quick answers about learning legend in HTML.

This free note from Coding Now Tech Institute explains legend 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 legend, is 100% free with no signup required.
With focused practice, most students grasp legend 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