ContentsThe library

How a Browser Draws a Page

The Boxes Are Placed and Nothing Is Visible Yet, Because Drawing Them Is a Separate Job With Its Own Rules and Its Own Surprising Bill

Last timeDeciding Where Everything Goes

How placed boxes become coloured pixels, the order that decides what covers what, the small list of properties that put part of the page on its own sheet, and what that sheet costs.

From boxes to pixels

Every box now has a position and a size, and the screen is still blank.

Painting is a separate job: deciding, for each pixel, what colour it is.

The browser does it in two stages, and the split matters. First it records what

to draw as a list of commands. Fill this rectangle with this colour. Stroke

this line. Draw this text at this baseline with this font. Then it executes the

list. Recording consults the box tree and the computed styles; executing does

not, which is why the same recorded list can be replayed at a different scale

or on a different thread without going anywhere near the tree.

Within one box the drawing happens in a fixed sequence.

FIG 1One box, painted
stepstepwhat is drawncovers what came beforecostwhat happened
11background colournothing yetone rectangle fillThe cheapest possible operation, and the reason a flat colour is almost free compared with anything else on this list.
22background imagethe coloura scaled image drawScaling is the expensive part. An image drawn at other than its natural size is resampled, and a large image behind a small box pays for every source pixel.
33borderthe backgrounds at the edgesfour edges, more if roundedA rounded corner turns a straight edge into a curve that has to be smoothed, which is measurably more work than a square one.
44the text and inline contentseverything aboveone draw per run of textUsually the dominant cost on a text-heavy page, and the reason a paint is roughly proportional to how much text is on screen.
55anything positioned, in ordereverything abovea repeat of this whole listChildren with a position of their own are painted after the entire normal content of this box, which is the root of the next section.
5 steps
Five steps, each one covering what the earlier ones drew. The last line is the important one: positioned descendants come after everything else in the box, regardless of where they appear in the markup, and that single rule accounts for most of the confusion about what covers what.

What covers what

People reach for an explicit layering number and are surprised when it does

nothing. The reason is that the painting order has several levels and that

number only operates within one of them.

The order, simplified: the background of the root element, then everything with

no position of its own in document order, then floated boxes, then the inline

content, then positioned boxes sorted by their layering number, with document

order as the tie-break.

FIG 2Two overlapping elements, and which one wins
first element positionedsecond element positionea layering number is setwhich one appears on top
both unpositioned, secon1002
one positioned, one not0102
both positioned, second 1111
both positioned, same nu1102
one has a lower number b0011
one is transformed, the 1112
Read the last column. The two marked rows are the ones that catch people: a low number inside a positioned ancestor can sit above a high number outside it, because the number only sorts within its own context, and a transform creates a context of its own, so an enormous number elsewhere cannot climb past it.

That last behaviour is the bridge to the rest of the lesson. A handful of

properties do not merely affect painting, they create a self-contained region

whose contents are sorted among themselves and then placed as a unit. And some

of those properties go further and move the region onto a surface of its own.

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 Billyou are here
  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