How browsers draw a page
5 minEvery page you open goes through the same four steps before you see it. Learn them, and when a screen comes out wrong you'll know where to look first.
Lesson video
Tastebud · 4:54 · Captions on screen
A browser builds a tree from the HTML, styles it, lays it out, then paints it.
- Why. Each step feeds the next, so a fault often shows up later than where it started. Knowing the four steps tells you where to look first.
- Real example. Press hide next to Contents. The sidebar leaves and the browser runs layout again, so the article moves into the space. The article's text never changed. (Wikipedia, Nov 2026)
- When to break it. Some changes skip steps. Moving an element with transform or fading it with opacity can skip layout and paint, which is why smooth animations use those two.
- Common mistake. Hiding an element with visibility: hidden and wondering why the gap stays. The browser skipped painting it but kept it in layout, so its space remains.
Our take. Find the broken step before changing any code. A gap that won't close is a layout bug. A div dressed as a button is a tree bug, and the fix is a button element, with no extra CSS or ARIA.
The HTML becomes a tree first
The browser reads your HTML from top to bottom and builds the DOM, the page as a tree it keeps in memory. Each tag becomes a node, and the tags inside it become its children. CSS and JavaScript both work on this tree.
Tags inside tags become branches of one tree
- button: Toggle. A real button element, so a keyboard can reach and press it.
- nav: Contents. The sidebar. Its children are the heading and the two links.
- h2: Contents.
- a: History.
- a: Geography.
- main: the article. The article column. Hiding the nav changes how much room it gets.
- h1: Bengaluru.
- p: the first line.
Layout places the boxes, paint fills them in
Hiding something can stop at either step. display: none takes the element out of layout, so the page closes up around it. visibility: hidden keeps it in layout and skips painting it, so an empty space stays behind.
- Still in the layout
- Not painted
- Its space stays, empty
- Taken out of the layout
- Not painted
- The page closes up around it
Both hide the element. Only one gives its space back.
Name the step before you fix the bug
Chrome's DevTools has a view for each step. The Elements panel shows the tree, the Styles tab shows which rule won and the box model, and Paint flashing in the Rendering tab turns repainted areas green. The picture files six bugs by step.
Each bug lives in one step, and DevTools has a view for it
- Tree: the Elements panel. Shows the DOM, the tree the browser built from your HTML.
- An a tag never closed. Everything after the open tag sits inside the link, so a paragraph turns into one.
- A div as the toggle. The Tab key skips it because the tree has no button in it. A real button element fixes it, and no styling can.
- Style: the Styles tab. Lists every rule that applies to the element, including the ones that lost.
- Link colour overridden. Another rule matched the same element and won, so yours lost at the style step.
- Layout: the box model. The box model in the Styles tab shows each box's size and spacing.
- A gap where a banner was. The hidden banner still takes up space in layout. display: none gives the space back.
- A long word pokes out. The card is too narrow for the word, which is a size and position problem.
- Paint: Paint flashing. In the Rendering tab, it turns repainted areas green.
- A blurred shadow flashes green. The browser redraws it on every scroll, which is paint work, and scrolling stutters.
Try it
Hide the Contents sidebar properly
This is a small copy of a Wikipedia article page, recreated from Wikipedia, Nov 2026. Press the toggle in the preview: the sidebar disappears but its space stays. Fix the CSS so the article takes the room.
Tab adds two spaces. Press Esc, then Tab, to leave the editor.
- The hidden sidebar uses display: none (not yet)
- visibility: hidden is gone (not yet)
What to notice: Press the toggle before and after your fix. With visibility: hidden the grey article stays put next to an empty space, because the sidebar was only skipped at paint. With display: none the sidebar leaves layout and the article widens.
Quick check
A teammate says: the promo banner is hidden now, but there's a blank strip where it used to be. What's the likely cause?
Watch next
Watch it for How a browser goes from HTML to the DOM tree, then to layout and painting, the same steps this lesson covers.
Watch it for The difference between layout, which places the boxes, and paint, which fills in their pixels.
Connects to: 1.1 What a design engineer does · 3.1 The box model · 10.4 Smooth animation · 18.1 What makes a site feel slow
Sources · checked Nov 2026
- MDN: Populating the page: how browsers work (updated 16 Nov 2026)The browser parses HTML into the DOM, then runs style, layout and paint; display: none nodes leave the render tree while visibility: hidden nodes stay because they take up space; paint draws text, colours, borders, shadows and images
- web.dev: Render-tree construction, layout, and paintvisibility: hidden still occupies space in layout, display: none removes the element from layout; layout computes the exact position and size of each object
- MDN: Introduction to the DOMThe DOM represents the document as a logical tree in memory, which scripts can read and change
- web.dev: Stick to compositor-only properties and manage layer counttransform and opacity are the two properties that can avoid layout and paint (the breakIt line)
- Chrome for Developers: Get started viewing and changing CSSThe Elements panel shows the DOM Tree, the Styles tab lists the rules applied to the selected element, and the Box Model diagram sits in the Styles tab
- Chrome for Developers: Analyze rendering performance with the Rendering toolPaint flashing flashes repainted areas green; Layout Shift Regions highlights shifts in purple
- Wikipedia: Bengaluru (article page, Vector 2022 skin)The Contents sidebar with its hide and move to sidebar buttons, the Toggle the table of contents button, and the History and Geography sections
- Chrome for Developers: CSS features referenceThe Styles tab shows every rule that applies to an element, including overridden declarations, and the box model view