How to Wrap Form Inputs in a Box with fieldset and legend in CSS
Wrap a group of inputs in a <fieldset> and put a <legend> as its first child, and you get a box with the <legend> text sitting on the border line. You get this box without writing any CSS, and you can change border and border-radius to make the corners rounded. Adding disabled to the <fieldset> disables all the inputs inside it at once.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened HTML and CSS pages with Playwright and recorded the values from getComputedStyle() and getBoundingClientRect(), along with the results of interactions. I did not test Firefox, Safari, mobile browsers, or reading with a screen reader.
- 1. In the HTML, put the legend as the first child of the fieldset
- 2. The box when you write no CSS
- 3. Rounded corners and vertically stacked items
- 4. Use disabled to disable all the inputs inside at once
- 5. If the box overflows on narrow screens, add min-inline-size: 0
- 6. When you do not want the legend on the border line
In the HTML, put the legend as the first child of the fieldset
Here is an example that groups the radio buttons for choosing a shipping method into one box.
<fieldset id="ship">
<legend>配送方法</legend>
<label><input type="radio" name="ship" value="normal" checked> 通常配送</label>
<label><input type="radio" name="ship" value="express"> お急ぎ便</label>
<label><input type="radio" name="ship" value="store"> 店舗で受け取る</label>
</fieldset>
The HTML specification defines the content of a <fieldset> as: at most one <legend> (optional), followed by flow content (other elements or text). The heading for the group is the <legend> that is the first child of the <fieldset>.
The MDN page for <fieldset> says the implicit ARIA role of <fieldset> is group. When I dumped the Playwright accessibility tree in the test environment, the text of the <legend> was used as the name of the group.
- group "配送方法":
- text: 配送方法
- radio "通常配送" [checked]
- text: 通常配送
- radio "お急ぎ便"
- text: お急ぎ便
- radio "店舗で受け取る"
- text: 店舗で受け取る
Not only the visual box but also the grouping, "these three radio buttons are the choices for the shipping method," shows up in the accessibility tree.
The box when you write no CSS
I opened a page with no CSS on <fieldset> or <legend> in the Chromium test environment and read the computed values. The font size was 16px.
| Property | Value on <fieldset> |
|---|---|
border |
2px groove |
padding |
top 5.6px, left and right 12px, bottom 10px |
margin |
left and right 2px |
min-inline-size |
min-content |
These are the same values as the default styles the browser applies, described in the display section of the HTML specification (padding-block: 0.35em 0.625em and padding-inline: 0.75em, calculated at a font size of 16px). The <legend> has padding of 2px on the left and right, which moves the border line away from the text by that amount.
The specification says that no border is drawn over the part that overlaps the <legend>. In the test environment, the <legend> sat at the top-left of the box, and the border was cut only where the <legend> is, so the text looked like it was sitting on the border line (the top half of the image in the next section).
Rounded corners and vertically stacked items
I changed the default box into a light blue box with rounded corners. The CSS is below. I left out CSS that only affects appearance, such as font size.
fieldset {
display: flex;
flex-direction: column;
gap: 8px;
margin: 0 0 16px;
padding: 12px 16px 16px;
border: 2px solid #00c8e8;
border-radius: 8px;
}
legend {
padding: 0 8px;
color: #067a98;
font-weight: bold;
}
fieldset:disabled {
border-color: #9aa5ab;
}
fieldset:disabled > label {
opacity: 0.5;
}
Changing border does not move the <legend>, which still sits on the border line. Even with border-radius rounding the corners, the border is still cut where the <legend> is. Widening the left and right padding of the <legend> to 8px leaves a gap between the text and the border line.

You can also use display: flex on <fieldset>. The MDN page for <fieldset> says that when <fieldset> is display: flex, the anonymous box that holds the content becomes a flex formatting context. The <legend> is not placed inside that anonymous box.
In the test environment, I measured the positions. The space between the labels of the radio buttons was 8px, matching the gap. The distance from the bottom edge of the <legend> to the first label was 12px, the same as the padding-top value. The gap of 8px was not added there. The <legend> is not laid out as a flex item and stays on the border line.
Use disabled to disable all the inputs inside at once
Adding disabled to a <fieldset> disables all the inputs inside it. The HTML specification says that only the inputs inside the first <legend> are not disabled.
With this, you can build a form where the wrapping fields are only editable after checking the gift-wrapping checkbox. Put the checkbox inside the <legend>.
<fieldset id="gift" disabled>
<legend>
<label><input type="checkbox" id="use-gift"> ギフト包装にする</label>
</legend>
<label>のし紙の名前 <input name="giftName" value="山田"></label>
<label>メッセージ <textarea name="giftMessage">いつもありがとう</textarea></label>
</fieldset>
When the checkbox changes, toggle disabled on the <fieldset>.
const gift = document.getElementById('gift');
document.getElementById('use-gift').addEventListener('change', (e) => {
gift.disabled = !e.target.checked;
});
In the test environment, I recorded the state right after the page opened and after checking the box.
| What was checked | Right after opening | After checking the box |
|---|---|---|
The checkbox inside <legend> |
Enabled | Enabled |
| The noshi name and message fields | Disabled (matches :disabled) |
Enabled |
Names included in FormData |
ship only |
ship, giftName, giftMessage |
While disabled, clicking the noshi name field with the mouse did not give it focus. Pressing Tab from the checkbox skipped the two disabled fields and moved to the place-order button. After checking the box, I could type "Sato" into the noshi name field.
The input elements themselves have no disabled attribute. The disabled on <fieldset> alone makes the fields inside match :disabled. You can switch the appearance with fieldset:disabled. In the CSS above, the border turns gray while disabled, and fieldset:disabled > label makes the labels semi-transparent (opacity: 0.5). The labels inside <legend> are not direct children of <fieldset>, so the checkbox text does not become semi-transparent. When I read the computed values in the test environment, that was indeed the case.
If the box overflows on narrow screens, add min-inline-size: 0
By default, <fieldset> has min-inline-size: min-content. If it contains a long line that does not wrap, the <fieldset> cannot become narrower than that line.
In the test environment, I placed a <fieldset> inside a 320px-wide box and put in a <pre> with overflow-x: auto and one long line. The <fieldset> became 663.77px wide and overflowed the box to the right. The <pre> did not become horizontally scrollable. When I wrapped the same <pre> in a <div>, the <div> fit within 320px and the <pre> became horizontally scrollable.
When I added min-inline-size: 0 to the <fieldset>, its width fit within 316px (the 320px box minus the 2px left and right margin), and the <pre> became horizontally scrollable.
fieldset {
min-inline-size: 0;
}

As stated on the MDN page for min-inline-size, min-inline-size maps to min-width or min-height depending on the writing mode. On a horizontal page, it maps to min-width.
When you do not want the legend on the border line
If you want the <legend> inside the box instead of on the border line, give the <legend> a float. The HTML specification says that only the first <legend> whose float is none and whose position is neither absolute nor fixed is placed on the border line.
legend {
float: left;
width: 100%;
padding: 0;
margin-bottom: 8px;
}
legend + * {
clear: both;
}
This sets float: left and width: 100% on the <legend> and adds clear: both to the next element, so the content starts below the <legend>.
In the test environment, when I applied this to a <fieldset> with a 2px border and 12px of top padding, the top edge of the <legend> was 14px below the top edge of the <fieldset>. That is inside the border and the padding. In the accessibility tree, the group name stayed the shipping-method label even after float was applied.
If you do not need the box and only want the grouping meaning, remove the default styles.
fieldset {
border: 0;
padding: 0;
margin: 0;
min-inline-size: 0;
}
legend {
padding: 0;
}
In the test environment, the <legend> sat at the top edge of the <fieldset>, and the first label followed directly beneath it with no gap. Visually it is just a heading and inputs, but the grouping of <fieldset> and <legend> remains.





