The idea: every width comes from a fixed procedure

After the cascade has picked a value for every property, the browser runs layout: it works out where each box goes and how big it is. For a box in a row this is plain arithmetic. The box model says what width measures. Flexbox and grid say how a container shares its width among its children. This page runs that arithmetic step by step on small examples, and every number on it is the number a real browser gives for the same CSS.

Two boxes with the same CSS: width 200px, padding 20px, border 5px, margin 10px. On the left, content-box: the content is 200, the border box 250 and the margin box 270. On the right, border-box: the border box is 200, the content 150 and the margin box 220.
Same CSS, two meanings of width: content-box adds padding and border on top, border-box takes them out of the 200px.

How the cascade picks the values is on The CSS Cascade. Where layout sits in the whole pipeline (DOM, style, layout, paint, composite) is on How a Browser Renders a Page.

The box model and box-sizing

Every element is a box made of four layers: the content, then padding, then the border, then the margin. Padding and border are part of the box you see; margin is empty space outside it. The property box-sizing decides which layer width sets:

CSS: width 200px, padding 20px, border 5px, margin 10pxcontentborder boxmargin box
box-sizing: content-box (the default)200200 + 40 + 10 = 250270
box-sizing: border-box200 − 10 − 40 = 150200220

Demo 1 grows the box ring by ring; Demo 2 starts from the 200px border box and takes the border and padding out of it. With content-box, two boxes of width: 50% and some padding no longer fit on one line. That is why many style sheets start with * { box-sizing: border-box }. Margin is never inside width.

Flexbox: basis, free space and flex-grow

A flex container (display: flex) lays its items along one main axis, here a row. The shorthand flex: 1 1 100px means flex-grow: 1, flex-shrink: 1, flex-basis: 100px. The browser resolves the widths like this:

  1. Every item starts at its flex-basis.
  2. Free space = container width − the sum of the bases.
  3. If it is positive, each item gets free space × its flex-grow ÷ the sum of all flex-grow. An item with flex-grow: 0 keeps its basis.

In Demo 3 the container is 600px and the items are A flex: 1 1 100px, B flex: 2 1 150px, C flex: 0 1 50px. Free space = 600 − 300 = 300. The grow factors add up to 3, so one unit is 100px: A gets 100, B gets 200 and C nothing. The widths are 200, 350 and 50. Note that B is not twice as wide as A: flex-grow shares the free space, not the whole width. For items that should end up equal, use flex: 1 1 0, so the whole width is free space.

flex-shrink: the overflow is weighted by basis

If the bases add up to more than the container, the free space is negative: the items overflow. Then they shrink, but not by flex-shrink alone. Each item's weight is flex-shrink × flex-basis, and each item gives back overflow × its weight ÷ the sum of the weights. A wide item gives back more than a narrow one with the same flex-shrink, so small items do not get squeezed to nothing first.

Top: three items at bases 100, 150 and 50 in a 600px container leave 300px free; split 1 to 2 to 0 it becomes plus 100, plus 200 and 0, giving 200, 350 and 50. Bottom: bases 200, 100 and 100 in a 300px container overflow by 100px; weights flex-shrink times basis are 200, 200 and 100, so the items lose 40, 40 and 20 and end at 160, 60 and 80.
Growing shares free space by flex-grow; shrinking shares the overflow by flex-shrink × flex-basis.

In Demo 4 the container is 300px and the bases are A 200, B 100, C 100, with flex-shrink 1, 2 and 1. The overflow is 400 − 300 = 100. The weights are 1 × 200 = 200, 2 × 100 = 200 and 1 × 100 = 100, together 500. So A and B each lose 100 × 200 ÷ 500 = 40 and C loses 20: the widths are 160, 60 and 80. B has twice A's flex-shrink and still loses only the same 40px, because A's basis is twice as big. Pick 480px with the width menu and press Lay Out: now there is free space, but every flex-grow is 0, so the items stay at their bases and 80px stay empty.

A real browser also stops an item from shrinking below its min-content size (min-width: auto): a long word or an image keeps the item wide. The items on this page are empty boxes, so that limit is 0 and never comes into play. When it does, the item is frozen at its minimum and the rest of the overflow is shared among the others.

Wrapping: each line on its own

With flex-wrap: wrap, the browser first breaks the items into flex lines: a line takes items while their bases fit. Then it resolves each line as above, with that line's own free space. In Demo 5 five cards of flex: 1 1 150px sit in 480px. Line 1 takes A, B and C (450px); D would make 600px, so D starts line 2. Line 1 has 30px free, 10px each, so A, B and C are 160px. Line 2 has 480 − 300 = 180px free, 90px each, so D and E are 240px. The last line's cards are wider and do not line up with the cards above.

Grid tracks: fixed, auto and fr

A grid container (display: grid) works the other way round. It sizes the tracks (columns and rows) first and then puts each item in a cell, so items in the same column always have the same width. For columns:

trackhow it is sized
120px (fixed)always 120px
autothe width of its widest content; if no fr track takes the leftover, auto tracks share it
1fr, 2fr (flexible)leftover = container − the other tracks − the gaps; 1fr = leftover ÷ the sum of fr

In Demo 6, grid-template-columns: 120px 1fr 2fr with column-gap: 30px in 600px: leftover = 600 − 120 − 2 × 30 = 420, the fr add up to 3, so 1fr = 140. The columns are 120, 140 and 280. In Demo 7, auto 1fr 1fr with 20px gaps: item A has width: 120px, so the auto column is 120, and the two fr columns get (600 − 120 − 40) ÷ 2 = 220 each. Unlike flex-grow, fr shares the space left after the other tracks, so 1fr 1fr columns are always equal.

Same cards, two layouts: when to use which

Demo 8 puts the same five cards in grid-template-columns: repeat(3, 1fr) at 480px. 1fr = 160, so all five cards are 160px. D and E sit in row 2 under A and B, and the third cell of row 2 stays empty. The columns line up; in Demo 5 they did not.

Left: flex-wrap in 480px, line 1 has A, B, C at 160px, line 2 has D and E at 240px, so the second line does not line up with the first. Right: grid with repeat(3, 1fr) in 480px, all five cards are 160px, D and E sit under A and B, and the last cell is empty.
Flex lines are sized one by one, grid columns once for all rows.
flexboxgrid
directionone axis at a time (a row or a column)two axes: rows and columns together
who decides the sizesthe items (basis, grow, shrink)the container (track list)
after wrappingeach line is sized alone; columns do not line upcells line up in every row
good fortoolbars, nav bars, a row of buttons, centring one thingcard galleries, forms with labels, page skeletons

The two mix well: a page skeleton in grid, and a toolbar inside one of its cells in flexbox.

See also How a Browser Renders a Page: the layout step there is this arithmetic, run for every box, and a change to a width or a flex property triggers a reflow. And see The CSS Cascade for how each of the properties used here gets its value.

What the page leaves out

The column direction (flex-direction: column, rows in grid), gap in flexbox, auto margins, justify-content and align-items (where leftover space goes and how items line up across the axis), order, min-width / max-width clamps beyond the zero floor, min-content and max-content sizes of real text (the items here are empty boxes, and an fr track is really minmax(auto, 1fr)), minmax(), auto-fill and auto-fit, named areas, line placement and spanning, implicit tracks, subgrid, and percentages and rounding to device pixels.

On the mind map

Open the Servers & Event Loops mind mapServers &Event LoopsI/O models: how a thread waits for dataI/O modelsWeb servers: one HTTP request, end to endWeb serversEvent loops: one thread, many connectionsEvent loopsIn the browser: the main thread draws the pageIn the browserScaling and releases: more servers, safe changesScaling and releasesBrowser event loopBrowser event looptask, all microtasks, then renderSSR vs CSRSSR vs CSRSSR paints first; hydration makes it liveDebounce vs throttleDebounce vs throttledebounce waits for quiet; throttle pacesBrowser renderingBrowser renderingDOM+CSSOM, layout, paint, compositeFlexbox vs Gridflex shares free space; grid sizes tracksCSS CascadeCSS Cascadeorigin, inline, specificity, then orderDOM event propagationDOM event propagationDown to target, then bubble upService worker cacheService worker cacheProxy in the browser: offline firstOpen the Servers & Event Loops mind mapServers & Event Loopsthe main thread draws the pageIn the browserBrowser event loopBrowser event looptask, all microtasks, then renderSSR vs CSRSSR vs CSRSSR paints first; hydration makes it liveDebounce vs throttleDebounce vs throttledebounce waits for quiet; throttle pacesBrowser renderingBrowser renderingDOM+CSSOM, layout, paint, compositeFlexbox vs Gridflex shares free space; grid sizes tracksCSS CascadeCSS Cascadeorigin, inline, specificity, then orderDOM event propagationDOM event propagationDown to target, then bubble upService worker cacheService worker cacheProxy in the browser: offline first
This page on the Servers & Event Loops mind map · 29 pages in 5 branches · open it to see the whole map and test yourself in recall mode.