Q&A-4Walk me through the DOM, the CSSOM and the render tree, and tell me what ends up in the render tree that has no DOM node, and what has a DOM node but never reaches it.
Answer
The 90-second answer. Three trees, built in order, each answering a different question. The DOM is the parsed document: every element, text node and comment, including the ones that will never be drawn. The CSSOM is the parsed and cascade-resolved style data: rules from <style>, <link> and style attributes, matched against selectors and resolved to a computed style per element. The render tree is the join of the two, holding one box per thing that gets laid out and painted, each box carrying its computed style. Blink calls it the layout tree, WebKit calls it the render tree; in neither engine is it the DOM with styles stapled on. It is a filtered join, and the filter runs in both directions.
The filter is display. Computed display: none generates no box, and neither do the descendants, so the subtree stays in the DOM and is absent from the render tree. visibility: hidden is the other case: the box is generated, occupies space, and is not painted.
| DOM | Render tree |
|---|---|
<p>Hello</p> | block box + inline text box |
<p style="display:none"> and its subtree | nothing |
<p style="visibility:hidden"> | box, laid out, not painted |
.badge::before { content: "new" } | inline box, no DOM node behind it |
<head>, <script>, <style>, <link>, <template> | nothing |
| comment node | nothing |
<input type="range"> | UA shadow tree boxes (Chromium) |
<div style="display:contents"> | no box; children get boxes |
The rows above the line are the ones people forget. Pseudo-elements are the cleanest render-tree-only content: ::before and ::after create real boxes with real text, and no node exists for them anywhere in the document. Anonymous boxes are the second: a block container with mixed inline and block children gets an anonymous block box wrapping the inline run, because the box tree has to be well-formed even when the markup isn't. Third, form controls and <video controls> render from a user-agent shadow tree in Chromium: boxes whose nodes are not in the page's DOM at all.
const hidden = document.querySelector('.hidden'); // <p class="hidden">A</p>
hidden.offsetWidth; // 0 — no box was ever laid out
getComputedStyle(hidden).display; // "none" — the CSSOM resolved it anyway
getComputedStyle(hidden).width; // "auto" — computed value, not a used one
document.querySelector('.badge').textContent; // "C" — the ::before text is not here
That snippet is the practical consequence: style resolution and layout are separate stages, so a display: none element has a full computed style and no geometry. getComputedStyle answers from the CSSOM; offsetWidth, getBoundingClientRect and IntersectionObserver answer from layout and return zeros.
Failure modes. Measuring a hidden element to decide a layout, the classic tab-panel or tooltip measurement, returns 0 and the code silently positions at the origin; detect it by asserting non-zero geometry in a test, or measure after making the element visible but off-screen (position: absolute; visibility: hidden keeps the box). Generated content carrying meaning: ::before text is invisible to textContent, to querySelector and to most test queries, so a badge count or required-field marker built that way is untestable and fragile for assistive tech. Put real text in a real node. display: contents on a wrapper removes its box, which is what you want for a flex pass-through and is a trap when the element also carried a background, border or role. And content-visibility: auto skips rendering for off-screen subtrees, so their children report zero geometry until they come near the viewport.
If pushed on ordering: the render tree for a given piece of content can't be built until the CSSOM is complete, which is why a stylesheet in <head> is render-blocking, and why a script that reads layout forces style and layout to be flushed synchronously.
Curated: · Written: · Reviewed: