# DOM Construction

> How the browser builds the Document Object Model tree from parsed HTML nodes.

Subject: Internet | Lesson 25 of 35 | 5 min read
Interactive lesson: https://codewithanimation.in/internet/dom-construction
Previous: [The Parsing Phase](https://codewithanimation.in/internet/parsing-phase.md) | Next: [CSSOM Construction](https://codewithanimation.in/internet/cssom-construction.md)

## What it is

The DOM (Document Object Model) is the browser's internal representation of the HTML document. It is a massive tree-like data structure.

As the parser emits Nodes (from the Parsing Phase), it links them together into the DOM tree, capturing parent-child relationships, text content, and attributes.

## Why it matters

The DOM is the *only* way JavaScript can interact with the page. JS does not read your HTML string; it queries and mutates the DOM tree.

Because the DOM is a massive tree, manipulating it with JavaScript can be computationally expensive, which is why libraries like React invented the Virtual DOM.

## How it works

The browser starts with a root `Document` node.

As the parser reads `<html>`, it attaches an `HTMLHtmlElement` node to the Document.

When it encounters nested tags like `<body>` and `<p>`, it creates `HTMLBodyElement` and `HTMLParagraphElement` nodes, attaching them as children of their parent nodes.

When a closing tag `</p>` is encountered, the parser moves back up the tree, ready to attach the next sibling.

## Common mistake: The 'HTML equals DOM' Illusion

A huge misconception is that the DOM is exactly what you wrote in your HTML file.

The DOM is a *living* structure. If you write invalid HTML (like a `<td>` outside of a `<tr>`), the browser will attempt to fix it and create a valid DOM tree that differs from your source code.

Furthermore, JavaScript can add or remove nodes, meaning the DOM is completely different from the original HTML 10 seconds after the page loads.

## Tip: Pro tip

**Text is a Node too**: In the DOM, the text inside a paragraph isn't just a property of the paragraph. It's an actual `TextNode` that sits as a child of the `HTMLParagraphElement`.

Whitespace and newlines in your HTML often become empty `TextNode` elements in the DOM, which can cause unexpected layout issues if you aren't careful.

## Try it: Try it

Watch how a nested HTML structure maps perfectly into a parent-child DOM tree.

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

## Check yourself

### Question 1

What happens if your HTML contains an unclosed `<p>` tag followed by a `<div>`?

- A. The parser throws a fatal error and shows a blank white page.
- B. The browser automatically closes the `<p>` tag and builds a valid DOM.
- C. The `<div>` becomes a permanent string literal inside the paragraph.
- D. The DOM tree skips the broken paragraph entirely.

Correct answer: B

Browsers are notoriously forgiving. They use complex error-correction algorithms to build a valid DOM tree even from broken HTML.

- Why not A: Browsers almost never throw fatal parsing errors for HTML.
- Why not C: The browser knows divs belong outside paragraphs or handles them as block elements.
- Why not D: Browsers try to preserve as much content as possible.

### Question 2

In the DOM tree, what is the relationship between `<body>` and `<h1>` if the h1 is directly inside the body?

- A. They are sibling nodes.
- B. The h1 is a child node of the body node.
- C. The body is a property of the h1 object.
- D. They are merged into a single BodyHeading node.

Correct answer: B

HTML nesting perfectly translates to parent-child relationships in the DOM tree.

- Why not A: They are not at the same level.
- Why not C: Nodes are distinct objects linked together.
- Why not D: Each distinct HTML element gets its own distinct DOM Node.

### Question 3

How does JavaScript view the DOM?

- A. As a giant string of HTML.
- B. As a read-only snapshot of the initial page load.
- C. As an Object-Oriented tree of Node instances.
- D. As a CSS Object Model (CSSOM).

Correct answer: C

The DOM is literally an API of JavaScript Objects (like HTMLDivElement) that inherit from the base Node class, structured as a tree.

- Why not A: JS interacts with the DOM API, not string parsing.
- Why not B: The DOM is fully mutable.
- Why not D: The CSSOM is a completely separate tree for styles.

## Remember this

- The DOM is a tree of Node objects, not an HTML string.
- The DOM is living and mutable by JavaScript.
- Browsers auto-correct invalid HTML when building the DOM tree.
