- Parsing of the HTML document to DOM (Document Object Model).
- CSS file interpretation (CSSOM – Cascading Style Sheets Object Model) for each of the DOM nodes.
- Creation of the new tree that includes the DOM, and each node’s style and layout.
- A render tree is rendered.
But, before we go any further with a web browser’s render tree, have you ever participated, or seen, a sports game where fans of a team sing while a visual shows as big as the dimensions of a public sector? The following image is an example of what I am talking about.
Credit: Scene from a Youtube video recorded by the user barcelona.jp
Why the question?
Because I am going to explain how the rendering engine of the browser works similarly to a stadium filled with people that together, illustrate a message while they chant to their favorite team.
1. Parsing: HTML to DOM
Web browsers do not interpret HTML files the same as programmers write them. That is why the rendering engine is in charge of parsing (analyzing and separating) the HTML structure into objects that the browser can read.
The rendering engine receives the HTML files as a stadium receives sports fans waiting in line. With lots of people entering consecutively but the stadium does not understand how does each fan group relates to one another or how are they going to be seated.
That’s what the stadium staff is for. They can read each person’s ticket and direct them to their respective chair to structure the team visual that will be seen during the game.
When browsers parse, they consider the HTML tags, and how they are nested, to create objects and relate them, including the DOM (Document Object Model).
The DOM is a document that models the relationship between the different objects, or nodes, that structure the web page.
The process of creating these objects is more complex, so I will not be detailing the process in this article. But, by this point, the DOM knows how the web page will be structured. Although it still has not interpreted how the web page will be styled or laid out.
On our sports fan example, by this point, each fan group has entered the stadium and is seated. But, they still do not have the colored cardboard they are going to display during the chant.
2. CSSOM Construction
Ten (10) minutes before the game starts, the stadium staff with lots of colored cardboard on hand, begin to walk to each corner of the seated rows.
Each cardboard has a seat number assigned, so the visual, at large, makes sense. Let’s say that each carton was appointed to a specific chair by a style calculation.
After converting the HTML into the DOM, the render engine assigns the styles described in the CSS file to each DOM node. This assignment is known as CSS-style computation. This process includes selector pairing and style cascading process (hence the name Cascading Style Sheets).
CSS style computation is a topic that can take up to an entire article, so I will not inquire about that in this article. But, for the sake of this article, it is essential to know that during this stage, the rendering engine assigns styles to each DOM node like color, font, and border.
When DOM nodes are assigned a style, it constructs a new tree. This tree is called the CSSOM (Cascading Style Sheets Object Model).
Up to this point, the rendering engine knows the page’s structure and each element’s style.
3. Rendering Tree
Stadium staff begins to distribute each colored cardboard, and the energy of the crowd transforms from conversations to excitement for the beginning of the game.
The process of distributing the cardboard assigned to each seat can represent the size and position of the node on the screen.
The squared form of each cardboard can also represent the elements that will show on the screen.
Once each node from the DOM has its style assigned, the rendering engine computes the size of each node and its position on the screen.
The result of this computation is a rendering tree. Were boxes represent each of the web page’s elements.
All of a sudden, the microphone turns ON, and a staff member welcomes the crowd. The game is about to begin.
4. Paint the Render Tree
The rendering engine is responsible for painting on screen the new render tree.
What does painting on-screen means?
Part of the computer’s memory goes to pixels. It is in the pixel memory spaces where the rendering engine sets the style value for each node. That is how pixels get their color, shape, and position.
The crowd goes wild when the home team is announced. The battle chant begins. Each person from the crowd puts the cardboard over their head.
The cardboard paint of each stadium seat with the color, shape, and position required for the message for the team can be appreciated.
“We are ready”, it reads. The energy is glorious.
The Ladybug Podcat’s suggestions led me to a different podcast episode that inspired me with the illustrations, The Code Newbie episode: “How Do Browsers Work?. The programmer and artist Lin Clark details how web browsers function, and her work at Mozilla.
Also, Lin Clark wrote another article: “Inside a Super Fast CSS Engine: Quantum CSS (aka Stylo)” which details the role of CSS when rendering a page.
Lin Clark’s drawings and explanations helped me understand the rendering engine. She goes more into detail about what I vaguely explained in this article so I would invite you to read those articles if you want to inquire more.
Thanks for reading!
The following CTRL-Y articles are related somewhat to this post. You may want to check them out!:
- HTML for Beginners
- HTML: Tag Atrributes
- HTML: Tag Nesting
- Night Owl Home Office
- Resources for Newbie Web Coders
- Programming Language: The General Parts
- Learn to Code: Lowering the Programming Pedestal
- NodeJs Layered Architecture
- Filtering with GraphQL and Prisma: What NOT to Do
By the Way – A playlist for you
I wrote this article listening to the playlist “Creativity Boost” created by Mia Astral on Spotify. This playlist was recommended by a dear friend. It has a great mix of upbeat but smooth music that helped me focus.