Basis Lab

How a Browser Draws a Page

Width Flows Downwards and Height Flows Back Up, Which Is Why the Browser Has to Visit Every Box Twice and Why One Careless Property Makes It Visit Them Forever

Last timeWhat Holds Up the First Paint

Layout computed by hand on a small tree, the two directions information travels, and the structural reason a parent cannot know its own height until its children are finished.

What a box is made of

Layout takes the tree, with every element now carrying a complete set of values, and produces a number for the position and size of every box. It is arithmetic, and on a small tree it can be done by hand in a couple of minutes.

Each box is four nested rectangles. The content at the centre, then padding around it, then a border, then a margin outside that. Each has a value per side.

FIG 1One box built outward from its content, in pixels
stepthe rectanglewhat it adds on each sidethe width across itwhat that width decideswhat happened
1contentnothing, this is the middle878where the text is laid out, so this is the width the line breaking usesThese are the numbers of the article box in the tree below, the one its parent hands 928 pixels to.
2padding24878 + 48 = 926the background and the border are drawn out to here, which is why padding is coloured and margin never isTwenty four on each side is forty eight across, and every one of these four values can differ per side.
3border1926 + 2 = 928the edge the reader can see, and the last rectangle the box paintsA single pixel per side costs two across, which is enough to break a layout built on round numbers.
4margin0 on this box, 20 on the body box above it928 + 0 = 928the space the box occupies in its parent, which here is exactly what the parent had availableThe outermost rectangle is the only one that belongs to the parent's arithmetic rather than to the box's own appearance.
5the same box with a width of 878 declaredthe same 24 and 1 either way928 across under the original rule, 878 across under the widely used settingone declaration, two boxes: content 878 or content 878 - 50 = 828The only real trap on this page. The original rule reads a declared width as the content, the widely used setting reads it as content plus padding plus border.
5 steps
Read the third column down and the box is assembled one rectangle at a time, each adding twice its per-side value. The arithmetic is trivial; the last row is what actually costs people an afternoon, because the same declaration of 878 gives an outer width of 928 under one rule and 878 under the other, and the box the reader sees differs by the 50 pixels of padding and border either way.

Widths decided on the way down

Now the structural fact that organises everything. The two dimensions behave completely differently.

A block-level box with no declared width takes whatever its parent has available, minus its own margins, borders and padding. The parent knows that number already, because the parent got its own width from its parent, and so on up to the window.

So widths can be assigned in a single sweep from the root downwards, before any child has been examined at all.

FIG 2Widths assigned down a four-level tree, by hand
plaintext
window is 1000 wide

body          margin 20 each side
              content width = 1000 - 40 = 960

  main        padding 16 each side
              available 960
              content width = 960 - 32 = 928

    article   border 1 each side, padding 24 each side
              available 928
              content width = 928 - 2 - 48 = 878

      p       no margin, padding or border
              available 878
              content width = 878

every number above was computed without looking at
any child, working strictly downwards
Four levels and four subtractions. Note that the paragraph knows its width before anything is known about the text inside it, which is the precondition for everything in the next section, because you cannot know how many lines text occupies until you know how wide the lines are.

Heights discovered on the way up

Height does not work like that at all. A box with no declared height is exactly as tall as its contents, and its contents are its children, whose heights are as tall as their contents.

For text, the height is the number of lines times the line height, and the number of lines depends on how many words fit on a line, which depends on the width that was just assigned on the way down.

So the only order that works is: assign widths downwards, reach the leaves, measure them, and sum heights back upwards.

The lesson stops here

1 more paragraph to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01The Browser Starts Building the Page Before It Has Finished Reading It, and One Tag in the Wrong Place Stops the Whole Production Line
  2. 02Every Element Has a Value for Every Property Whether You Wrote One or Not, and the Browser Will Not Place a Single Box Until It Knows Them Allopening only
  3. 03Four Things Stand Between a Document Arriving and Anything Appearing, and Only One of Them Is Usually the Reason Your Page Is Slowopening only
  4. 04Width Flows Downwards and Height Flows Back Up, Which Is Why the Browser Has to Visit Every Box Twice and Why One Careless Property Makes It Visit Them Foreveryou are here
  5. 05The Boxes Are Placed and Nothing Is Visible Yet, Because Drawing Them Is a Separate Job With Its Own Rules and Its Own Surprising Billopening only
  6. 06Everything So Far Described Drawing the Page Once, and the Page Is Drawn Again Sixty Times a Second, Which Changes Which Mistakes Matteropening only
  7. 07You Reached for a Link and the Page Moved and You Pressed Something Else, and the Reason Is That the Browser Was Never Told How Big a Picture Wasopening only
  8. 08Parsing, Styling, Layout, Painting, Your Code and Every Press of a Button Take Turns on a Single Queue, and Whoever Is Holding It Is Holding Everythingopening only

Read alongside