CSS Margin, Padding, and Margin Collapse
Lesson 5 of 41 in the CSS3 learning path.
When does vertical margin collapse, and how do I space elements?
Margin and padding both create space, and they are not interchangeable. Padding sits inside the border, inherits the background, and is the right tool for space between a card edge and its text. Margin sits outside the border, is transparent, and is the tool for pushing a card away from its neighbors. Horizontal margins simply add together. Vertical margins between siblings collapse: the gap becomes the larger of the two margins, not the sum.
Collapse also happens between a parent and its first or last child when nothing separates them: no padding, no border, and no new formatting context. The child's margin then appears to leak out of the parent, and the parent's background looks too short. The sample shows a two rem margin meeting a one rem margin, a parent that lets the margin escape, and a parent whose padding contains it. A flex column with gap spaces the items without collapsing at all.
Syntax & Parameters
.slab-lg { margin-bottom: 2rem; }
.slab-sm { margin-top: 1rem; } /* gap collapses to 2rem */
.stack { display: flex; flex-direction: column; gap: 1rem; }
Example
Interactive preview availablebody {
margin: 0;
font-family: "Segoe UI", system-ui, sans-serif;
line-height: 1.45;
color: #1c2430;
background: #f6f4ef;
}
.page {
max-width: 40rem;
margin-inline: auto;
padding: 1.1rem 1.15rem 1.5rem;
}
h1 { margin: 0 0 0.3rem; font-size: 1.4rem; }
.lead { margin: 0 0 0.4rem; color: #5c6b7a; }
h2 { margin: 1rem 0 0.45rem; font-size: 1rem; }
.slab {
margin: 0;
padding: 0.55rem 0.75rem;
background: #fff;
border-radius: 8px;
}
.slab-lg {
margin-bottom: 2rem;
background: #ccfbf1;
}
.slab-sm {
margin-top: 1rem;
background: #fef3c7;
}
.h-row span {
display: inline-block;
padding: 0.45rem 0.6rem;
}
.h-left {
margin-right: 1rem;
background: #ccfbf1;
}
.h-right {
margin-left: 1rem;
background: #fef3c7;
}
.leaky {
outline: 2px solid #b45309;
background: #fff7ed;
}
.leaky .slab { margin: 1.5rem 0; }
.contained {
padding: 0.75rem;
border: 2px solid #0f766e;
background: #f0fdfa;
}
.contained .slab { margin: 1rem 0; }
.flex-stack {
display: flex;
flex-direction: column;
gap: 1rem;
padding: 0.75rem;
background: #fff;
border: 1px solid #e3ddd2;
border-radius: 10px;
}
.note { margin: 0.35rem 0 0; color: #5c6b7a; font-size: 0.85rem; }
Expected Output
A teal block and an amber block stack on the paper background. The empty space between their backgrounds is 2rem, the larger of 2rem and 1rem, not the 3rem sum. Beside that, two inline blocks show a 2rem horizontal gap because a 1rem right margin and a 1rem left margin add. The orange outline around the leaky section hugs the text. The paragraph's 1.5rem margin sits outside that outline, and the peach background does not fill it. The teal-bordered section keeps its child's margin inside the padding. The flex stack shows two white slabs 1rem apart, and that gap does not change if you add equal vertical margins later and then remove them in favor of gap.
Line-by-Line Walkthrough
- Padding on .slab is inside the background, so the words sit away from the colored edge. That space belongs to the box.
- .slab-lg sets margin-bottom: 2rem and .slab-sm sets margin-top: 1rem. Vertical sibling margins collapse, so the gap is 2rem, not 3rem.
- The larger margin wins the collapse. Equal margins collapse to that same value, not to double.
- Horizontal margins on .h-left and .h-right add. A 1rem right margin plus a 1rem left margin is a 2rem gap.
- .leaky has an outline and a background but no padding and no border. Outline does not stop collapse, so the child margin leaks out.
- The orange outline hugs the padding edge of the parent, which is why the background looks shorter than the space the paragraph occupies.
- .contained has padding and a border. Either one is enough to stop parent-child collapse, and the child margin stays inside the teal border.
- A new block formatting context also stops that leak. Flex, grid, and overflow other than visible all do this.
- .flex-stack is a column flex container with gap: 1rem. Flex items do not collapse margins with each other, and gap is not a margin.
- The slabs inside the flex stack keep margin: 0, so the 1rem you see is only the gap. There is nothing to collapse.
Use padding inside a component and gap between components. Reach for margin when one sibling must push away from another and you have checked the collapse.
Common Pitfalls & Security
Collapse only affects vertical margins of blocks in the same formatting context. Expecting every margin to add is how empty sections and doubled gaps sneak into a page.
- Horizontal margins never collapse. A row that looks too loose is usually two side margins added together, or a margin plus a gap.
- Parent-child collapse makes a heading's top margin escape the hero, so the background starts lower than you drew it. Add padding or a border on the hero.
- Display flex or grid on the parent stops the leak, which is why a card "fixes itself" when you turn it into a flex column. Know that you changed the formatting context.
- Negative margins collapse too, and the result can pull a sibling upward over the previous box. Measure before you use them to fake an overlap.
- Empty paragraphs still collapse. A spacer div with a margin is less predictable than gap or a single padding on the parent.
Best Practices & Modern Standards
Space the inside of a component with padding, and space sibling components with gap on a flex or grid parent. That pairing avoids collapse entirely. When you still use margin, put it on one side only, usually margin-bottom on the first sibling, so you are not adding two numbers that will not both survive.
Store the space scale in custom properties, such as --space: 1rem, and use the same token for padding and for gap. A card grid can then change density by changing one token. Container queries can tighten that padding when the card's column gets narrow, without a new margin-collapse puzzle at each breakpoint.
Real-World Use
A lesson page stacks a title, a summary, and a list of objectives. If each block has a top and bottom margin, the gaps collapse and the rhythm is the larger margin, which is rarely what the mockup measured. Putting the stack in a flex column with a 1rem gap makes every interval the same.
The card around the objectives uses padding so the text never touches the border, and the card's margin no longer leaks through the section once the section is a flex or grid container.
Frequently Asked Questions
When do vertical margins collapse?
Adjacent vertical margins of block boxes collapse when nothing separates them. That includes two siblings, and a parent with its first or last child if the parent has no padding, no border, and no new formatting context. The used gap is the larger margin. If one margin is negative, the margins are added, and the negative one pulls inward.
Why did my parent background ignore the child margin?
The child margin collapsed through the parent and now lives outside it. Backgrounds paint the padding edge, not the margin, so the color looks too short. Give the parent even 1px of padding or a border, or make the parent a flex or grid container. An outline will not stop the collapse, which is why the sample uses one to reveal the leak.
Should I use margin or padding inside a card?
Use padding between the card border and the text, because padding keeps the background and stays inside the width when you use border-box. Use margin or, better, gap to separate one card from the next card. Mixing both on every side makes the spacing hard to predict once collapse starts.
Does gap collapse like margin?
No. Gap on a flex or grid container is a fixed space between items, and it is not a margin. Items can still have margins, which add to the gap and can collapse only in the ways margins always collapse. For a stack of cards, set item margins to zero and let gap do the work.