DEV Community

swetha palani
swetha palani

Posted on

DAY11-12 :1. What is the DOM, and how does the browser create it?

  • DOM (Document Object Model) is a tree-like representation of your web page.
  • It converts your HTML and XML documents into objects and nodes so JavaScript can read, modify, or delete elements dynamically.
  • Think of it as a live interface between your code and what you see in the browser.

When

  • The browser creates the DOM right after it downloads and parses the HTML file, before rendering the page fully.
  • It’s updated in real time when JavaScript manipulates elements (e.g., adding or removing nodes).

Where

  • The DOM exists inside the browser’s memory as part of its rendering engine (e.g., Blink for Chrome, Gecko for Firefox).
  • You interact with it through JavaScript in the browser environment (window.document).

How

  1. Parse HTML → The browser reads raw HTML and tokenizes it.
  2. Create Nodes → Each tag, attribute, and piece of text becomes a node.
  3. Build Tree → Nodes are connected into a tree structure (the DOM tree).
  4. Attach CSSOM → CSS is parsed into a CSS Object Model (CSSOM) and combined with the DOM for rendering.
  5. Render → The browser paints the pixels on screen based on the DOM + CSSOM.

Example:
If you write:

<body>
  <h1>Hello</h1>
  <p>World</p>
</body>
Enter fullscreen mode Exit fullscreen mode

The DOM tree looks like:

Document
 └─ html
    └─ body
       ├─ h1 ("Hello")
       └─ p ("World")
Enter fullscreen mode Exit fullscreen mode

You can access the <h1> using JavaScript:

Top comments (0)