1.card {2 width: 200px;3 height: 120px;4 padding: 20px;5 border: 10px solid;6 margin: 30px;7 box-sizing: border-box;8}contentnext box200px120px20px10px30pxborder-box
Left: style.css, one rule for .cardRight: the card box, to scale: content, padding, border, marginTokens: each value on its way from its line to the boxLeft: the stylesheet, one rule for the .card box. Right: the parent box with the card drawn to scale as four nested layers (content, padding, border, margin) and the next box in front of it. Each declaration hops to the box and grows its layer; the rulers measure the box border to border and how far down the next box starts. Last, box-sizing: border-box fits padding and border inside the 200 × 120. A real browser applies all of a rule's declarations at once. The figure applies them one at a time so you can see what each one does.

Four layers

  1. The browser lays out every element as a rectangle, a box. Each box has four layers, from the inside out: content, padding, border and margin. Here one box, the .card div, is built from this CSS rule, one line at a time.

Building the box

Measuring it

What happens

;; step 1/7 · four-layers
every element = a box, four layers:
  content   the text and images
  padding   space inside, around the content
  border    a line around the padding
  margin    space outside, between boxes
.card with every line applied: 260 × 180 border to border

A real browser applies all of a rule's declarations at once. The figure applies them one at a time so you can see what each one does. Widths and depths in the drawing are to scale (the same number of drawing units for every CSS pixel); the layer heights are only there to show the layers. The next box is drawn 40px tall.