Live Preview
A fieldset with legend and labeled inputs:

By the end of this tutorial, you’ll caption form groups with accessible, semantic <legend> elements.
Place <legend> as the first child of <fieldset>.
Name sections like Contact Info or Payment Method.
Distinguish group titles from individual field labels.
Use legend for mutually exclusive or related options.
Style legends for polished form section headings.
Help screen readers announce form group context.
<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.
<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.
Place the legend as the first element inside a fieldset:
<fieldset>
<legend>Your Caption Here</legend>
<!-- form controls go here -->
</fieldset><legend> is only valid as a child of <fieldset>.| Use Case | Code Snippet | Notes |
|---|---|---|
| Basic group | <legend>Contact Info</legend> | Inside <fieldset> |
| Radio group | <legend>Payment</legend> | Names the option set |
| Multiple groups | One legend per fieldset | Shipping, 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> |
|---|---|---|
| Scope | Names an entire fieldset group | Names one form control |
| Parent | Must be inside fieldset | Standalone or in a form |
| Typical use | “Contact Information” | “Email:” on one input |
| Radio/checkbox sets | Group title (e.g. “Gender”) | Each option label |
| Click behavior | No direct focus link | Focuses associated control |
<legend> vs <h2>Do not replace legend with a heading inside fieldset:
| Element | Best for | In fieldset? |
|---|---|---|
<legend> | Form group accessible name | Yes — first child |
<h2> | Page or section headings | Not for form group a11y |
| Together | Heading for page; legend for form group | Use legend inside fieldset |
The <legend> element has no unique attributes. It supports global attributes only:
class GlobalCSS hook for typography, color, and spacing on the group caption.
class="section-legend"id GlobalUnique identifier for scripting or referencing from CSS.
id="contact-legend"lang GlobalLanguage of the legend text in multilingual forms.
lang="en"title GlobalAdvisory 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.
Contact groups, radio sets, multiple fieldsets, and styled legends with copy-ready code and live previews.
A fieldset with legend and labeled inputs:
The primary use of <legend> — caption a related set of form fields.
<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>Use <legend> whenever you group related controls in a <fieldset> — contact sections, address blocks, payment methods, and radio or checkbox option sets.
Name a set of mutually exclusive options with a legend; label each radio individually.
<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>Use separate legends for distinct sections in one form.
<form>
<fieldset>
<legend>Shipping Address</legend>
<!-- shipping fields -->
</fieldset>
<fieldset>
<legend>Billing Address</legend>
<!-- billing fields -->
</fieldset>
</form>Style the legend and fieldset together with CSS classes.
<fieldset class="form-section">
<legend class="form-section__title">Account Details</legend>
<!-- fields -->
</fieldset>Browsers render legend text at the top of the fieldset border. Customize it for your design system:
font-weight Bold group titlepadding Space around legend textfieldset border Group container stylingcolor Muted disabled groups/* 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
Legends are critical for accessible form groups:
Wrap related inputs in fieldset and add legend first.
Legend text appears at the top edge of the fieldset box.
Screen readers speak the legend when focus enters a control in the group.
Users know which section they are filling out and why fields are grouped.
The <legend> element is fully supported in all browsers when used inside <fieldset>.
From legacy Internet Explorer to the latest mobile browsers — legend inside fieldset is fully supported.
Bottom line: Ship accessible form groups with confidence. The <legend> element is safe to use in every production environment today.
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.
<legend>Bookmark these before you ship — they’ll keep your form groups semantic and accessible.
<legend> titles a <fieldset> group.
Legend must be the first element in fieldset.
SyntaxLegend names groups; label names individual fields.
ComparisonEssential for accessible radio/checkbox groups.
FormsAnnounced when focus enters the group.
AccessibilityWorks in every browser, including legacy IE.
Compatibility<fieldset>, naming a group of related form controls for users and assistive technology.<fieldset>, as the first child element.legend names an entire group. label names one individual control.legend for the group accessible name. Headings do not replace legend for form accessibility.Try contact groups, radio sets, and styled legends in the interactive editor.
6 people found this page helpful