Difficulty: Beginner
Explain the CSS box model. What is the difference between content-box and border-box, and what is margin collapsing?
Every element in CSS is rendered as a rectangular box made of four layers: content, padding, border and margin. The box-sizing property decides what width and height measure: with the default content-box they size only the content, so padding and border are added on top; with border-box they include padding and border, so the box is exactly the width you set.
width/height (under content-box).Open DevTools, select an element and look at the box diagram in the Computed tab to see all four with real pixel values.
content-box (default) | border-box | |
|---|---|---|
width covers | Content only | Content + padding + border |
width: 200px; padding: 20px; border: 5px renders as | 250px wide | 200px wide (content shrinks to 150px) |
| Mental model | Add everything up | What you set is what you get |
| Typical use | Legacy default | Almost every modern reset |
border-box makes layouts predictable. A width: 50% column with padding still fits two per row, and a width: 100% input with padding no longer overflows its container. That is why most resets (Tailwind's preflight, modern-normalize and others) apply box-sizing: border-box to every element and its pseudo-elements.
In normal block flow, vertical margins between blocks can merge into one margin equal to the larger of the two (or the sum if one is negative). It happens in three cases:
margin-bottom: 20px on one paragraph and margin-top: 30px on the next give a 30px gap, not 50px.Collapsing does not happen for horizontal margins, for flex or grid items, for floated or absolutely positioned elements, or across a new block formatting context. display: flow-root on the parent is the cleanest way to stop the parent-child case.
<span> ignore vertical margins and width/height; set display: inline-block or block if you need them.outline and box-shadow do not take space in the box model, which is why they are safe for focus rings and hover effects.gap in flex and grid layouts instead of margins between children, which sidesteps collapsing entirely..card-a {
box-sizing: content-box; /* the default */
width: 200px;
padding: 20px;
border: 5px solid #333;
/* Rendered width: 200 + 20*2 + 5*2 = 250px */
}
.card-b {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid #333;
/* Rendered width: 200px, content area shrinks to 150px */
}
/* The reset almost every project uses */
*, *::before, *::after {
box-sizing: border-box;
}
With border-box the number you write is the number you see on screen, which is why it is applied globally in nearly every modern codebase.
/* 1. Siblings: gap is 30px, not 50px */
.intro { margin-bottom: 20px; }
.details { margin-top: 30px; }
/* 2. Parent/child: child's margin leaks above .panel */
.panel h2 { margin-top: 24px; }
/* Fix: give the parent its own block formatting context */
.panel { display: flow-root; }
/* 3. Flex and grid items never collapse */
.stack {
display: flex;
flex-direction: column;
gap: 16px; /* prefer gap over margins between children */
}
Collapsing is a block-flow behaviour only. Creating a new formatting context with flow-root, or switching to flex or grid with gap, removes the surprise.
Box model, box-sizing, content-box, border-box, Margin collapsing