Skip to content
Velloreth
Web Development 6 min read

The CSS Box Model Explained With Examples

Every element is a box of content, padding, border and margin. Learn how the box model works and why box-sizing changes everything.

By

The four layers

Every element is a box with four layers, from the inside out.

┌─────────────────────────────────┐
│           margin                │
│  ┌───────────────────────────┐  │
│  │         border            │  │
│  │  ┌─────────────────────┐  │  │
│  │  │      padding        │  │  │
│  │  │  ┌───────────────┐  │  │  │
│  │  │  │    content    │  │  │  │
│  │  │  └───────────────┘  │  │  │
│  │  └─────────────────────┘  │  │
│  └───────────────────────────┘  │
└─────────────────────────────────┘

Content is the text or the child elements.

Padding is the space inside the border, and it takes the element’s background.

Border is the line around the padding.

Margin is the space outside the border, and it is always transparent.

The default: content-box

By default, box-sizing is content-box, so the declared width applies to the content only.

.box {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
}

The total width is 200 + 20 + 20 + 5 + 5 = 250px.

This is the source of most layout surprise. A width: 100% element with padding overflows its parent, because the padding is added on top.

border-box

Setting box-sizing: border-box makes the declared width include the padding and the border.

*,
*::before,
*::after {
  box-sizing: border-box;
}

Now the same element is 200px in total, and the content area is 200 - 40 - 10 = 150px.

This is the near-universal reset, and it is what most CSS frameworks do. It makes a width predictable, which is what a layout needs.

The universal selector is fine here. The performance concern is a myth for a rule that applies to every element anyway.

Padding versus margin

PaddingMargin
PositionInside the borderOutside the border
BackgroundTakes the element’sAlways transparent
NegativeNot allowedAllowed
CollapsesNoYes, vertically

Use padding to give the content room inside a button or a card.

Use margin to separate one element from another.

A negative margin pulls an element toward its neighbour, which is occasionally useful and usually a sign of a layout that could be simpler.

Margin collapsing

Adjacent vertical margins collapse to the larger of the two.

.a { margin-bottom: 20px; }
.b { margin-top: 30px; }

The gap between them is 30px, not 50px.

It also happens between a parent and its first or last child, which is the more surprising case.

.parent { margin-top: 0; }
.child { margin-top: 20px; }

The child’s margin escapes the parent, and the parent moves down.

The fixes:

  • Add padding-top: 1px to the parent.
  • Add a border-top.
  • Set overflow: hidden or auto on the parent.
  • Use display: flex or grid on the parent.

Any of these creates a block formatting context, which contains the margin.

A flex or grid container does not collapse margins at all, which is one reason a modern layout is more predictable.

Width and max-width

.container {
  width: 100%;
  max-width: 65ch;
  margin-inline: auto;
}

max-width is the basis of a responsive layout. The element fills the available space up to a limit, then stops.

margin-inline: auto centres a block element with a known width. It is the modern shorthand for margin-left: auto; margin-right: auto.

65ch is a measure in characters, which is a good default for a readable line length.

Logical properties

Modern CSS has logical properties that adapt to the writing direction.

.box {
  margin-inline: 1rem;   /* left and right in LTR */
  margin-block: 0.5rem;  /* top and bottom in LTR */
  padding-inline-start: 1rem;
}

inline is the writing direction (horizontal in English), and block is the perpendicular direction.

The benefit: the same CSS works in a right-to-left language, because the logical property follows the direction.

Why the box model matters in practice

The box model is the reason a layout that looks correct in a design tool behaves differently in a browser, and understanding it removes most of the guesswork from a CSS problem. When an element overflows its parent, the cause is almost always a width that does not account for the padding and the border, and the fix is either border-box or a width that subtracts them. When two elements have an unexpected gap, the cause is usually a margin that collapsed, and the fix is to create a block formatting context or to use a flex container. When a card’s content touches its edge, the cause is a missing padding, and the distinction between padding and margin is what determines whether the background extends behind the space. Learning to see every element as four nested rectangles is the single most useful mental model in CSS, because it turns a mysterious layout bug into a specific question about which layer is the wrong size.

A debugging checklist

When a layout does not look right, the box model gives a short list of things to check in order. First, inspect the element and look at the computed width and height, which shows whether the declared value is the content box or the border box. Second, check the padding and the border, because a width that does not account for them is the most common cause of an overflow. Third, check the margin, and specifically whether it collapsed with a neighbour or escaped a parent. Fourth, check the display value, because a flex or grid container changes how the children are sized and whether their margins collapse. Fifth, check the box-sizing on the element and its ancestors, because a single element with content-box in a border-box codebase behaves differently from everything around it. Working through that list takes a minute and resolves the large majority of layout problems, which is a better use of time than changing values at random until the page looks right.

Practical guidance

  • Set box-sizing: border-box globally.
  • Use padding for space inside an element.
  • Use margin for space between elements.
  • Use max-width rather than a fixed width.
  • Use margin-inline: auto to centre.
  • Watch for margin collapsing between a parent and a child.
  • Use a flex or grid container to avoid collapsing.
  • Prefer a logical property for a direction-agnostic layout.

For the related guides, see css-flexbox-complete-guide and css-grid-complete-guide. For the broader context, css-specificity-explained and responsive-design-breakpoints.

Quotable summary

Every element is a box with four layers: content, padding, border, and margin, and the default box-sizing is content-box, so padding and border add to the declared width. Setting box-sizing: border-box makes the declared width include the padding and the border, which is why it is the near-universal reset and what makes a width predictable. Padding is inside the border and takes the element’s background, while margin is outside and is always transparent. Margin collapses between adjacent vertical margins to the larger of the two, and it also collapses between a parent and its first or last child, which is fixed by creating a block formatting context with padding, a border, overflow, or a flex or grid container.

Frequently asked questions

What is the CSS box model?
The model that describes every element as a box with a content area, padding, a border, and a margin. The total space an element occupies is the sum of all four.
What is the difference between padding and margin?
Padding is inside the border, so it takes the element's background and pushes the content inward. Margin is outside the border, is always transparent, and pushes other elements away.
What does box-sizing: border-box do?
It makes the declared width and height include the padding and the border, so an element with width 200px and padding 20px is 200px wide in total rather than 240px.
Why do my margins collapse?
Adjacent vertical margins collapse to the larger of the two, rather than adding. It happens between siblings and between a parent and its first or last child.
How do I stop margin collapsing?
Add padding or a border to the parent, use a flex or grid container, or set overflow to a value other than visible. Any of these creates a block formatting context.
What is the difference between width and max-width?
Width sets a fixed size. Max-width sets an upper bound, so the element can shrink on a smaller screen, which is the basis of a responsive layout.