The Box Model
To a browser, every element is a rectangular box. The box model describes the four layers of that box: the content, the padding around it, the border, and the margin outside. Understanding it is the key to controlling spacing and sizes, and to fixing most "why is this too wide?" bugs.
The four layers
- Content: the text, image or child elements. Its size is set by
widthandheight(or by the content itself). - Padding: space between the content and the border. It takes the element's background colour.
- Border: a line around the padding. It can have a width, style and colour.
- Margin: transparent space outside the border that pushes other elements away.
Setting padding and margin
You can set each side separately (padding-top, margin-left and so on) or use the shorthand. The shorthand goes clockwise from the top: top, right, bottom, left.
.card {
padding: 20px; /* all four sides */
padding: 10px 20px; /* top/bottom 10px, left/right 20px */
padding: 10px 20px 30px; /* top 10px, left/right 20px, bottom 30px */
padding: 10px 20px 30px 40px; /* top, right, bottom, left */
}
(Writing four padding lines like this is only for illustration: each one overrides the previous, so the last wins.) Margin works the same way. A useful trick: margin: 0 auto; on a block with a set width centres it horizontally, because the browser splits the leftover space equally between the left and right margins.
Modern CSS also has logical properties that follow the writing direction instead of physical sides: padding-inline (left and right in English), padding-block (top and bottom), margin-inline: auto, and inline-size in place of width. They work in every current browser and automatically flip for right-to-left languages such as Arabic.
How wide is the box? content-box vs border-box
By default (box-sizing: content-box), width sets only the content. Padding and border are added on top. Take this card:
.card {
width: 300px;
padding: 20px;
border: 5px solid navy;
margin: 10px;
}
content-box (default) | border-box | |
|---|---|---|
| Content width | 300px | 300 − 40 − 10 = 250px |
| Visible width (content + padding + border) | 300 + 40 + 10 = 350px | 300px |
| Space taken including margins | 350 + 20 = 370px | 300 + 20 = 320px |
With box-sizing: border-box, the width you write is the width you see, which is far easier to reason about. Almost every modern project starts with this reset:
*,
*::before,
*::after {
box-sizing: border-box;
}
Margin collapsing
When two block elements sit on top of each other, their vertical margins don't add up; they collapse into one margin equal to the larger of the two. If a heading has margin-bottom: 30px and the next paragraph has margin-top: 20px, the gap between them is 30px, not 50px. This only happens with vertical (block-direction) margins in normal flow. It does not happen between flex or grid items, which is one reason many developers use gap in Flexbox and Grid for spacing.
Padding can't be negative, but margins can. A negative margin pulls an element towards its neighbour, which can cause overlaps. Use it deliberately, not as a quick fix.
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Box model</title>
<style>
body { font-family: system-ui, sans-serif; }
.card {
width: 300px;
padding: 20px;
border: 5px solid #1e3a8a;
margin: 10px;
background-color: #dbeafe;
}
.border-box {
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="card">content-box: 350px wide on screen</div>
<div class="card border-box">border-box: exactly 300px wide</div>
</body>
</html>
Two light blue cards with dark blue borders appear one above the other. The first is visibly wider (350px) than the second (300px), even though both say width: 300px. The gap between them is 10px, not 20px, because their vertical margins collapse. Right-click a card, choose Inspect and look at the box-model diagram in the Styles or Computed panel to see each layer measured.
Key points
- Every element is a box: content, padding, border, margin.
- Shorthands go clockwise: top, right, bottom, left.
box-sizing: border-boxmakeswidthinclude padding and border; use it everywhere.- Vertical margins between blocks collapse to the larger one.
Tip: use the ← and → keys to move between pages.
