DOM & CSSOM Construction
The HTML parser incrementally builds the DOM tree while the CSS parser builds the CSSOM tree. Because JavaScript can query styles via getComputedStyle(), any synchronous <script> pauses DOM construction until every preceding stylesheet finishes building the CSSOM.
Parser Blocking: HTML + CSSOM + Sync Script
Step through how a synchronous script stalls DOM construction while waiting on CSSOM.
The HTML tokenizer converts UTF-8 bytes into StartTag and EndTag tokens incrementally.
<link rel="stylesheet" href="/app.css">- domNodes
- 4
- cssomReady
- false
- parserState
- running
Under the Hood
Like a dual-track railway converging at a single switchyard, HTML and CSS arrive as raw byte streams that must become structured trees before layout begins.
- Tokenization: The engine decodes bytes into characters, then emits tokens (
StartTag,Text,EndTagfor HTML; selectors and declarations for CSS). - Tree Construction: HTML tokens append
Elementnodes to the DOM incrementally; CSS rules form a cascade-indexed CSSOM tree. - The Serialization Trap: When the HTML parser hits a non-
async, non-defer<script>, it halts DOM construction until CSSOM completes, preventing scripts from reading stale styles.
<head>
<!-- Render-blocking: CSSOM must finish before first paint -->
<link rel="stylesheet" href="/styles.css" />
<!-- Parser-blocking: waits for CSSOM, then halts DOM -->
<script src="/analytics.js"></script>
<!-- Non-blocking: downloads in parallel, runs after DOM -->
<script defer src="/app.js"></script>
</head>Use Preload Scanner Hints to fetch subresources early while the main Browser Rendering Pipeline waits on CSSOM.
Problem Genealogy
- 1995–2008: Blocking
<script>tags at the top of<head>froze page rendering until every script downloaded and executed. - 2009 (HTML5):
asyncanddeferattributes decoupled script downloading from HTML tokenization. - 2015+: HTTP/2 multiplexing and
preloadhints reduced round-trip stalls, yet render-blocking CSSOM remains mandatory to avoid unstyled flashes.
Production Pitfalls
- CSS-before-script deadlock: Placing a small inline
<script>after a slow external<link rel="stylesheet">turns a render-blocking stylesheet into a full parser blocker. - CSSOM
@importchains: Nesting@importinside stylesheets hides secondary CSS files from the preload scanner, adding serial network round-trips.
- When not to use synchronous scripts: Never place synchronous
<script>tags in<head>unless the script mutates critical above-the-fold state before first paint (such as theme class hydration).
Connections & Logical Next Steps
Style calculation requires parsed DOM and CSSOM trees before constructing the render tree.
Render Tree construction cannot begin until DOM nodes and CSSOM rules exist.
Runtime insertRule() calls invalidate the browser CSSOM and force style recalculation across the DOM.
Preload scanner discovers blocked CSS and JS assets before the main parser stalls.
DOM and CSSOM trees are the two mandatory inputs to Render Tree construction.