Frontend System Design · Lesson 6 of 86 · 4 min
DOM, CSSOM and the Render Tree
This is how you explain what happens before the first pixel is drawn, and why display: none is not the same as visibility: hidden.
After this lesson, you'll be able to walk an interviewer through the browser's first steps in turning code into a visual representation. You'll know the data structures involved, how they're built, and what that means for performance. This isn't just theory; it's the foundation for diagnosing rendering bottlenecks.
How the browser builds its blueprint
The browser receives bytes of HTML and CSS, but it can't render from that directly. It needs structured, in-memory representations. It builds two of these in parallel: the DOM and the CSSOM.
The Document Object Model (DOM) is the browser's object representation of your HTML document. The parser goes through the HTML, token by token, and builds a tree of nodes. <html> is the root, <body> is a child, a <div> inside that is a grandchild, and so on. This tree represents the document's structure and content.
Simultaneously, the browser parses your CSS from <link> tags, <style> blocks, and inline styles. It builds the CSS Object Model (CSSOM), another tree structure. This tree represents the styles, their specificity, and how they cascade. A rule for div p will be a child of a rule for div.
The trap: Thinking that CSS parsing waits for the DOM. It doesn't. They happen in parallel. However, the browser cannot proceed to the next stage—building the Render Tree—until it has both the complete DOM and the complete CSSOM. This is why both HTML and CSS are considered render-blocking resources.
Combining DOM and CSSOM into the Render Tree
The Render Tree is the browser's internal model of what will actually be rendered on the page. It's the synthesis of structure (DOM) and style (CSSOM).
The browser walks the DOM tree, starting from the root. For each node, it looks for matching rules in the CSSOM and calculates the computed styles for that node. It then creates a "render object" that contains both the content and the final styles.
This process creates a new tree, the Render Tree, which maps closely to the DOM but isn't a 1:1 copy.
What the Render Tree leaves out
The key is that the Render Tree only includes what is visible. This is a critical distinction.
Nodes that are not in the Render Tree:
- Non-visual elements like
<head>,<script>,<meta>, and<link>. They provide information but don't have a visual output. - Elements explicitly hidden with
display: none. The browser knows these elements (and their children) take up no space, so it omits them entirely from the blueprint for the screen.
"What's the difference between
display: noneandvisibility: hidden?" This is a classic question. Your answer must center on the Render Tree.An element with
display: noneis not included in the Render Tree. It occupies no space and is not rendered.An element with
visibility: hiddenis included in the Render Tree. It still occupies its space in the layout, creating an empty box. It just isn't painted.
Understanding this distinction shows you're thinking about layout and rendering, not just whether something is visible.
How JavaScript changes trigger updates
JavaScript interacts with the page by modifying the DOM. When you change an element's content, add a class, or alter an inline style, you are changing the DOM or, by extension, the CSSOM. The browser must then react to this change.
Consider this simple page and some JavaScript modifications.
<!DOCTYPE html>
<html>
<head>
<title>Render Tree Example</title>
<style>
body { font-family: sans-serif; }
.app { padding: 20px; background-color: #f0f0f0; }
h1 { color: #333; }
.hidden { display: none; }
</style>
</head>
<body>
<div class="app">
<h1>Welcome!</h1>
<p class="hidden">I am hidden.</p>
</div>
</body>
</html>
// Get a reference to the main container
const appDiv = document.querySelector('.app');
// 1. Change content: Modifies a DOM text node
const heading = document.querySelector('h1');
heading.textContent = 'Hello, Engineer!';
// 2. Change style: Triggers CSSOM/style recalculation for one element
appDiv.style.backgroundColor = '#e0e0ff';
// 3. Unhide an element: Major change.
// This adds the element back into the Render Tree.
const hiddenP = document.querySelector('.hidden');
hiddenP.classList.remove('hidden');
hiddenP.textContent = 'I am now visible!';
Each of these changes invalidates some part of the rendering data.
- Changing
textContentorbackgroundColormight only require a repaint of that element. - Removing the
.hiddenclass is a bigger deal. The browser must add the element and its children back into the Render Tree, then recalculate the layout of the page to make space for it. This is a "reflow" or "layout" operation, and it's expensive.
The trap: Saying "DOM manipulation is slow" is too generic. A senior engineer explains why. It's slow because a seemingly small DOM change can force the browser to invalidate the Render Tree, recalculate the layout of many elements, and repaint the screen. The cost is in the rendering work that follows the DOM update.
Try it
Open DevTools on any site. In the Elements panel, find a visible element. Right-click and select "Hide element" (which applies display: none !important). Watch it disappear and the layout shift. Now, uncheck that style in the Styles pane, and add visibility: hidden;. See how the space is preserved. This is the Render Tree in action.
Next: We'll break down what happens after the Render Tree is built in Layout, Paint and Compositing.
Lock it in — apply this lesson under interview conditions.