HTML Parsing Pipeline WHATWG HTML

HTML bytes network Tokenizer state machine Tokens tag, text, etc Tree Builder insertion mode DOM Tree document
Token TypeDescriptionExample
DOCTYPEDocument type declaration<!DOCTYPE html>
StartTagOpening element tag<div class="x">
EndTagClosing element tag</div>
CharacterText contentHello World
CommentHTML comment<!-- note -->
EOFEnd of file(implicit)

DOM Tree Construction Document Object Model

Document <html> <head> <body> <title> <div> <p>
Node TypeInterfacenodeType
ElementElement1
AttributeAttr2
TextText3
CommentComment8
DocumentDocument9
DocumentTypeDocumentType10

CSS Parsing CSSOM

CSS bytes stylesheet Tokenizer lexical Parser syntax Style Rules selector + decl CSSOM stylesheets
CSS TokenDescriptionExample
identIdentifiermargin
hashID selector#header
dimensionNumber with unit16px
stringQuoted string"Arial"
functionFunction callrgb(
delimDelimiter. *

JavaScript Execution script element

normal HTML parse JS HTML parse async HTML parse fetch JS HTML parse defer HTML parse (unblocked) fetch JS DOMContentLoaded
AttributeFetchExecuteOrder
(none)BlockingImmediatelyDocument order
asyncParallelWhen readyFetch completion
deferParallelAfter parseDocument order
type="module"ParallelAfter parseDependency order

Render Tree DOM + CSSOM

DOM CSSOM Render Tree Layout
ElementIn Render Tree?Reason
<html>YesRoot element
<head>Nodisplay: none
<script>Nodisplay: none
<div hidden>Nohidden attribute
display: noneNoCSS display value
visibility: hiddenYesStill occupies space

Critical Rendering Path performance

HTML DOM CSS CSSOM Render Layout Paint render-blocking
ResourceBlocking TypeOptimization
CSS <link>Render-blockingInline critical CSS
JS <script>Parser-blockingdefer or async
FontsRender-blockingfont-display: swap
ImagesNon-blockingloading="lazy"

Document Events DOMContentLoaded vs load

navigate HTML parsing DOMContentLoaded images, iframes, etc load idle
EventFires WhenUse Case
DOMContentLoadedHTML parsed, defer scripts doneDOM manipulation
loadAll resources loadedImage dimensions, full page
beforeunloadUser navigating awayUnsaved changes warning
unloadPage being unloadedCleanup (deprecated)
document.readyState
├── "loading"      → document still loading
├── "interactive"  → DOM ready (DOMContentLoaded)
└── "complete"     → all resources loaded (load)