Overview
Curated: · Written: · Reviewed:
DOM and Browser Rendering
In one line
The browser turns HTML into pixels through parse, style, layout, paint and composite. A DOM or CSS change re-enters at the earliest stage it touches, so name that stage when a page paints late, stutters or responds slowly.
Key definitions
- Render-blocking stylesheet: a
<link rel=stylesheet>whose media query matches the viewport. Paint waits for its CSSOM, and it also holds back any classic script after it. - Parser-blocking script: a classic
<script>with neitherdefernorasync. The parser stops until it is fetched and executed. - Render tree: the boxes that get laid out and painted. It omits
head,scriptanddisplay: nonesubtrees and includes::beforeand::afterboxes. - Forced synchronous layout: a geometry read after a style write, which flushes pending style and layout inside the current task.
- Event phases: capture runs from
windowdown to the target, the target is where the event fires, and bubble runs back up. - Accessibility tree: built from the DOM, filtered and augmented by ARIA. Assistive technology reads this tree, not the markup.
Formulas and complexity
Hypothetical: a 1000 by 1000 CSS px layer at DPR 2 is 2000 by 2000 device px, about 16 MB. Ten such layers hold about 160 MB.
How it works
- A classic script halts the parser. The preload scanner keeps fetching images, stylesheets and later scripts ahead of it.
- Before first paint, every matching stylesheet must produce a CSSOM.
- Style, layout, paint and composite run in that order. A change re-enters at the earliest stage it touches.
transformandopacityon a composited layer skip style, layout and paint, so only composite runs.
When to use and when not to
| Technique | Use when | Cost |
|---|---|---|
Animate transform or opacity | Motion on an element that is already composited, or easily promoted | One GPU layer per promoted element |
Animate top, left, width | Never for motion | Layout and paint on every frame |
defer | Script needs the DOM and execution order matters | Runs after parse, before DOMContentLoaded |
async | Script is independent (analytics, polyfill loader) | Execution order is not guaranteed |
content-visibility: auto | Long sections far below the fold | Skipped sections have no size until contain-intrinsic-size gives one, so page height shifts as the reader scrolls |
will-change | Measured jank on a known layer | Memory held whether or not the animation runs |
| Inline critical CSS | First screen needs only a small stylesheet | Repeated in every HTML document and not cached separately |
Traps interviewers probe
- Stylesheets never block rendering. Matching ones do, and they also delay classic scripts after them. Fix: scope media queries correctly and keep the critical stylesheet small.
visibility: hiddenremoves an element. It keeps its space and stays in the accessibility tree and the tab order. Fix: usedisplay: noneorinertwhen the element must leave both.- Reading
offsetHeightis free. It flushes pending layout. Fix: read all values first, write all values second, or schedule writes inrequestAnimationFrame. - Delegation catches every event.
focus,blur,mouseenterandmouseleavedo not bubble. Fix: usefocusin,focusout,mouseoverormouseout, or listen in the capture phase. preventDefault()in a document-level touch or wheel listener works. Chrome treats those listeners as passive by default, so the call is ignored and a console warning is logged. Fix: pass{ passive: false }explicitly, or attach the listener to the element that needs it.
Interview tips
Open by naming the stage a change forces: "This writes top every frame, so each frame reruns layout and paint. transform skips both." Then give the cost and the trace that shows it.
Expect these follow-ups:
- Why
deferoverasyncfor an app bundle that touches the DOM? - Why does a stylesheet above a script delay that script?
- How do you confirm forced layout in a trace? Look for
Layoutblocks inside long tasks, change the property, and record again. - How do you attribute a 480 ms INP? INP above 200 ms needs work and above 500 ms is poor. Find the long task in the event timing entries, then check which handler and which forced layout sit inside it.
30-second outline: "Parsing stops at a classic script, and a matching stylesheet must reach the CSSOM before first paint. Changes re-enter at the earliest stage they touch. Animating transform on a composited layer skips layout and paint, but each layer costs GPU memory. I would confirm the stage in a Performance trace before changing anything."
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.