CSS Box Model
Understand the CSS box model — content, padding, border, margin — and box-sizing.
What you'll learn
- The four layers of the box model: content, padding, border, margin
- How box-sizing: border-box changes width calculations
- The difference between margin collapse and padding behavior
- How to debug spacing with browser DevTools
Concept
CSS Box Model
Every element on a web page is a rectangular box. The box model describes the four layers of that box, from innermost to outermost: content, padding, border, and margin. Understanding these layers is essential — almost every layout issue a beginner faces (things looking squished, misaligned, or overflowing) traces back to the box model.
The Four Layers
- Content — the actual text or image. Its size is determined by
widthandheight(or by the content itself if not set). - Padding — space *inside* the border, between the content and the border. Padding is part of the element's clickable area.
- Border — the line around the padding. It has a width, style, and color.
- Margin — space *outside* the border, pushing neighboring elements away. Margin is transparent and not part of the element's own area.
box-sizing: border-box
By default, width sets only the *content* width. Padding and border are *added on top*, so a width: 100px element with padding: 10px and border: 1px is actually 122px wide. This causes layouts to overflow unexpectedly. Setting box-sizing: border-box makes width include padding and border, so width: 100px means the *total* box is 100px. Nearly every project applies box-sizing: border-box globally with a reset.
Margin Collapse
When two vertical margins meet, they collapse — the space between them is the *larger* of the two, not their sum. A 20px margin below an element and a 30px margin above the next element produce 30px of space, not 50px. Margin collapse does not happen with horizontal margins, padding, or flex/grid children. It is a common source of confusion when spacing does not add up as expected.
Debugging with DevTools
The browser DevTools Elements panel shows the box model visually — a color-coded diagram with the content size in the center and padding, border, and margin as concentric rectangles. You can edit the values live and see the result instantly. When spacing looks wrong, this diagram is the first place to look.
Example
/* Universal reset: border-box for everything */
*, *::before, *::after {
box-sizing: border-box;
}
.card {
width: 300px; /* total width, includes padding + border */
padding: 20px; /* space inside, part of the 300px */
border: 2px solid #e5e7eb;
margin: 24px; /* space outside, pushes neighbors away */
background: #ffffff;
border-radius: 8px;
}
.card-content {
padding: 12px 16px; /* vertical | horizontal */
margin-bottom: 16px; /* space below, may collapse with next margin */
}
The universal reset applies border-box so width includes padding and border. The card is exactly 300px wide total. Padding creates internal space; margin creates external separation. The shorthand padding: 12px 16px sets vertical and horizontal values.
Try it
- Image Color Picker
Pick border and background colors for your box model elements.
- SVG Optimizer
SVG elements follow the box model too — optimize and inspect their dimensions.
Common mistakes
The mistake
Forgetting box-sizing: border-box and wondering why elements overflow their container.
The fix
Apply *, *::before, *::after { box-sizing: border-box; } at the top of your CSS. This makes width include padding and border, preventing unexpected size growth.
The mistake
Confusing margin (outside) with padding (inside) when spacing elements.
The fix
Padding adds space inside the border, growing the clickable area. Margin adds space outside, pushing neighbors away. Use padding for internal breathing room and tap targets; use margin for separation between distinct elements.
Related Resources
Related Snippets
- CSS Centering Snippet
Centering techniques that rely on understanding the box model.
Related Cheatsheets
- CSS Cheatsheet
Reference for box model properties and box-sizing.
Practice CSS Box Model
7 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.