# CSS Box Model

> Each part of a page is a box. Space and a border wrap around what is in it.

**Field:** Software Development  
**Category:** Web Development  
**Also known as:** Box Model, Margin and Padding, box-sizing

## Think of it like

Think of it like a framed photo on a wall. The photo, the mat around it, the frame, then the gap to the next frame.

## Definition

The CSS box model says every element is a box made of four layers. In the middle is the content, like text or a picture. Padding is the space inside, around the content. The border is the edge you can see. Margin is the space outside, between this box and the next. Width and height count only the content, unless you change box-sizing.

## Example

Emma sets a box to 300px wide with 20px of padding. It shows up 340px wide, until she adds box-sizing: border-box.

## Example code

```css
* {
  box-sizing: border-box;
}
```

With this rule, width includes the padding and border, which is easier to plan.

## Why it matters

Every gap and every size on a page comes from these four layers. Knowing them ends most layout puzzles.

## Common mistake

Mixing up padding and margin. Padding is inside the border, margin is outside it.

## Three principles

- **Four layers.** Content, padding, border, margin.
- **Inside or outside.** Padding inside the border, margin outside.
- **Watch the width.** border-box makes sizes add up simply.

## Related terms

- [Cascading Style Sheets (CSS)](https://learn.benibix.com/term/development/css)
- [Flexbox](https://learn.benibix.com/term/development/flexbox)
- [CSS Grid](https://learn.benibix.com/term/development/css-grid)
- [HTML Element](https://learn.benibix.com/term/development/html-element)
- [Gutter](https://learn.benibix.com/term/design/gutter)
- [CSS Units](https://learn.benibix.com/term/development/css-units)
- [CSS Position](https://learn.benibix.com/term/development/css-position)

## Taught in

- [The CSS Box Model](https://learn.benibix.com/learn/development-web/css-box-model): HTML, CSS and JS, 8/14

![Four boxes nested inside each other. In the middle, the content, text or a picture. Around it, padding, the space inside. Around that, a thick border, the edge you can see. 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)

---

Source: https://learn.benibix.com/term/development/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-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
