ContentsThe library

How a Browser Draws a Page

You 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 Was

Last timeDoing It All Again

Content that arrives after the page is already drawn changes geometry, geometry means layout, and layout means everything already placed moves. The causes are a short list and so are the fixes.

What a shift actually is

A reader is part way down an article. A picture finishes loading above where

they are looking, the text moves down by two hundred pixels, and the sentence

they were reading is somewhere else.

Nothing unusual happened. Three lessons of machinery just worked correctly.

The picture had no declared size, so at layout time the browser reserved no

space for it, because it had no number to reserve. When the bytes arrived the

intrinsic size became known, which is a geometry change, which is the first

class from the previous lesson. Layout ran again. Everything after the picture

in the flow moved down by its height, and the new positions were painted.

FIG 1One page, four arrivals
stepmomentwhat arrivedwhat movedhow farwhat happened
10.4 sthe markup and the stylesheetnothing, this is the first paint0The baseline. Everything placed here was placed using only information the browser already had.
20.9 sthe web fontevery line of text, slightly4The fallback face was narrower, so lines rewrapped and the whole column settled by about four pixels. Small, and it affects almost the entire viewport.
31.6 sthe hero image, no declared sizeeverything below it240The large one. A single element gaining a height of 240 pushes the entire rest of the document down by exactly that much.
42.3 sa banner inserted at the topeverything, again72The most annoying kind, because it happens after the reader has started interacting and is almost always inserted by code that could have reserved the space.
4 steps
Three of the four arrivals moved content, and the one that moved it furthest was the one with no declared size. Note the second row especially: a four pixel settling affecting the whole screen scores comparably to a large move of a small element, which is the reasoning behind the measurement in the next section.

The short list of causes

Nearly every shift in practice has one of five causes.

FIG 2Causes, and what the browser was missing
the browser lacked a sizthe size was knowable inthe fix is a declared sihas a standard fix
an image or video with n1001
content inserted above e1001
a font that swaps to dif1101
an element sized from da1011
an animation of width, h0011
The first column is almost entirely ones, which is the finding of this lesson: a shift is a missing number, not a mysterious behaviour. The last row is the exception and the marked one, because there the size was known and geometry was animated anyway, so the fix is to animate a transform instead.

The font case deserves a note, because it is the one people find unfair. The

fallback face and the web face have different character widths, so the same

sentence occupies a different number of lines, so every block below it moves.

The fix is not to avoid web fonts; it is to choose a fallback with similar

metrics and to adjust its size and spacing so the two occupy the same space.

The lesson stops here

4 more paragraphs 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 Foreveropening only
  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 Wasyou are here
  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