Critical Rendering Path: Understanding Browser Rendering
The critical rendering path is the browser pipeline that turns HTML, CSS, and JavaScript into pixels on the screen. For interview purposes, the best conceptual sequence is DOM + CSSOM β Render Tree β Layout β Paint β Composite, with the nuance that HTML parsing starts immediately and CSS parsing begins as stylesheets are discovered during parsing.
Quick Navigation: The Correct Answer β’ Step 1: HTML Parsing and DOM Construction β’ Step 2: CSS Parsing and CSSOM Construction β’ Step 3: Render Tree Creation β’ Step 4: Layout β’ Step 5: Paint β’ Step 6: Composite β’ How the Given HTML Flows
The Correct Answer
Best Answer for Interviews
B) HTML parsing β DOM β CSSOM β Render Tree β Layout β Paint β Composite
This is the best high-level answer among the choices.
Important Nuance
The real browser pipeline is more incremental than the option suggests:
<link> or <style> is discoveredSo B is the best conceptual choice, even though real rendering is not a single rigid linear pipeline.
Step 1: HTML Parsing and DOM Construction
HTML Parsing β DOM
When the browser receives HTML:
1. Bytes are decoded into characters
2. Characters are tokenized
3. Tokens are parsed into nodes
4. Nodes are attached to the DOM tree
<html>
<body>
<h1>Hello</h1>
<p>World</p>
</body>
</html>This produces a DOM structure representing the document.
Key Point
HTML parsing is incremental. The browser does not wait for the full document before starting to parse.
JavaScript Interaction
A normal script like this can pause parsing:
<script src="app.js"></script>The parser stops, the script is fetched/executed, and only then does HTML parsing continue.
That is why defer is so important for non-critical scripts.
Step 2: CSS Parsing and CSSOM Construction
CSS Parsing β CSSOM
When the browser discovers CSS:
<link rel="stylesheet" href="styles.css">it fetches and parses the stylesheet into the CSSOM (CSS Object Model).
body { font-size: 16px; }
h1 { color: tomato; }
p { margin-top: 8px; }The CSSOM contains style rules after resolving:
Why CSS Matters for Rendering
The browser generally needs CSS before first render because it must know how visible DOM nodes should be styled.
Examples:
display: none removes an element from layout and render treeSo although HTML parsing may continue while CSS is loading, CSS is effectively render-blocking for first paint by default.
Step 3: Render Tree Creation
DOM + CSSOM β Render Tree
The browser combines DOM structure with computed style information to build the Render Tree.
The render tree includes only nodes relevant to visual rendering.
Included
Excluded
<head><script> elementsdisplay: noneImportant Distinction
visibility: hidden is different from display: none:
display: none β not in layout, not in render tree output for paintingvisibility: hidden β still participates in layout, but is not visibly paintedWhy Render Tree Comes Before Layout
The browser must know what boxes exist before it can calculate where they go and how big they are.
Step 4: Layout
Layout (Reflow)
Once the render tree exists, the browser performs layout.
Layout computes geometry for render tree nodes:
This is where the browser figures out the exact placement of content on the page.
Why Layout Can Be Expensive
Layout work can grow with DOM size and complexity. It may be triggered by:
// Can force layout work in the wrong pattern
el.style.width = '200px';
const h = el.offsetHeight;This kind of read-after-write pattern is a common performance problem.
Step 5: Paint
Paint
After layout, the browser paints visual parts of each box:
Paint is about turning layout results into drawing instructions or pixels.
Important Distinction
Layout answers:
Paint answers:
A change like color may trigger paint without requiring full layout.
A change like width often requires layout first, then paint.
Step 6: Composite
Composite
Modern browsers often split content into layers. During compositing, those layers are combined into the final frame shown on screen.
Properties that often help isolate work to compositing include:
transformopacityThese are often cheaper to animate because they can avoid layout and sometimes avoid repainting large areas.
But Be Careful
Creating too many layers also has memory and management costs. Compositing is powerful, not free.
How the Given HTML Flows
Applying the Pipeline to the Example
Given:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello World</h1>
<p>Content here</p>
<script src="app.js"></script>
</body>
</html>A simplified timeline looks like this:
1. Browser starts parsing HTML
2. <link rel="stylesheet"> is discovered
3. CSS request begins
4. DOM continues building as HTML is parsed
5. Browser reaches <script src="app.js">
6. HTML parsing pauses while script is fetched/executed unless deferred/async
7. Once relevant DOM and CSSOM information are ready, browser builds the render tree
8. Layout runs
9. Paint runs
10. Layers are composited to the screen
Interview Takeaway
The best mental model is not βeverything finishes, then next step starts forever.β
It is:
Common Misconceptions
Misconception 1: CSSOM always finishes before DOM
Not necessarily.
DOM construction often starts first because HTML parsing starts immediately. CSSOM starts when CSS is discovered. They may overlap in time.
Misconception 2: Layout happens before Render Tree
Wrong.
The browser must know which renderable boxes exist before it can compute their geometry.
Misconception 3: Paint and Composite are the same
They are related but different:
Misconception 4: CSS blocks HTML parsing
Usually not directly.
CSS blocks rendering, not raw HTML tokenization itself. However, interaction with scripts can make the overall pipeline feel blocked because scripts may need up-to-date style information.
Misconception 5: The DOM is the same as what gets painted
Also wrong.
The render tree is derived from DOM + style information and excludes non-rendered nodes.
Performance Implications
How to Speed Up the Critical Rendering Path
1. Reduce render-blocking CSS
2. Avoid parser-blocking JavaScript
Use defer for scripts that do not need to run immediately:
<script defer src="app.js"></script>defer lets HTML parsing continue and runs the script after parsing, before DOMContentLoaded.
3. Reduce layout work
4. Prefer compositor-friendly animations
Animate transform and opacity when possible.
Real Interview Framing
When interviewers ask about rendering performance, they often want you to connect optimizations to pipeline stages:
Follow-up Interview Questions
Q: Why is B correct if DOM and CSSOM can happen in parallel?
Because B is the best conceptual ordering among the choices. DOM construction begins first, CSSOM is built when CSS is discovered, and the render tree depends on both.
Q: What does a normal `<script>` do during parsing?
It pauses HTML parsing at that point, fetches and executes the script, then resumes parsing.
Q: What changes trigger layout?
Examples include:
Q: What is the difference between `display: none` and `visibility: hidden`?
display: none removes the element from layout flowvisibility: hidden keeps layout space but hides the visual output