Q6: What is Margin Collapse and when does it occur?
Margin collapse happens when top and bottom margins of adjacent block elements combine into a single merged margin whose size equals the largest individual margin. It occurs between adjacent vertical siblings, parent and first/last child, or empty blocks.
Vertical Siblings Margin Collapse
.box1 {
margin-bottom: 30px;
}
.box2 {
margin-top: 20px;
}
/* Rendered clearance between box1 and box2 is 30px (not 50px) */Vertical sibling margins collapse, adopting the larger 30px value instead of summing them.
Key Takeaways
Only vertical (top/bottom) margins collapse; horizontal margins never collapse.
Creating a new Block Formatting Context (BFC) prevents parent-child margin collapse.
Knowledge Check
What is the final vertical margin gap between two siblings where item 1 has margin-bottom: 40px and item 2 has margin-top: 25px?