CSS Box Model
Every element on a web page is drawn as a rectangle. The box model describes the layers that make up that rectangle and how they add up to the space the element occupies. Once you understand it, most spacing and sizing puzzles become easy.
The four layers
From the inside out:
| Layer | What it is | Background visible? |
|---|---|---|
| Content | The text, image or child elements. Sized by width/height. |
Yes |
| Padding | Space between content and border. | Yes |
| Border | A line around the padding. | Border colour |
| Margin | Transparent space outside the border, separating the box from neighbours. | No |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; background: #fdecc8; margin: 0; padding: 10px; }
.box {
width: 180px;
padding: 24px;
border: 10px solid #3366cc;
margin: 30px;
background: #a8d8ff;
}
.content { background: white; text-align: center; }
</style>
</head>
<body>
<div class="box"><div class="content">content</div></div>
<p>Orange = page (margin area shows it), dark blue = border, light blue = padding, white = content.</p>
</body>
</html>Tip: Open your browser's developer tools and select an element. The "Computed" panel shows a coloured diagram of its exact margin, border, padding and content sizes.
Calculating the total size
With the default box-sizing: content-box:
total width = margin-left + border-left + padding-left + width + padding-right + border-right + margin-right
total height = margin-top + border-top + padding-top + height + padding-bottom + border-bottom + margin-bottomFor the box above: 30 + 10 + 24 + 180 + 24 + 10 + 30 = 308px of horizontal space, of which 248px is the visible box (border to border).
Let's prove it by placing a 248px ruler beneath a box:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.box { width: 180px; padding: 24px; border: 10px solid #8e44ad; background: #e8d4f2; }
.ruler { width: 248px; height: 10px; background: repeating-linear-gradient(90deg, #222 0 2px, #ffd200 2px 20px); margin-top: 4px; }
</style>
</head>
<body>
<div class="box">width: 180px</div>
<div class="ruler"></div>
<p>The ruler is 248px: 180 + 2×24 + 2×10.</p>
</body>
</html>border-box: the easier model
If you set box-sizing: border-box, width and height include padding and border. The content shrinks to make room. Most modern stylesheets enable it everywhere:
*, *::before, *::after {
box-sizing: border-box;
}<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.box { width: 220px; padding: 20px; border: 8px solid #e67e22; margin-bottom: 10px; background: #fde3c8; }
.bb { box-sizing: border-box; border-color: #16a085; background: #c9f2e8; }
</style>
</head>
<body>
<div class="box">content-box: 220 + 40 + 16 = 276px wide</div>
<div class="box bb">border-box: exactly 220px wide</div>
</body>
</html>Margin collapsing
Vertical margins between block elements do not add up — they collapse into the larger of the two. If one paragraph has margin-bottom: 30px and the next has margin-top: 20px, the gap is 30px, not 50px. Horizontal margins never collapse, and margins inside flex or grid containers don't collapse either.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.a { margin-bottom: 30px; background: #ffb3b3; padding: 8px; }
.b { margin-top: 20px; background: #b3d1ff; padding: 8px; }
</style>
</head>
<body>
<div class="a">margin-bottom: 30px</div>
<div class="b">margin-top: 20px — the gap above me is 30px, not 50px</div>
</body>
</html>Inline vs block boxes
The full box model applies to block and inline-block boxes. For inline boxes (like <span>), width/height are ignored and vertical padding/margin don't push other lines away. Keep this in mind when styling links and labels.
Test yourself
Which layer of the box model is always transparent?
- Content
- Padding
- Border
- Margin
With content-box, width: 100px; padding: 10px; border: 5px solid; — how wide is the visible box (border edge to border edge)?
- 100px
- 115px
- 130px
- 110px
Two stacked blocks have margin-bottom: 40px and margin-top: 25px. What is the gap between them?
- 65px
- 40px
- 25px
- 15px