Skip to content
YouTurkey Tech
All HTML topics

How the browser builds the DOM

Beginner

How the browser reads your HTML tag by tag and grows a tree of nodes from it, and why that tree, not your source, is what JavaScript sees.

Tree

Empty

Console

Empty

0/20Press Play, or step through with the arrows, to begin.
Step 0 of 20

Explanation

3 min read

On this page

What is the DOM?

The DOM (Document Object Model) is the tree of objects the browser builds from your HTML. Every element, every piece of text and every comment becomes a . Each node has one parent and an ordered list of children, and the root of the tree is the document.

The browser draws the page from this tree (together with the CSS), and JavaScript reads and changes the page through it: document.querySelector, element.textContent, element.append… Your HTML is the text you send; the DOM is the live structure the browser keeps in memory.

How is the DOM different from your HTML?

The parser reads the HTML from top to bottom, one token at a time, and keeps a stack of open elements:

  • A start tag (<p>) creates an element, attaches it to the open element on top of the stack, and pushes it on the stack.
  • Text becomes a text node inside the element on top of the stack.
  • An end tag (</p>) closes that element and pops it off the stack.

Because the browser must show something for any input, it never stops at a mistake. It repairs the markup as it goes, so the tree can differ from the source:

  • It adds what is missing: html, head and body if you leave them out, and a tbody around table rows.
  • It closes elements for you: a new <p> or <li> closes the previous one, and a <div> closes an open <p>.
  • It moves or drops what cannot be there: an element written directly inside a <table> is moved before the table, and a stray end tag is ignored.

The DOM also keeps changing after parsing, every time JavaScript adds, removes or edits a node. View Source shows the HTML that arrived; the Elements panel in DevTools shows the DOM as it is now.

What mistakes do people make with the DOM?

Mini example

None of these end tags are written, yet the DOM has two paragraphs and a list with two items, each properly closed, inside an html, head and body the browser adds:

<p>First paragraph<p>Second paragraph<ul>  <li>One  <li>Two</ul>

Quick check

How many p elements does the DOM built from the mini example contain?

Further reading

All steps as text
  1. Step 1:

    The browser receives the page as plain text and starts parsing it. First it creates the Document node, the root of the tree.

  2. Step 2:

    <!DOCTYPE html> becomes a doctype node under the document. It tells the browser to use standards mode.

    Line 1

  3. Step 3:

    A start tag: the parser creates an html element with its lang attribute, and puts it on the stack of open elements.

    Line 2

  4. Step 4:

    <head> goes inside html, because html is the open element on top of the stack.

    Line 3

  5. Step 5:

    <title> opens inside head, and the text Scores becomes a text node, its child.

    Line 4

  6. Step 6:

    </title> and </head> are end tags. Each one closes the element on top of the stack.

    Lines 4–5

  7. Step 7:

    <body> becomes the second child of html. The spaces and line breaks between tags are text nodes too; the picture leaves them out.

    Line 6

  8. Step 8:

    <h1> opens inside body, gets its text node, and </h1> closes it again.

    Line 7

  9. Step 9:

    <table> opens inside body.

    Line 8

  10. Step 10:

    In the DOM a row cannot sit directly in a table. So before the <tr>, the parser creates a tbody the source never wrote.

    Line 9

  11. Step 11:

    Then the row goes inside that tbody.

    Line 9

  12. Step 12:

    <td>Ali</td> adds a cell with its text node, then closes it.

    Line 10

  13. Step 13:

    The second cell, 12, joins the same row.

    Line 11

  14. Step 14:

    </tr> closes the row. </table> closes the table and the tbody inside it, though no </tbody> was written.

    Lines 12–13

  15. Step 15:

    The script element and its text are added. At </script> the parser pauses and runs the script, with only the tree built so far.

    Lines 14–17

  16. Step 16:

    querySelector searches the DOM tree, not the HTML text. Its way to the row goes through the tbody.

    Line 15

  17. Step 17:

    The row’s parentNode is the tbody, not the table the source shows.

    Line 16

  18. Step 18:

    So the script logs TBODY. JavaScript only ever sees the DOM.

    Line 16

  19. Step 19:

    Parsing resumes: </body> and </html> end the document. At the end of the text, the parser closes everything still open.

    Lines 18–19

  20. Step 20:

    The finished DOM: built from your HTML, but not the same thing. It has nodes you never wrote, and it is what CSS and JavaScript work with.