Render Tree and Layout
Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.
What you will be able to do
#- Describe how the render tree is built from the DOM and CSSOM.
- Explain what triggers layout and how to limit invalidation.
- Use containment to reduce layout cost.
Overview
#The render tree is the input to layout. It contains only the boxes that will be painted.
Building the render tree
DOM nodes with display: none are skipped. Pseudo-elements such as ::before are included. Text nodes become text boxes. visibility: hidden still creates a box, so it participates in layout but paints nothing.
Layout
Layout computes the position and size of every box. Layout is expensive because a change can invalidate a subtree and its ancestors. Changing width, font size, or adding nodes triggers layout.
Avoiding forced synchronous layout
Reading geometry after a write forces the browser to run layout immediately. Batch reads before writes. Use transform and opacity for animation because they can skip layout and paint.
Containment
contain: layout tells the browser that a subtree's internal layout cannot affect the outside, which limits invalidation and speeds up large pages.
Questions to carry forward
- Which CSS properties are layout-triggering?
- How does contain: layout change invalidation?
- Why is transform cheaper than top and left for animation?
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Which of these elements creates a box in the render tree?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.html<div style="display:none">A</div> <div style="visibility:hidden">B</div> <div>C</div>Exercise 2Implement
Rewrite this animation trigger to avoid layout on every frame.
starter.javascriptel.style.left = x + 'px';Reveal the reference answer
reference.javascriptel.style.transform = 'translateX(' + x + 'px)';Authored expected output: A transform-based movement that can run on the compositor.
Changing left triggers layout. transform can be handled by the compositor without re-running layout or paint.
Common misconceptions
#- display: none elements still occupy a box in layout.
- visibility: hidden removes an element from layout.
- Changing transform always triggers layout.
Follow-up practice
#- browser-internals-paint-composite
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.