What is the DOM & How Does HTML Rendering Happen in Browsers?

DOM is a W3C (World Wide Web Consortium) standard. It is a platform and language independent interface standard that allows programs and scripts to dynamically access and update the content, structure, and style of a document.

When a web page loads, t…


This content originally appeared on DEV Community and was authored by Muhammed Erdinç

DOM is a W3C (World Wide Web Consortium) standard. It is a platform and language independent interface standard that allows programs and scripts to dynamically access and update the content, structure, and style of a document.

When a web page loads, the browser builds the page’s DOM tree. In this way, we can change all HTML properties on the page, remove existing HTML elements and attributes, add new HTML elements and attributes, or change all CSS styles on the page with scripting languages ​​such as JavaScript.

Example HTML DOM Tree (Image source: [https://www.w3schools.com/js/pic_htmltree.gif](https://www.w3schools.com/js/pic_htmltree.gif))

What is the DOM Tree?

A DOM tree is a type of tree whose nodes represent the content of the HTML and XML document. Every HTML or XML document has a unique DOM tree representation. For example, let’s examine the following code:

<html>
<head>
  <title>My document</title>
</head>
<body>
  <h1>Header</h1>
  <p>Paragraph</p>
</body>
</html>

The DOM tree of the above code block is as follows:

Example HTML DOM Tree 2

How the Browser Renders HTML?

A web browser is a piece of software that downloads files from a remote server and displays them to you, allowing user interaction. The process of compiling the files downloaded from a remote server and showing them to the user is done by browser engines. If you are interested, you can examine different browser engines and comparisons.

Data is sent over the Internet as byte packets. The browser must convert these data bytes into a form it understands. Firstly, bytes are converted to HTML characters and then to Tokens. In the next step, Tokens are converted into nodes. Nodes are different objects with certain properties. After the nodes are created, the DOM tree is created.

Stages of creating the DOM tree (Image source: [web-dev](https://web-dev.imgix.net/image/C47gYyWYVMMhDmtYSLOWazuyePF2/cSL20piziX7XLekCPCuD.png?auto=format&w=845))

While creating the DOM tree, the “document” node is created first. Document is a node that represents the entire HTML document. The node that specifies an HTML tag is called “elements”. We can learn the type of any node in the DOM tree with “nodeType”. The NodeType property returns a number. You can review the relevant document to find out which node type this number represents.

Type of document node

The DOM tree has been successfully created, but the browser needs information on how the elements will appear in order to render the page. It is the CSSOM’s responsibility to know how the elements of a page should appear.

What is the CSSOM?

While creating the DOM tree, a request is sent to the CSS link in the

and the CSS styles are returned as a result of this request. As with HTML tags, CSS information comes in bytes, and the CSS Object Model (CSSOM) is created by going through certain stages.

Stages of CSSOM tree structure (Image source: [web-dev](https://web-dev.imgix.net/image/C47gYyWYVMMhDmtYSLOWazuyePF2/yb5YfU0vx6vHvB7c0fyD.png?auto=format&w=845))

CSS bytes are converted to characters, then tokens and nodes; Finally, a tree structure known as the CSS Object Model or CSSOM for short, is created.

CSSOM tree example (Image source: [web-dev](https://web-dev.imgix.net/image/C47gYyWYVMMhDmtYSLOWazuyePF2/keK3wDv9k2KzJA9QubFx.png?auto=format&w=650))

What is the Render Tree?

DOM and CSSOM tree structures are two independent structures. The DOM contains all the information about the relationships of the HTML element of the page, while the CSSOM contains the information about how to style the elements.

DOM + CSSOM = Render Tree (Image source: [web-dev](https://web-dev.imgix.net/image/C47gYyWYVMMhDmtYSLOWazuyePF2/b6Z2Gu6UD1x1imOu1tJV.png?auto=format&w=845))

The Render Tree contains information about all visible DOM content on the page and all necessary CSSOM information for the different nodes. If an element is hidden by CSS (for example, display: none), it means that the node will not be represented in the Render Tree.

The hidden element (for example, display: none) is present in the DOM, but not in the Render Tree. This is because the Render Tree combines information from both the DOM and the CSSOM.

When the browser renders a Render Tree, it first renders each visible node, starting from the root of the DOM tree (Tags such as script, meta are not included in the Render Tree, and the nodes hidden by CSS are not included in the Render Tree).

visibility: hidden and **display: none **are different. The first makes the item invisible, but the item is contained in the Render Tree (as an empty node), whereas the second (display: none) removes the item from the Render Tree.

Layout and Paint Steps

We have all the content and style information that will be displayed on the screen with the Render Tree, but the image has not yet appeared on the screen. Firstly, the browser has to calculate the exact size and position of each object on the page.

To determine the exact size and position of each object, the browser starts at the root of the Render Tree and calculates each object on the page. As a result of this calculation, the exact location and size of each element is determined. In the next step, the browser paints the nodes one by one on the screen.

Sample HTML Output (Image source: [web-dev](https://web-dev.imgix.net/image/C47gYyWYVMMhDmtYSLOWazuyePF2/H9mc9hE33imsdh7TfCgm.png?auto=format&w=741))

Resources Blocking Rendering

DOM and CSSOM must be created before painting. Getting the HTML and CSS to the client as soon as possible is important for optimizing the time to first rendering of web applications.

Even a simple web page is likely to have used JS. When the browser encounters a script tag while reading scripts, the DOM rendering process is paused and stopped until the script file has finished executing. This is because JavaScript can modify both the DOM and the CSSOM. Because the browser isn’t sure what the JavaScript will do, it takes precautions by completely stopping the entire DOM structure.

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Örnek</title>
    <link rel="stylesheet" href="style.css">
    **<script src="app.js"></script>**
</head>

<body>
  .....
</body>

</html>

In the above code example, when the browser comes to the script tag, the DOM rendering process will be stopped until the script file is finished executing. As a different scenario, if the app.js file was being pulled from a server rather than locally, and it was taking seconds to fetch app.js due to network connection, the DOM construction process would also be stopped for the same time.

Let’s continue with a different scenario, for example, when the browser encounters a script tag, if the CSSOM is not ready yet, the JS execution will wait until the CSSOM is ready.

By default the DOM construction process will be stopped whenever the browser encounters a script tag. If you add the “async” keyword to your script tag, the DOM rendering will not be stopped and will be executed when the script file is downloaded and ready.

<!DOCTYPE html>
<html>

<head>
<meta name=”viewport” content=”width=device-width,initial-scale=1">
<title>Örnek</title>
<link rel=”stylesheet” href=”style.css”>
<script src=”https://random-app.js" async></script>
</head>
<body>
....
</body>
</html>




References:


This content originally appeared on DEV Community and was authored by Muhammed Erdinç


Print Share Comment Cite Upload Translate Updates
APA

Muhammed Erdinç | Sciencx (2023-03-05T14:34:53+00:00) What is the DOM & How Does HTML Rendering Happen in Browsers?. Retrieved from https://www.scien.cx/2023/03/05/what-is-the-dom-how-does-html-rendering-happen-in-browsers/

MLA
" » What is the DOM & How Does HTML Rendering Happen in Browsers?." Muhammed Erdinç | Sciencx - Sunday March 5, 2023, https://www.scien.cx/2023/03/05/what-is-the-dom-how-does-html-rendering-happen-in-browsers/
HARVARD
Muhammed Erdinç | Sciencx Sunday March 5, 2023 » What is the DOM & How Does HTML Rendering Happen in Browsers?., viewed ,<https://www.scien.cx/2023/03/05/what-is-the-dom-how-does-html-rendering-happen-in-browsers/>
VANCOUVER
Muhammed Erdinç | Sciencx - » What is the DOM & How Does HTML Rendering Happen in Browsers?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/05/what-is-the-dom-how-does-html-rendering-happen-in-browsers/
CHICAGO
" » What is the DOM & How Does HTML Rendering Happen in Browsers?." Muhammed Erdinç | Sciencx - Accessed . https://www.scien.cx/2023/03/05/what-is-the-dom-how-does-html-rendering-happen-in-browsers/
IEEE
" » What is the DOM & How Does HTML Rendering Happen in Browsers?." Muhammed Erdinç | Sciencx [Online]. Available: https://www.scien.cx/2023/03/05/what-is-the-dom-how-does-html-rendering-happen-in-browsers/. [Accessed: ]
rf:citation
» What is the DOM & How Does HTML Rendering Happen in Browsers? | Muhammed Erdinç | Sciencx | https://www.scien.cx/2023/03/05/what-is-the-dom-how-does-html-rendering-happen-in-browsers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.