Margin Collapse: Edge Cases
You'll learn: When vertical margin collapse doesn't happen, even when elements are vertically adjacent.
In the previous lesson, you learned that vertical margins normally collapse between block elements. But CSS has several situations where margin collapse is turned off entirely.
When Margins Don't Collapse
Think of margin collapse like magnets that snap together — but certain CSS properties act like insulators that prevent the magnets from touching.
Floated Elements
When you apply float: left or float: right to an element, its margins never collapse with any other element. The float removes it from the normal document flow, so its margins stay fully intact.
.box {
float: left;
margin-bottom: 20px; /* This won't collapse */
}
Positioned Elements
Elements with position: absolute or position: fixed also don't participate in margin collapse. They're positioned independently of the normal flow, so their margins remain separate.
.positioned {
position: absolute;
margin-bottom: 20px; /* Stays 20px, won't collapse */
}
Flex and Grid Items
When an element becomes a flex item (inside a display: flex container) or a grid item (inside a display: grid container), margin collapse doesn't occur between siblings or with the parent container.
.flex-container {
display: flex;
}
.flex-item {
margin-bottom: 20px; /* Won't collapse with siblings */
}
Why This Matters
These layout methods create their own spacing rules. Knowing when margins won't collapse helps you predict exactly how much space will appear in your layouts.
Key Takeaway: Margin collapse only happens with normal block-level elements in the regular document flow — floats, positioned elements, and flex/grid items keep their full margins.