-- online Search!

The CSS Box Model

by kirupa   |   filed under HTML and CSS

TL;DR

Every element on a page is a rectangle made of four layers: content in the middle, then padding, a border, and an outer margin. Together, they're the CSS box model. By default, width only sizes the content, so padding and borders make the box bigger, and box-sizing: border-box fixes that. Vertical margins between blocks collapse into the larger one.

  • Our 200-pixel card with 20 pixels of padding and a 5-pixel border ends up 250 pixels wide on screen, and it takes up 310 pixels with its 30-pixel margins.
  • With box-sizing: border-box, the width covers the padding and border too, so the same card is exactly 200 pixels wide and its content shrinks to 150.
  • Padding sits inside the border and takes the background color, while margin sits outside, stays see-through, and pushes neighbors away.
  • A 30-pixel bottom margin above a 20-pixel top margin makes a 30-pixel gap instead of 50, except inside flex and grid containers, where margins never collapse.

Here's a fun experiment. Take a div, give it a width of 200 pixels, add some padding and a border, and then measure it on the screen. It won't be 200 pixels wide. It'll be bigger! Now stack a heading on top of a paragraph, give each of them a margin, and measure the space between them. That gap will be smaller than the two margins added together. Neither of these is a bug. Both happen because of a few simple rules the browser follows every time it figures out how big something is and where it goes.

In this tutorial, we'll take one element apart layer by layer, see exactly how the browser adds up its size, and learn the fix for each of those two surprises. You'll end up using both of them on pretty much every page you build.

Onwards!

Every Element Is a Box

Every element the browser draws, whether it's a heading, a paragraph, an image, or a button, sits inside an invisible rectangle. Even a perfectly round profile picture is a rectangle as far as the layout is concerned (the roundness is just paint!). To see what that rectangle is made of, we'll use a simple card as our running example:

<div class="card">Hello!</div>

Here's the CSS that styles it:

.card {
  width: 200px;
  height: 100px;
  padding: 20px;
  border: 5px solid goldenrod;
  margin: 30px;
}

The rectangle around our card isn't one solid thing. It's made up of four layers nested inside each other, a lot like a framed picture hanging on a wall. The picture itself is in the middle. Around it is a mat that gives the picture some breathing room. Around the mat is the frame. Around the frame is the empty wall space that keeps the next picture from bumping into this one:

A box drawn as four nested layers. In the middle is the content. Around it is the padding, then the border, and on the outside, the margin. The margin is see-through, so whatever is behind the box shows through it.

The browser's version of this has the same four layers, and each one lines up with a property we set on our card:

  • Content is the picture. It's where our text, images, or child elements go, and its size comes from width and height.
  • Padding is the mat. It's the space between the content and the border, and it's filled in with the element's background color.
  • Border is the frame. It wraps around the padding, and it can be thick, thin, dotted, or not there at all.
  • Margin is the empty wall space. It sits outside the border and pushes neighboring elements away. It's always see-through, so whatever is behind the element shows through it.

These four layers together are known as the box model. Every element on every page follows it, so once it clicks, a whole bunch of "why is this thing that size?" mysteries go away. If the parts of the CSS rule above look unfamiliar, the Anatomy of a CSS Style tutorial walks through them.

How Wide Is Our Box, Really?

Let's go back to our card. We gave it a width of 200px, so it should be 200 pixels wide, right? As it turns out, no. By default, width and height only set the size of the content. The padding and border get added on top of that, on both sides:

A row of colored segments from left to right: margin 30, border 5, padding 20, content 200, padding 20, border 5, and margin 30. The box we see on screen, from border to border, is 250 pixels wide, and with its margins it takes up 310 pixels of space.

Starting from the content's 200 pixels, we add 20 pixels of padding on the left and 20 on the right, and then 5 pixels of border on each side. That's 200 + 40 + 10, which gives us a card that's 250 pixels wide on the screen. The margin doesn't change how big the card looks, since it's see-through, but it still claims space in the layout. Add the 30 pixels of margin on each side, and our card takes up 310 pixels of room. The same math happens vertically, so our 100-pixel-tall card is really 150 pixels tall from border to border.

This adding up catches everyone at some point. A common way it shows up is when we set two boxes to width: 50% so they sit side by side, add a little padding to each, and the second box suddenly drops down to the next line. The padding made the two of them wider than their parent, so they no longer fit. Fortunately, there's a one-line fix.

How Does box-sizing: border-box Help?

The box-sizing property tells the browser which layers width and height should measure. Its default value is content-box, which gives us the adding-up behavior we just saw. If we change it to border-box, the width covers everything from the outside edge of the left border to the outside edge of the right border:

.card {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid goldenrod;
}

Now our card is exactly 200 pixels wide on the screen. The padding and border still need their 50 pixels, so they get carved out of the inside, and the content shrinks to make room:

Two boxes with the same CSS: a width of 200px, 20px of padding, and a 5px border. With box-sizing: content-box, the content is 200px wide and the box ends up 250px wide. With box-sizing: border-box, the box is exactly 200px wide, and the content shrinks to 150px to make room.

With border-box, the content area is 200 - 40 - 10, which leaves 150 pixels for our text. This is a much easier way to think about sizes. The number we write is the size we see, and we can add or remove padding without the element growing or shrinking on us.

It's so handy that a lot of developers turn it on for every element right at the top of their stylesheet:

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

The * selector matches every element, and the ::before and ::after selectors catch the extra pieces (known as pseudo-elements) that some styles generate. With this one rule, every box on the page plays by the easier rules.

Here's a box we can poke at. It uses the same 200-pixel width as our card, and the striped orange area around it is its margin:

Try it

  1. Drag Padding all the way up
  2. Press border-box
  3. Drag the sliders again and watch Box on screen

With content-box, every padding or border change makes the box on screen bigger or smaller. With border-box, the box stays put at 200 pixels, and only the content inside it gets squeezed. Dragging the margin never changes the box itself, only how much space it takes up.

Should I Use Padding or Margin?

Padding and margin both add space, so it's easy to grab the wrong one. The difference is which side of the border the space lands on:

  • Padding is inside the border. It's painted with the element's background color, and it's part of the element, so clicking or tapping on it counts as clicking on the element.
  • Margin is outside the border. It's always transparent, it isn't clickable, and its only job is to push other things away.

A button is a good example. If we want a bigger button that's easier to tap, with more colored area around its label, we add padding. If we want the button to stay the same size but keep its distance from the text next to it, we add margin:

.button {
  background-color: #0798ff;
  padding: 12px 24px;  /* bigger and easier to tap */
  margin-left: 16px;   /* space from its neighbor */
}

A good rule of thumb is that if the space should look like part of the element, it's padding. If the space is between elements, it's margin. And if all we want is a visible line around something that doesn't take up any room at all, an outline is drawn outside the border without moving anything, which the Display an Outline (Not a Border!) on Hover tutorial shows off.

How Do the Shorthand Values Work?

So far, we've given every side the same value. We can set each side on its own with properties like margin-top and padding-left, but most of the time we'll use the shorthand and list up to four values. With four values, they go around the box clockwise, starting at the top:

The declaration margin: 10px 20px 30px 40px. Its four values go around the box clockwise, starting at the top: 10px on top, 20px on the right, 30px on the bottom, and 40px on the left.

The order is top, right, bottom, left. If that's hard to remember, a lot of people think of the word TRouBLe. When we give fewer than four values, the browser fills in the missing sides from the ones we did give:

ValuesExampleWhat it means
1margin: 10pxAll four sides get 10px
2margin: 10px 20pxTop and bottom get 10px, left and right get 20px
3margin: 10px 20px 30pxTop gets 10px, left and right get 20px, bottom gets 30px
4margin: 10px 20px 30px 40pxTop, right, bottom, and left, in that order

The same patterns work for padding and border-width too. One margin value that's worth knowing about is auto. When a block element has a width, setting its left and right margins to auto splits the leftover space evenly between them, which centers the element. The Centering a Div Horizontally tutorial uses exactly this trick.

Why Do Vertical Margins Collapse?

Now for the second surprise. Let's say we have a heading with a bottom margin of 30 pixels, and right below it, a paragraph with a top margin of 20 pixels:

h1 {
  margin-bottom: 30px;
}

p {
  margin-top: 20px;
}

It seems reasonable to expect a 50-pixel gap between them. Instead, the gap is only 30 pixels:

On the left, what we might expect: a heading with a 30px bottom margin and a paragraph with a 20px top margin, stacked for a 50px gap. On the right, what browsers do: the two margins overlap, and the gap is only 30px, the size of the larger margin.

When the top and bottom margins of two blocks touch, they don't stack. They overlap, and only the bigger one counts. This is known as margin collapsing. Our 20-pixel margin is still there. It's just tucked inside the 30-pixel one.

This is on purpose, and it makes the most sense with text. Picture a stack of paragraphs, each with a 16-pixel margin above and below it. If margins added up, the gap between two paragraphs would be 32 pixels, while the gap above the very first paragraph would only be 16. Because they collapse, every gap is 16 pixels, and the spacing looks even.

There are a few rules that decide when margins collapse:

  • Only vertical margins collapse. Left and right margins always add up.
  • Margins collapse between blocks stacked on top of each other, like our heading and paragraph.
  • They also collapse between a parent and its first or last child. If nothing separates a parent's top edge from its first child's top margin, that margin leaks out of the parent and shows up above it. Giving the parent some padding, a border, or display: flow-root keeps the margin inside.
  • Margins never collapse inside a flex or grid container. Their children keep every pixel of their margins.

Here's our heading and paragraph, ready to be poked at. The striped orange area between them is the margin:

Try it

  1. Drag Paragraph's margin-top past 30px
  2. Press display: flex
  3. Compare Margins added up with Actual gap

While the container is a regular block, the actual gap always matches whichever margin is bigger. Once we switch it to a flex container, the margins stop collapsing, and the gap becomes the full sum of both of them.

Do Inline Elements Follow the Box Model?

Everything we've looked at so far applies to block elements like div, p, and headings, which stack on top of each other. Inline elements like span, a, and strong flow along with the text around them, and they only follow part of the box model:

  • Their width and height are ignored. An inline element is exactly as big as the text inside it.
  • Left and right padding, borders, and margins work normally and push the neighboring text away.
  • Top and bottom padding and borders are drawn, but they don't push the lines above and below away, so they can overlap them. Top and bottom margins do nothing at all.

Images are the exception here (along with form controls like buttons). They sit inline with the text, but they still respect a width, a height, and vertical margins. For everything else, if we want something like a link to behave like a little box while still sitting in a line of text, we can give it display: inline-block:

a.pill {
  display: inline-block;
  padding: 6px 14px;
  margin: 4px 0;
  border-radius: 999px;
}

Now the padding and margin push other things away in every direction, just like the card we started with.

How Can I See the Box Model in My Browser?

We don't have to do any of this math in our heads. Every browser's developer tools will draw the box model for whatever element we pick. Right-click on an element, choose Inspect, and look in the Computed tab (or the Layout tab in Firefox). There's a diagram of the content, padding, border, and margin nested inside each other, using colors that are very close to the ones in this tutorial's figures. Hovering over an element in the list of elements also highlights its layers right on the page. When a layout looks off by a few pixels, this is the first place to check.

Conclusion

The box model seems a bit fussy at first, and then it turns out to explain a big chunk of the layout bugs you'll run into. Every element is a box with four layers: content, padding, border, and margin. By default, width and height only size the content, so the padding and border get added on top, which is why our 200-pixel card ended up 250 pixels wide. Switching to box-sizing: border-box makes the width we write the width we see. Vertical margins between blocks collapse so that only the bigger one counts, except inside flex and grid containers. Inline elements only follow part of these rules, unless we make them inline-block.

Now that we know how a box gets its size, the next questions are what units we should measure it in and which of our CSS rules gets the final say when several of them try to size the same box. The CSS units and cascade and specificity tutorials answer those!

Before you go, here are a few things you can do:

  1. Ask a question

    Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright © KIRUPA 2026 //--