Definition and Usage
The <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
<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 | Any element that accepts flow content. |
|---|---|
| Permitted content | flow content. <legend> element may optionally appear as the first child. |
Implicit ARIA role |
group |
Permitted ARIA roles |
radiogroup,
presentation,
none, (group is also allowed, but NOT RECOMMENDED.) |
Using Related Attributes
Let's learn about the attributes related to the <fieldset> tag.
disabled Attribute
The disabled attribute is a control-disabling attribute. When applied, the control cannot be edited or receive focus, and its value is not submitted with the form. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
The disabled attribute can also be used with the <fieldset> tag. When set on a <fieldset>, it disables all descendant controls within it. The following example demonstrates how applying the disabled attribute disables all controls inside the <fieldset>.
<form>
<fieldset disabled>
<legend>Required Information</legend>
<div>
<label for="user-name">Name</label>
<input type="text" id="user-name" value="John Doe">
</div>
<div>
<label for="user-pw">Password</label>
<input type="password" id="user-pw" value="test">
</div>
</fieldset>
</form>
<input type="text"> and <input type="password"> within the <fieldset> do not have the disabled attribute, but you can see that each control is disabled due to <fieldset disabled>.
name Attribute
The name attribute specifies the name of the set grouped together by the element.
It specifies a name for referencing the element through the form.elements API in JavaScript.
form Attribute
Even when the form attribute is set on a <fieldset>, form-related elements nested inside it are not automatically associated with that <form>. However, if a name attribute is set on the <fieldset>, the <fieldset> itself can be referenced by that name through the form.elements API of the associated form.
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 |
disabled
|
20 | 12 | 4 | 6 |
name
|
19 | 12 | 4 | 6 |
form
|
1 | 12 | 1 | 3 |
Specifications
| Specification | |
|---|---|
<fieldset>
|
HTML Standard #the-fieldset-element |