<fieldset> and <legend> : Form Grouping Elements
The <fieldset> and <legend> elements are used together to create visually grouped sections within forms or form-like layouts. The <fieldset> element creates a bordered container, while the <legend> element provides a caption or title that appears embedded in the fieldset’s border. These elements are essential for organizing complex forms into logical sections.
On this page
Usage
The <fieldset> element creates a container that:
- Groups related form elements together
- Displays a border around its contents by default
- Can contain a
<legend>as its first child for labeling - Supports full CSS styling and customization
- Creates semantic structure for form sections
- Works with any HTML content, not just form elements
The <legend> element provides a caption that:
- Appears at the top of the fieldset border
- Acts as a title or label for the grouped content
- Is typically the first child of a
<fieldset> - Can be styled independently
- Has auto-width by default (doesn’t span full width)
<fieldset>
<legend>Personal Information</legend>
<label>Name:</label>
<input type="text" value="John Doe" />
</fieldset>
Fieldset Attributes
Standard HTML Attributes
| Attribute | Type | Description |
|---|---|---|
id |
string | Unique identifier for the element. Used for styling and references. |
class |
string | CSS class name(s) for styling. Multiple classes separated by spaces. |
style |
string | Inline CSS styles applied directly to the element. |
title |
string | Sets the outline/bookmark title for the fieldset. |
hidden |
string | Controls visibility. Set to “hidden” to hide the element, or omit/empty to show. |
CSS Style Support for Fieldset
Sizing:
width,min-width,max-widthheight,min-height,max-height- Default: Auto-sized to content
Positioning:
display:block(default),inline-block,noneposition:static,relative,absolutefloat:left,right,none
Spacing:
margin(all variants) - Default:2pt left/right,1em toppadding(all variants) - Default:0.35em top,0.625em bottom,0.75em left/right
Visual Styling:
border,border-width,border-color,border-style- Default:2pt solidborder-radius- rounded cornersbackground-color,background-imagecolor- text color (inherited by contents)
Typography (inherited by contents):
font-family,font-size,font-weight,font-styleline-height,text-align
Legend Attributes
Standard HTML Attributes
| Attribute | Type | Description |
|---|---|---|
id |
string | Unique identifier for the element. |
class |
string | CSS class name(s) for styling. |
style |
string | Inline CSS styles applied directly to the element. |
title |
string | Sets the outline/bookmark title for the legend. |
hidden |
string | Controls visibility. Set to “hidden” to hide the legend. |
CSS Style Support for Legend
Sizing:
width- Default: Auto-width (not full width)padding(all variants)
Positioning:
display:block,inline-blocktext-align:left,center,right
Visual Styling:
color,background-colorborder,paddingfont-family,font-size,font-weight,font-style
Notes
Default Styling
Fieldset defaults:
- Margin:
2ptleft and right,1emtop - Padding:
0.35emtop,0.625embottom,0.75emleft/right - Border:
2pt solid(default color) - Display:
block
Legend defaults:
- Width: Auto (not full-width)
- Position: Embedded in top border
- Display:
block(but auto-sized)
Proper Structure
The <legend> should be the first child of the <fieldset>:
<!-- Correct structure -->
<fieldset>
<legend>Section Title</legend>
<!-- Other content -->
</fieldset>
<!-- Incorrect - legend not first -->
<fieldset>
<div>Some content</div>
<legend>Section Title</legend> <!-- Will still work but not semantically correct -->
</fieldset>
Legend Positioning
The <legend> appears embedded in the top border of the fieldset. Its position can be influenced by:
text-alignon the fieldset (moves legend left/center/right)paddingon the legend (adds space around the text)marginon the legend (adjusts position)
Nesting Fieldsets
Fieldsets can be nested to create hierarchical form sections:
<fieldset>
<legend>Shipping Information</legend>
<fieldset>
<legend>Address</legend>
<!-- Address fields -->
</fieldset>
<fieldset>
<legend>Delivery Options</legend>
<!-- Delivery fields -->
</fieldset>
</fieldset>
Styling Tips
- Remove border: Set
border: nonefor invisible grouping - Custom borders: Use any border style, width, and color
- Background colors: Add backgrounds to distinguish sections
- Legend styling: Style legends independently for emphasis
- Padding control: Adjust padding for spacing within the fieldset
Class Hierarchy
In the library codebase:
HTMLFieldSetextendsDiv(block container)HTMLLegendextendsDivwith auto-width enabled- Both support full HTML attributes and CSS styling
Examples
Examples will follow with forms implementation within the library
See Also
- input - Input field element
- label - Label element for form fields
- form - Form container element
- div - Generic block container
- Data Binding - Dynamic data binding and expressions
- CSS Styles - Complete CSS styling reference