Frontend / The CSS box model

The CSS box model

Every element on a web page is a rectangle with four layers. Build one layer by layer, then see how its width is counted and how boxes sit next to each other.

Beginner7 steps~6 min
Content — the text or image itself
Padding — space inside the border
Border — the line around the padding
Margin — transparent space outside the border
// THE SHORT VERSION

In 30 seconds

A browser treats every element as a box with four layers: the content, then padding around it, then a border, then margin on the outside.

What width means depends on box-sizing. By default it measures the content only, so padding and border make the box bigger. With border-box it measures the box out to its border.

Boxes also interact: touching top and bottom margins of stacked blocks overlap instead of adding up, and inline boxes flow inside lines of text rather than stacking.

The CSS box model, step by step

STEPS01 / 07

Drawings are scaled up, not pixel-exact. The inline rules shown are for ordinary text-level elements: images and inline-block boxes sit in a line but do take a width and height.

STEP 01 / 07 Content: the inner box
THE MATH
width: 200px height: 100px
  1. Step 1: Content: the inner box. To the browser, every element on a page is a rectangle. At its centre is the content area, where the text or image goes. By default, width and height set the size of this inner area only.
  2. Step 2: Padding: space inside. Padding is breathing room between the content and the edge of the box. It belongs to the element: the element’s background colour fills it. Here it adds 20px on every side.
  3. Step 3: Border: the edge. The border is a line drawn around the padding. It has a thickness, here 5px, and that thickness takes up real space on the page just like padding does.
  4. Step 4: Margin: space outside. Margin is empty space outside the border that keeps neighbouring boxes away. It is always transparent and never takes the element’s background. Content, padding, border, margin: that is the whole box model.
  5. Step 5: content-box vs border-box. box-sizing decides what width measures. With content-box, the default, 200px is the content, and padding and border are added on top: 250px on screen. With border-box, 200px is the whole box out to the border, and the content shrinks to 150px. Margin is never counted.
  6. Step 6: Margins collapse. Stack two blocks and their touching top and bottom margins do not add up. They overlap, and the larger one wins: the gap is 30px, not 50px. Left and right margins never collapse, and neither do margins inside a flex or grid container.
  7. Step 7: Block boxes, inline boxes. Boxes are laid out in two ways. A block box, such as a div or p, starts on a new row and stacks downwards, even when it is narrower than the row. An inline box, such as a span or a link, sits inside a line of text and wraps with it; width, height and top and bottom margins have no effect on it.
VALUES SO FAR
width200pxcontent
height100pxcontent
padding····inside
border····edge
margin····outside
total····default
total····reset
gap····collapse

Spotted a mistake on this page?