MyShop's reply has arrived. Inside is HTML text, just like what you wrote in the HTML lessons. But a screen can't show the text <h1> as it is. The browser has to draw the page first, and always in the same order.
Read, build, measure, paint
- Read. The browser reads the HTML from the top line down.
- Build. Every tag becomes a branch of a tree: a tag inside another tag becomes its child. For example,
<body>has the children<h1>,<img>and<button>. This tree is called the DOM. - Measure. Every tag that shows on the screen gets its own box. With the CSS rules (the rules for how the page looks), the browser calculates how wide every box is and where it sits on the screen (layout).
- Paint. Only then does the browser paint the pixels (colour the tiny dots on the screen), and the page appears.
What changes, what runs again
When a button gets a new colour, its size stays the same, so painting it again is enough. When a heading gets bigger, the boxes under it move too, so the browser measures again first, then paints again. Measuring again is more work for the browser than painting again, especially on a cheap phone.
TL;DR: The browser reads the HTML, builds its tags into a tree (the DOM), measures their boxes, then paints the screen. When only a colour changes, the browser just paints again. When a size changes, the browser has to measure again first.
⚡ Four steps, always in order
Guess first: to show a page, the browser always works in this order: read, build the tree, measure, paint. The DOM tree of MyShop's page is built. What does the browser do next?
It measures. The browser calculates how wide the box of every tag is and where it sits on the screen. Only after that is the screen painted.
Meaning: The browser always reads the HTML from the top, builds every tag into a branch of a tree (the DOM), then measures the box of every tag (layout). Last, the browser paints the pixels, the tiny dots on the screen.
⚡ A new colour or a new size
Guess first: a button gets a new colour, then a heading is made bigger. Which change is more work for the browser?
The bigger heading. The boxes under the heading move too, so the browser measures again first, then paints again. When only a colour changes, the browser just paints again.
Meaning: Measuring again is more work than painting again, especially on a cheap phone.