completefrontend

What is the CSS box model?

Every element on a page is a box with four layers: content, padding, border, and margin. Here's how those layers add up to a box's size, what box-sizing changes, and how to see it all in DevTools.

Four nested bands from the outside in: a dashed margin, a peach border, a light padding band, and the content in the middle

Hang a framed photo on a wall. There's the photo itself, a strip of white card around it, then the wooden frame, then some bare wall before the next frame. A web page is built the same way, out of boxes with layers, and the CSS box model describes those layers.

The CSS box model says every element is a rectangular box with four layers: content in the middle, then padding, then a border, and margin on the outside. Padding is space inside the border, and margin is space outside it. The box model also decides how big each box ends up, which is where many layout surprises come from.

What are the four parts of the box model?

Each layer matches a part of the framed photo.

  • Content is the photo: the text or image itself. You size it with width and height.
  • Padding is the white card. It's space inside the border, and the box's background color shows through it.
  • Border is the frame. It's a line around the padding, with a thickness, a style, and a color.
  • Margin is the bare wall. It's invisible space outside the border that pushes other boxes away.

Here's a card that sets all four.

.card {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
  margin: 30px;
}

This card has 300 pixels of content width and 20 pixels of padding on every side. Around that sits a 5-pixel black border, and around the border, 30 pixels of margin.

The box model drawn as nested boxes, margin outside, then border, then padding, then content, next to the width math: 350 pixels wide by default, and 300 pixels wide with box-sizing border-box
The four layers of a box, and how its width adds up.

How is the width of a box calculated?

By default, width sets the width of the content only. The padding and border are added on top. So on the screen, the card above is wider than 300 pixels.

Its visible width is 300 + 20 + 20 + 5 + 5, which is 350 pixels. That's the content, the padding on both sides, and the border on both sides.

Margin isn't part of the box's size, but it still takes up room. With 30 pixels on each side, the card needs 410 pixels of space on the page. Height works the same way. This default is called the standard box model, or content-box.

What does box-sizing border-box do?

With box-sizing: border-box, the width you set is the width of the visible box, border included. The browser makes the content smaller so the padding and border fit inside.

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

Now the card is exactly 300 pixels wide on screen. The content shrinks to 250 pixels: 300, minus 40 for the padding, minus 10 for the border.

Turning this on for every element is a common choice, because sizes become much easier to predict.

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

A selector picks which elements a rule styles, and * picks every element. The ::before and ::after parts cover the extra boxes CSS can add before or after an element's content.

What's the difference between margin and padding?

Padding Margin
Where it sits Inside the border Outside the border
Background color Shows through it No, it's see-through
Can it be negative? No Yes
Clicks on it Count as clicks on the element Go to whatever is behind it
Can it collapse? No Yes, top and bottom margins can

A simple rule: use padding to give content room inside its box, and use margin to push boxes apart.

What is margin collapsing?

Block boxes, like paragraphs and divs, each start on a new line. When two of them sit one above the other, their touching margins can merge into one. The bigger margin wins, and the smaller one disappears. This is called margin collapsing.

.first {
  margin-bottom: 50px;
}

.second {
  margin-top: 30px;
}

You might expect 80 pixels between these two boxes. You get 50, the larger of the two margins.

Only top and bottom margins collapse, never left and right. It doesn't happen inside a flex or grid container either. One more case catches people out. A child's top margin can poke out above its parent when the parent has no border or padding on top. Adding a little padding to the parent stops it.

How do you see the box model in DevTools?

DevTools, the browser's built-in tools for inspecting a page, can show you the box model of any element. Here's how in Chrome or Firefox.

  1. Right-click an element on the page and choose Inspect.
  2. DevTools opens with that element selected. Chrome calls this the Elements panel, and Firefox calls it the Inspector.
  3. Find the box model diagram next to the element's styles. It shows nested rectangles for margin, border, and padding, with the content size in the middle.
  4. Hover over the element's line of HTML. The page highlights its content, padding, and margin in different colors.
  5. Change a number in the diagram (in Chrome, double-click it) and watch the page update.

The same DevTools can pause your JavaScript line by line, as debugging in the browser shows. The DOM tree lesson explores the Elements panel too.

In JavaScript, offsetWidth reads a box's width including padding and border. For the first card, that's 350. Sizes and coordinates covers this and the other size numbers.

Why is my element wider than its width?

  • Padding on a full-width box. A box with width: 100% plus padding ends up wider than its parent, so it spills out. Setting box-sizing: border-box fixes it.
  • Forgetting the border. Even a 1-pixel border adds 2 pixels to the width in the default model.
  • Sizing inline elements. Width, height, and top and bottom margins do nothing on inline elements, the ones that sit inside a line of text, like <span> and <a>. Give them display: inline-block if you need those.
  • Expecting margins to add up. Two vertical margins that touch collapse into the larger one.

Where to go next

Boxes are what every layout moves around. How to center a div puts one in the middle, and flexbox vs grid lines them up in rows and columns. If you're brand new, start with HTML vs CSS vs JavaScript.

The browser works out every box's size and position in a step called layout. The pixel pipeline shows where layout fits, and layout performance shows how to keep it fast. When a box changes size after the page loads, the content below it jumps. Fixing CLS shows how to reserve each box's space ahead of time.

To change these styles from code, read styles and classes in the JavaScript course. MDN's box model guide and mastering margin collapsing go deeper.

Written by Shrikanth PS

Senior Software Engineer at Microsoft, working on Copilot Enterprise, with more than nine years in frontend engineering.