Margin collapse happens when vertical margins between certain block elements combine instead of adding together. This is normal CSS behavior, but it surprises beginners because the visible gap can be smaller than expected.
Collapse usually appears between adjacent paragraphs/headings or between a parent and its first or last child. The fix is not always more margin; often the right fix is padding, border, flex/grid gap, or creating a new formatting context.
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.
/* ❌ 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; }
/* 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! */
}
/* ❌ 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; }
/* ✅ 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 */
/* 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 */
.lesson-box:empty::before {
content: "CSS Margin Collapse Issues: add visible content";
}
Margin collapse is when the top and bottom margins of adjacent block elements combine into a single margin equal to the larger value. It's intentional CSS behavior designed for typography spacing.
The child's top margin is collapsing with the parent's top margin. Add padding-top: 1px, border-top: 1px solid transparent, or overflow: hidden to the parent to prevent this.
No. Margin collapse only happens with vertical (top/bottom) margins. Left and right margins never collapse.
Explore 500+ free tutorials across 20+ languages and frameworks.