Here’s a quick overview of the steps:
To begin with, let’s take a look at the architecture of a modern browser.
Mostly, screens refresh 60 times per second(Frame rate). The browser needs to match the device’s refresh rate and put up 1 new picture/frame, for each of those screen refreshes the browser has limited time 16.66 ms.
If we open up devtools, we can capture the actual time taken for DOM construction
Similar to DOM construction, a browser convert the received CSS rules into something that the browser can understand and work with. Hence, we repeat the HTML process, but for CSS instead of HTML.
If we open up devtools, we can capture the actual time taken for DOM construction
The CSSOM and DOM trees are combined into a render tree, which is then used to compute the layout of each visible element and serves as an input to the paint process that renders the pixels to screen. Optimizing each of these steps is critical to achieving optimal rendering performance.
<script>, <meta>
…etc are omitted since they are not reflected in the rendered output..body.p.span
is missing from the render tree because we set it display: none
.
P.S.
visibility: hidden
!= display: none
visibility: hidden
makes the element invisible, but the element still occupies space in the layout (rendered as an empty box),display: none
removes the element entirely from the render tree such that the element is invisible and is not part of the layout.
E.g. : flow of Safari’s WebKit Rendering Engine
E.g. : flow of Mozilla’s Gecko Rendering Engine
It calculates the exact position of DOM elements and size within the viewport of the device.
To figure out the exact size and position of each object on the page, the browser begins at the root of the render tree and traverses it.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Critial Path: Hello world!</title>
</head>
<body>
<div style="width: 50%">
<div style="width: 50%">Hello world!</div>
</div>
</body>
</html>
The body of the above page contains two nested div’s: the first (parent) div sets the display size of the node to 50% of the viewport width, and the second div—contained by the parent—sets its width to be 50% of its parent; that is, 25% of the viewport width.
When layout is complete, the browser issues “Paint Setup” and “Paint” events, which convert the render tree to pixels on the screen
In this step, the browser combines all the layers together. It is a really important process, especially for the overlapping DOM elements.
There are 3 cases in which the browser will rework again:
layout -> paint -> compose
e.g. if we change the width of an element, this will cause the browser to calculate the position of each element on the screen.
paint -> compose
e.g. if we changed the color
or background-color
of the element, there are no changes in the layout just in the visual part so the browser will rework from the paint step
compose
e.g. if we change the transform of an element, this will merely cause the composing step.
🎊🎊🎊 Finally, DONE 🎊🎊🎊