CSS Margin Collapse Issues — How to Fix (2026) | Tutorials Logic
What is CSS Margin Collapse?
CSS margin collapsing is a behavior where the top and bottom margins of adjacent block elements combine (collapse) into a single margin equal to the larger of the two. This is intentional CSS behavior, but it often causes unexpected spacing issues.
Common Symptoms:
Unexpected spacing between elements • Margin "leaking" out of parent • First/last child margin affecting parent position
Unexpected spacing between elements • Margin "leaking" out of parent • First/last child margin affecting parent position
Common Causes
Quick Fix (TL;DR)
/* ❌ Problem — child margin collapses with parent */
.parent { /* No padding or border */ }
.child { margin-top: 20px; } /* Pushes parent down, not child! */
/* ✅ Solution 1: Add padding to parent */
.parent { padding-top: 1px; }
/* ✅ Solution 2: Add border to parent */
.parent { border-top: 1px solid transparent; }
/* ✅ Solution 3: Use overflow */
.parent { overflow: hidden; }
/* ✅ Solution 4: Use flexbox (no collapse) */
.parent { display: flex; flex-direction: column; }
Common Scenarios & Solutions
Scenario 1: Adjacent Sibling Collapse
/* Expected: 30px gap (20 + 10) */
/* Actual: 20px gap (larger wins) */
.box-1 { margin-bottom: 20px; }
.box-2 { margin-top: 10px; } /* Collapses with box-1's margin */
/* ✅ Use margin on only one side */
.box-1 { margin-bottom: 20px; }
/* Remove margin-top from box-2 */
/* ✅ Or use gap in a flex/grid container */
.container {
display: flex;
flex-direction: column;
gap: 20px; /* No collapse! */
}
Scenario 2: Parent-Child Margin Collapse
/* ❌ Child margin pushes parent down */
.parent { background: lightblue; }
.child { margin-top: 30px; }
/* The 30px appears ABOVE the parent, not inside it! */
/* ✅ Add padding to parent instead */
.parent {
background: lightblue;
padding-top: 30px; /* Use padding, not child margin */
}
.child { /* No margin-top needed */ }
/* ✅ Or add border to parent */
.parent { border-top: 1px solid transparent; }
Scenario 3: Prevent Collapse with Flexbox
/* ✅ Flexbox prevents margin collapse entirely */
.container {
display: flex;
flex-direction: column;
gap: 16px; /* Use gap instead of margins */
}
/* Margins between flex items do NOT collapse */
Scenario 4: When Margins Don't Collapse
/* Margins DO NOT collapse when: */
/* 1. Elements are in flex or grid container */
.flex-parent { display: flex; }
/* 2. Parent has padding or border */
.parent { padding: 1px; }
/* 3. Parent has overflow other than visible */
.parent { overflow: hidden; }
/* 4. Elements are floated or absolutely positioned */
.floated { float: left; }
/* 5. Horizontal margins (left/right) never collapse */
Best Practices to Avoid This Error
Related Issues
Key Takeaways
- Margin collapse combines adjacent vertical margins into the larger of the two
- Parent-child margins collapse when no border, padding, or overflow separates them
- Margins never collapse horizontally (left/right) — only vertically (top/bottom)
- Flexbox and Grid containers prevent margin collapse between their children
- Use gap in flex/grid containers instead of margins for predictable spacing
- Add padding-top: 1px or border-top to a parent to prevent child margin collapse
Frequently Asked Questions
Level Up Your Css Skills
Master Css with these hand-picked resources
10,000+ learners
Free forever
Updated 2026
Related CSS Topics