The CSS Box Model

By

Published · Updated

Tagged: CSS

Learn how the CSS box model works: content, padding, border and margin, how width is calculated with content-box and border-box, inline boxes, and margin collapsing.

Every HTML element is essentially a box. Every element is a generic box.

The box model explains the sizing of the elements based on a few CSS properties.

From the inside to the outside, we have:

The best way to visualize the box model is to open the browser DevTools and check how it is displayed:

Firefox DevTools Layout panel showing CSS box model visualization with content area, padding, border, and margin layers

Here you can see how Firefox tells me the properties of a span element I highlighted. I right-clicked on it, pressed Inspect Element, and went to the Layout panel of the DevTools.

See, the light blue space is the content area. Surrounding it there is the padding, then the border and finally the margin.

How the width is calculated

By default, if you set a width (or height) on the element, that is going to be applied to the content area. All the padding, border, and margin calculations are done outside of the value, so you have to take this in mind when you do your calculation.

Take this box:

.box {
  width: 350px;
  height: 150px;
  padding: 25px;
  border: 5px solid black;
  margin: 10px;
}

You asked for 350px, but the box you see on the page is 410px wide:

350 (content) + 25 + 25 (padding) + 5 + 5 (border) = 410px

and 210px tall, for the same reason. The margin is not part of the box size. It’s empty space added around it, so this box takes 430px of horizontal space in the layout.

This is why a width: 100% element with some padding overflows its parent.

box-sizing: border-box

You can change this behavior using the box-sizing property. It has two values:

With border-box, the same box is 350px wide on the page. The browser shrinks the content area to make room for the padding and border, so the content gets 350 - 50 - 10 = 290px. The margin is still added outside.

Most developers prefer this, because the number you write is the size you see. This is the common way to turn it on for the whole page:

html {
  box-sizing: border-box;
}

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

Every element inherits the value from its parent, so if a component needs content-box you set it once on its root and its children follow. I wrote more about it in CSS box-sizing: border-box.

Inline elements don’t follow all the rules

The box model works as described for block elements like div, p and h1. Inline elements like span, a and strong behave differently:

If you need an inline element to respect its width, height and vertical spacing while staying on the same line as the text around it, use display: inline-block:

a.button {
  display: inline-block;
  padding: 10px 20px;
}

Margin collapsing

This one surprises everyone at some point. When two vertical margins touch, they don’t add up. You get only the bigger one.

h2 {
  margin-bottom: 50px;
}

p {
  margin-top: 30px;
}

The space between an h2 followed by a p is 50px, not 80px.

With negative margins: if one is positive and one negative, they’re added together, so 50px and -10px give 40px. If both are negative, the most negative wins.

Margins also collapse between a parent and its first or last child. If a div has no padding or border and its first child has margin-top: 20px, that margin ends up outside the div, pushing the div itself down. Add padding-top or a border to the parent to stop it.

Margins never collapse:

That’s one reason people like using gap in flex and grid layouts: the spacing is predictable. See CSS margin for more on margins.

Percentages in padding and margin

A percentage padding or margin is calculated from the width of the containing block, also for top and bottom. So padding-top: 50% is half the parent’s width, not half its height.

Outline is not part of the box

outline is drawn outside the border, but it doesn’t take up any space. Adding an outline never changes the size of the box or moves anything around, which is why it’s handy for focus styles and for debugging layouts:

* {
  outline: 1px solid red;
}

Want me to talk about your product? You can sponsor this site.

Related posts about css: