Paint and Compositing
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
#- Explain how layers and compositing work.
- Choose compositor-friendly animation properties.
- Weigh the cost of promoting too many layers.
Overview
#After layout, the browser records what to draw and then composites layers on the GPU.
Paint
Paint walks the render tree and records draw commands such as rectangles, text, and images. Painting is split into layers. A new layer can be created by transform: translateZ(0), will-change: transform, position: fixed, or a video element.
Compositing
Compositing combines layers on the GPU. Moving or fading a layer with transform and opacity can be done on the compositor thread without re-running layout or paint, which is why those properties animate smoothly.
Costs of too many layers
Each layer uses GPU memory and can add compositing overhead. Promoting everything with will-change can make memory usage worse and slow things down. Promote deliberately and measure.
Stacking contexts
transform, opacity below 1, filter, and positioned elements with a z-index create stacking contexts. They change paint order and can force extra layers.
Questions to carry forward
- Which properties can be animated on the compositor?
- How do you inspect layers in DevTools?
- When does a stacking context force a new layer?
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 1Explain
Explain why animating transform and opacity is usually smoother than animating width and height.
Reveal the reference answer
reference.texttransform and opacity can be composited on the GPU without layout or paint, so they run on the compositor thread. width and height change geometry, forcing layout and paint on the main thread each frame.Authored expected output: An answer naming layout and paint avoidance for transform and opacity.
The compositor can move or fade an existing layer; it cannot recompute geometry, so layout properties must go through the main thread.
Exercise 2Predict
Which of these creates a new compositing layer?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.css.a { will-change: transform; } .b { color: red; } .c { opacity: 0.5; }
Common misconceptions
#- will-change is free and should be applied broadly.
- Compositing always requires re-running layout.
- opacity: 1 creates a stacking context.
Follow-up practice
#- browser-internals-critical-rendering-path
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.