The CSS Box Model Explained: Content, Padding, Border, Margin
Every element in CSS is a box made of content, padding, border, and margin. Learn how box-sizing changes what 'width' actually measures.
Published September 21, 2026
Every element is a box made of content, padding, border and margin, from the inside out. Setting box-sizing: border-box makes width include padding and border, which is almost always what you want.
The concept
Content holds text or images. Padding is space inside the border, the border wraps the padding, and margin is the transparent space outside.
With the default content-box, width applies only to the content, so adding padding makes the element wider than declared. border-box includes padding and border in width.
Layers of a box
Practical examples
Size arithmetic
.box { width: 200px; padding: 20px; border: 5px solid; }
/* content-box: 250px wide; border-box: 200px wide */Global reset
*, *::before, *::after { box-sizing: border-box; }Margin collapsing
h1 { margin-bottom: 30px; }
p { margin-top: 20px; } /* gap is 30px, not 50px */Common mistakes
- Forgetting box-sizing and getting overflow after adding padding.
- Expecting vertical margins to add up (they collapse).
- Using margin for space inside an element (that is padding).
Key takeaways
- content → padding → border → margin.
- Use box-sizing: border-box.
- Vertical margins between siblings collapse.
FAQ
Do inline elements use vertical margin?
No; vertical margin and padding do not affect line layout for inline elements.
How do I see the box?
Browser DevTools shows a box-model diagram for any selected element.