In CSS, adjacent vertical margins usually “collapse” into one another (i.e. the vertical space between the elements will be equal to the largest margin, not the sum of the margins).
However, fieldset
elements, unlike most other elements, do not allow margins on their child elements to collapse with margins on their sibling elements:
<div>Before div</div>
<div style="margin:0; border:0; padding:0; background:#ccc;">
<div style="margin:20px 0;">This div has a top and bottom margin, which has collapsed outside of the parent div.</div>
</div>
<div>After div</div>
<div>Before fieldset</div>
<fieldset style="margin:0; border:0; padding:0; background:#ccc;">
<div style="margin:20px 0;">This div has a top and bottom margin, but the parent fieldset prevents it from collapsing.</div>
</fieldset>
<div>After fieldset</div>
I think (but am not sure) that this is because the fieldset is creating a new block formatting context — the CSS spec doesn’t currently define whether fieldsets should or not, but the HTML5 spec says it “expects” them to.
Is there any way to prevent fieldsets from blocking the collapsing of margins between their children and their siblings?
The top and bottom margins of blocks are sometimes combined (collapsed) into a single margin whose size is the largest of the individual margins (or just one of them, if they are equal), a behavior known as margin collapsing. Note that the margins of floating and absolutely positioned elements never collapse.
How to Avoid Margin Collapse. First, remember that margins should preferably be used to increase the distance between sibling elements, not to create spacing between a child and a parent. If you need to increase space within the Flow layout, reach first for padding if possible.
Only one type of margin can collapse: Vertical (top and bottom). Margin collapse never applies to horizontal (left and right) margins.
Yes, the fieldset
element establishes the new block formatting context (this behavior was first implemented in the browsers, so the spec incorporated this feature as part of "expected default rendering").
Unfortunately, I don't know any way to "undo" this with CSS, except completely removing the fieldset
element's box by setting it to display:contents
, which currently only gives the desired result in Chrome with the "Experimental Web Platform features" flag turned on (Firefox, although implemented display:contents
back in 2015, hasn't updated its implementation to work for "unusual elements" like form controls according to the recent addition to the spec yet).
As you and @Blazemonger already discussed in the comments, it might be something browser-specific. You could still use a div
and not sacrifise accessibility if you use the aria role
attribute with the value group
.
<div role="group">
<!-- inputs here -->
</div>
From the official spec:
WAI-ARIA provides a grouping role that functions similarly to
fieldset
andlegend
. In this example, thediv
element hasrole=group
to indicate that the contained elements are members of a group and thearia-labelledby
attribute references theid
for text that will serve as the label for the group.
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With