# The CSS Box Model

> Why every part of a page is a box with padding, a border and a margin. How box-sizing changes its width, and which unit to size it in.

**Course:** [HTML, CSS and JavaScript](https://learn.benibix.com/learn/development-web)  
**Time:** 12 min

**After this lesson you can:**

- Name the four layers of the box
- Work out the real width of a box
- Pick px, rem or % for a size

## What the four layers of a box are

Every element on a page is a box with four layers. In the middle is the content. Around it is the padding, then the border, then the margin. By default, width sets only the content. So a box with width: 300px, 20px of padding and a 2px border is really 344 pixels wide.

Each layer has its own job. Padding is space inside the border, and it takes the box's background colour. The border is the line you can see. Margin is space outside the border that pushes other boxes away. It is always clear, so the page behind shows through it.

That extra width catches everyone out. Two boxes set to width: 50% will not fit side by side once they have padding, because each is now more than half. The fix is one line: box-sizing: border-box. Then width covers the content, the padding and the border together, and 50% means 50%.

Most sites put box-sizing: border-box on every element at the top of their CSS. It makes sizes work the way people expect. Margins sit outside it either way. A margin is the gap between two boxes, not part of one.

Sizes can be set in different units. A px is a fixed screen pixel. A % is a share of the box around it. A rem is a share of the page's base text size, which starts at 16 pixels in most browsers, so 1.5rem is 24 pixels. A vw is one hundredth of the window's width.

The choice matters for the reader. Someone who sets a larger text size in their browser gets bigger text where you used rem, and no change where you used px. So use rem for text and spacing, % or fr for widths, and px for thin lines such as borders.

When a box is the wrong size, guessing wastes time. Browser DevTools open with F12 in most browsers. They draw every box as four coloured layers. Hover over an element and you see its content, padding, border and margin, each with its size in pixels.

```css
*, *::before, *::after {
  box-sizing: border-box;   /* width now includes padding and border */
}

.card {
  width: 300px;
  padding: 1.25rem;         /* 20px when the base size is 16px */
  border: 2px solid #ccc;
  margin-bottom: 1rem;      /* space between this card and the next */
}
```
With border-box, the card is 300 pixels wide, padding and border included.

![Four boxes nested inside each other. In the middle, the content. Around it, the padding. Around that, a thick border. Outside it, a dashed line marks the margin, the space between this box and the next.](https://learn.benibix.com/diagrams/dev-box-model.svg)

### Example: Emma's two columns will not sit side by side

Emma sets two columns to width: 50% with 24px of padding each. The second column keeps dropping below the first.

DevTools shows each column is 50% plus 48 pixels. She adds box-sizing: border-box, and both columns fit on one line.

**Words in this lesson:** [CSS Box Model](https://learn.benibix.com/term/development/css-box-model), [CSS Units](https://learn.benibix.com/term/development/css-units), [Browser DevTools](https://learn.benibix.com/term/development/browser-devtools)

**Question:** How wide is a box with width 300px, 20px padding and a 2px border, by default?

**Answer:** 344 pixels. By default width covers only the content. Add 20 pixels of padding and 2 of border on each side: 300 plus 40 plus 4 makes 344.

---

Source: https://learn.benibix.com/learn/development-web/css-box-model
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
