HTML <legend> Tag

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Form Groups

What You’ll Learn

By the end of this tutorial, you’ll caption form groups with accessible, semantic <legend> elements.

01

Core Syntax

Place <legend> as the first child of <fieldset>.

02

Group Captions

Name sections like Contact Info or Payment Method.

03

legend vs label

Distinguish group titles from individual field labels.

04

Radio & Checkbox Groups

Use legend for mutually exclusive or related options.

05

CSS Styling

Style legends for polished form section headings.

06

Accessibility

Help screen readers announce form group context.

What Is the <legend> Tag?

The legend element (<legend>) represents a caption for the content of its parent <fieldset>. It provides a human-readable title for a group of related form controls, such as contact details, shipping options, or payment methods.

💡
First child of fieldset

<legend> must appear inside <fieldset> and should be the first child. Screen readers announce the legend when users enter controls in that group.

While <label> names individual inputs, <legend> names the entire group. Use both together: a legend for the section, labels for each field inside it.

📝 Syntax

Place the legend as the first element inside a fieldset:

syntax.html
<fieldset>
  <legend>Your Caption Here</legend>
  <!-- form controls go here -->
</fieldset>

Syntax Rules

  • <legend> is only valid as a child of <fieldset>.
  • It must be the first child of the fieldset (only whitespace or comments may precede it).
  • Write concise, descriptive text — it names the whole group.
  • Pair every meaningful fieldset with a legend for accessibility.

⚡ Quick Reference

Use CaseCode SnippetNotes
Basic group<legend>Contact Info</legend>Inside <fieldset>
Radio group<legend>Payment</legend>Names the option set
Multiple groupsOne legend per fieldsetShipping, billing, etc.
Styled legend<legend class="...">CSS on global class
Parent element<fieldset>Required container
Individual fields<label>Use label, not legend

⚖️ <legend> vs <label>

Legends and labels work together but serve different roles:

Feature<legend><label>
ScopeNames an entire fieldset groupNames one form control
ParentMust be inside fieldsetStandalone or in a form
Typical use“Contact Information”“Email:” on one input
Radio/checkbox setsGroup title (e.g. “Gender”)Each option label
Click behaviorNo direct focus linkFocuses associated control

⚖️ <legend> vs <h2>

Do not replace legend with a heading inside fieldset:

ElementBest forIn fieldset?
<legend>Form group accessible nameYes — first child
<h2>Page or section headingsNot for form group a11y
TogetherHeading for page; legend for form groupUse legend inside fieldset

🧰 Attributes

The <legend> element has no unique attributes. It supports global attributes only:

class Global

CSS hook for typography, color, and spacing on the group caption.

class="section-legend"
id Global

Unique identifier for scripting or referencing from CSS.

id="contact-legend"
lang Global

Language of the legend text in multilingual forms.

lang="en"
title Global

Advisory tooltip with extra context (rarely needed if text is clear).

title="Required section"

Use class on legend or parent fieldset for consistent form section styling.

Examples Gallery

Contact groups, radio sets, multiple fieldsets, and styled legends with copy-ready code and live previews.

Live Preview

A fieldset with legend and labeled inputs:

Contact Information

Contact Information Group

The primary use of <legend> — caption a related set of form fields.

contact-legend.html
<form>
  <fieldset>
    <legend>Contact Information</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>
Try It Yourself

📚 Common Use Cases

Use <legend> whenever you group related controls in a <fieldset> — contact sections, address blocks, payment methods, and radio or checkbox option sets.

Radio Button Group

Name a set of mutually exclusive options with a legend; label each radio individually.

radio-legend.html
<fieldset>
  <legend>Payment Method</legend>
  <input type="radio" id="card" name="payment" value="card">
  <label for="card">Credit Card</label>
  <input type="radio" id="paypal" name="payment" value="paypal">
  <label for="paypal">PayPal</label>
</fieldset>
Try It Yourself

Multiple Fieldsets

Use separate legends for distinct sections in one form.

multiple-legends.html
<form>
  <fieldset>
    <legend>Shipping Address</legend>
    <!-- shipping fields -->
  </fieldset>
  <fieldset>
    <legend>Billing Address</legend>
    <!-- billing fields -->
  </fieldset>
</form>

Styled Legend

Style the legend and fieldset together with CSS classes.

styled-legend.html
<fieldset class="form-section">
  <legend class="form-section__title">Account Details</legend>
  <!-- fields -->
</fieldset>
Try It Yourself

Styling <legend> with CSS

Browsers render legend text at the top of the fieldset border. Customize it for your design system:

font-weight Bold group title
padding Space around legend text
fieldset border Group container styling
color Muted disabled groups
legend-styles.css
/* Fieldset + legend styling */
fieldset {
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 1rem 1.25rem;
  margin: 0 0 1rem;
}

legend {
  font-weight: 600;
  font-size: 0.9375rem;
  color: #0f172a;
  padding: 0 0.35rem;
}

fieldset:disabled legend {
  color: #94a3b8;
}

Live styled legend

♿ Accessibility

Legends are critical for accessible form groups:

  • Include a legend in every fieldset — it gives the group an accessible name.
  • Keep legend text descriptive — “Payment Method” is clearer than “Options”.
  • Use legend for radio/checkbox sets — screen readers announce the group before each option.
  • Do not use headings instead of legend — inside fieldset, legend is the correct semantic caption.

🧠 How <legend> Works

1

Author groups controls in fieldset

Wrap related inputs in fieldset and add legend first.

Markup
2

Browser renders group border

Legend text appears at the top edge of the fieldset box.

Rendering
3

Assistive tech announces group

Screen readers speak the legend when focus enters a control in the group.

Accessibility
=

Organized, understandable forms

Users know which section they are filling out and why fields are grouped.

Universal Browser Support

The <legend> element is fully supported in all browsers when used inside <fieldset>.

Baseline · Since HTML 4

Form group captions that work everywhere

From legacy Internet Explorer to the latest mobile browsers — legend inside fieldset is fully supported.

100% Core tag support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
<legend> tag 100% supported

Bottom line: Ship accessible form groups with confidence. The <legend> element is safe to use in every production environment today.

Conclusion

The <legend> tag is essential for structured, accessible HTML forms. It captions each <fieldset> group, helps users scan complex forms, and gives assistive technology the context needed to understand grouped controls.

Remember to place legend first inside fieldset, write clear group titles, and pair legends with individual <label> elements on each control.

💡 Best Practices

✅ Do

  • Include a legend in every meaningful fieldset
  • Write concise, descriptive group titles
  • Use legend for radio and checkbox option sets
  • Place legend as the first child of fieldset

❌ Don’t

  • Use legend outside of fieldset
  • Replace legend with a heading inside fieldset
  • Use vague captions like “Section 1”
  • Skip legend when grouping related controls

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <legend>

Bookmark these before you ship — they’ll keep your form groups semantic and accessible.

6
Core concepts
🔗 02

First Child

Legend must be the first element in fieldset.

Syntax
⚖️ 03

Not a Label

Legend names groups; label names individual fields.

Comparison
🔹 04

Radio Sets

Essential for accessible radio/checkbox groups.

Forms
05

Screen Readers

Announced when focus enters the group.

Accessibility
06

Universal Support

Works in every browser, including legacy IE.

Compatibility

❓ Frequently Asked Questions

It provides a caption for a <fieldset>, naming a group of related form controls for users and assistive technology.
Inside <fieldset>, as the first child element.
legend names an entire group. label names one individual control.
No. Inside fieldset, use legend for the group accessible name. Headings do not replace legend for form accessibility.
Strongly recommended. Without a legend, screen reader users may not know the purpose of the grouped controls.
Yes. Full support in all modern and legacy browsers when used inside fieldset.

Practice Form Group Captions

Try contact groups, radio sets, and styled legends in the interactive editor.

Try It Yourself →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful