# Render Tree Compilation

> How the browser merges the DOM and CSSOM to create the final Render Tree.

Subject: Internet | Lesson 27 of 35 | 5 min read
Interactive lesson: https://codewithanimation.in/internet/render-tree-compilation
Previous: [CSSOM Construction](https://codewithanimation.in/internet/cssom-construction.md) | Next: [The Layout Phase (Reflow)](https://codewithanimation.in/internet/layout-phase.md)

## What it is

The Render Tree is the union of the DOM and the CSSOM. It contains only the nodes that are required to render the page, along with their computed styles.

Any node that is hidden (like `<head>`, `<meta>`, or `display: none`) is omitted from the Render Tree completely.

## Why it matters

The Render Tree determines exactly what will be drawn to the screen. Understanding what goes into it (and what doesn't) is key to writing performant CSS.

If you change a style in JavaScript that affects the Render Tree structure (like toggling `display: none`), you force the browser to re-compile the tree, which triggers expensive Layout calculations.

## How it works

The browser starts at the root of the DOM tree and traverses each visible node.

For each visible node, it finds the matching CSSOM rules and applies them to compute the final styles.

Nodes like `script`, `meta`, and `link` are skipped. Nodes styled with `display: none` are also skipped, along with all of their children.

The resulting Render Tree is a new data structure containing visible nodes and their computed styles, ready for the Layout phase.

## Common mistake: Display None vs Visibility Hidden

A classic interview question and beginner trap is the difference between `display: none` and `visibility: hidden`.

If an element has `display: none`, it is *completely removed* from the Render Tree. The browser pretends it doesn't exist, and it takes up zero space.

If an element has `visibility: hidden`, it is still included in the Render Tree! The browser calculates its layout and reserves its physical space on the screen, it just paints it entirely transparent.

## Tip: Pro tip

Because `display: none` removes an element from the Render Tree, animating from `display: none` to `display: block` using pure CSS transitions is notoriously difficult.

The browser can't transition an element that wasn't in the Render Tree in the previous frame. This is why you often have to use JavaScript or newer CSS features like `@starting-style` to animate entry states.

## Try it: Try it

Drag the toggle to see how `display: none` vs `visibility: hidden` affects the Render Tree compilation.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/render-tree-compilation

## Check yourself

### Question 1

Which of the following elements will NEVER be included in the Render Tree?

- A. An empty <div> with no text.
- B. A <title> tag inside the <head>.
- C. An element with opacity: 0.
- D. A <span> with visibility: hidden.

Correct answer: B

Elements in the <head> section (like title, meta, link, script) are never drawn to the page viewport, so they are explicitly skipped when compiling the Render Tree.

- Why not A: Empty divs are included; they might have padding or borders that need rendering.
- Why not C: Opacity 0 elements are in the Render Tree; they still take up space and capture clicks.
- Why not D: Visibility hidden elements are still in the Render Tree to reserve layout space.

### Question 2

What is the primary difference between the DOM and the Render Tree?

- A. The DOM contains HTML elements; the Render Tree contains CSS styles.
- B. The Render Tree only contains nodes that will actually be drawn to the screen.
- C. The DOM is created by the server, while the Render Tree is created by the client.
- D. They are two names for the exact same data structure.

Correct answer: B

The Render Tree is a filtered version of the DOM. It excludes everything that doesn't need to be drawn (head tags, display: none elements) and attaches computed styles to everything that does.

- Why not A: The CSSOM contains CSS styles. The Render Tree is the union of both.
- Why not C: Both are created by the client browser.
- Why not D: They are distinct. A node with display:none is in the DOM, but not the Render Tree.

### Question 3

If you toggle an element from `display: none` to `display: block` via JavaScript, what happens?

- A. The browser simply paints the element, as it was already in the Render Tree.
- B. The browser must re-compile that part of the Render Tree and recalculate Layout.
- C. The browser re-downloads the CSS file.
- D. The DOM tree must be completely rebuilt from scratch.

Correct answer: B

Because `display: none` removes the element from the Render Tree, bringing it back forces the browser to re-insert it into the Render Tree and calculate how much space it needs (Layout).

- Why not A: It wasn't in the Render Tree, so it has no layout yet.
- Why not C: The CSSOM is already cached.
- Why not D: The DOM remains intact; only the Render Tree and Layout are affected.

## Remember this

- The Render Tree is DOM + CSSOM.
- Invisible elements (head, meta) and `display: none` elements are excluded.
- `visibility: hidden` elements are included in the tree and take up space.
