Definition and Usage
The <legend> tag is used to represent a caption (a brief title or description) for the elements grouped together as a set by their parent <fieldset> tag.
While optional, if used, it must be the first child element of the <fieldset> tag.
To understand the <legend> tag, you should be familiar with the <fieldset> tag.
Additional Explanation
<fieldset> tag represents a set of web form control elements (or other content) grouped together.
- Web Form Control Elements
- The term commonly used is 'form controls'.
These refer to elements that provide controls for users to interact with web pages and are used together with form elements.<input>,<textarea>,<select>,<button>, and others are elements that allow users to enter, select, and submit data.
The <fieldset> tag can optionally include a caption.
This caption is represented by the <legend> tag, which is the first child element of the <fieldset> tag, and the other elements included within it form the group.
The <legend> tag serves as the caption for the group (a brief title or description) and is an important element for informing screen reader users about the contents of the group. Within the <fieldset> tag, the <legend> tag can be used optionally when needed and is not required. However, the <legend> tag plays an important role when considering web accessibility within the <fieldset> tag.
Basic Example
The following example demonstrates how to use the <legend> tag.
<form>
<fieldset>
<legend>Select the web development languages you are interested in.</legend>
<div>
<input type="radio" id="html" name="interest" value="html">
<label for="html">HTML</label>
</div>
<div>
<input type="radio" id="css" name="interest" value="css">
<label for="css">CSS</label>
</div>
<div>
<input type="radio" id="js" name="interest" value="js">
<label for="js">JavaScript</label>
</div>
<button type="submit">Submit</button>
</fieldset>
</form>
Code Explanation
The type="radio" attribute of the <input> tag represents a radio button.
Technical Summary
| Permitted parents | <fieldset> element. Note that <legend> must be the first child. |
|---|---|
| Permitted content | Phrasing content, optionally intermixed with heading (<h1>~<h6>) content. |
Implicit ARIA role |
No corresponding role |
Permitted ARIA roles |
No role permitted |
Using Related Attributes
The <legend> tag can only use global attributes.
CSS Styling
Adding CSS styles to <fieldset> and <legend> makes the structure of the grouped form clearer and makes it easier to visually distinguish control elements.
This article focuses on HTML, so it does not cover CSS for <fieldset> and <legend> tags in detail.
fieldset {
padding: 1em;
background-color: #f1f9eb;
border: 1px solid yellowgreen;
border-radius: 10px;
min-width: 0;
}
fieldset + fieldset {
margin-top: 1em;
}
legend {
background-color: yellowgreen;
padding: 0.2em 1em;
border-radius: 5px;
}
input {
display: block;
width: 100%;
}
<form>
<fieldset>
<legend>Legend One</legend>
<label for="input-one">Input One</label>
<input type="text" id="input-one">
</fieldset>
<fieldset>
<legend>Legend Two</legend>
<label for="input-two">Input Two</label>
<input type="text" id="input-two">
</fieldset>
<fieldset>
<legend>Legend Three</legend>
<label for="input-three">Input Three</label>
<input type="text" id="input-three">
</fieldset>
</form>
Browser Compatibility
| Tag and Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<fieldset>
|
1 | 12 | 1 | 4 |
<legend>
|
1 | 12 | 1 | 3 |
Specifications
| Specification | |
|---|---|
<legend>
|
HTML Standard #the-legend-element |