A concise introduction of how the browser displays a web page.
Document Object Model (DOM) is an interface which treats the HTML (or XHTML/XML) as a tree structure. The node of this tree represents an HTML element.
The process of rendering a page on the web browser involves many steps. Simply speaking, when we type a URL in the address bar of the web browser and hit enter, the browser makes a call to the server and receives the response. The response only contains the texts and instructions.
The page that we see on the browser, the design, the colours, the dimensions of the containers, and so on is not something the browsers get from the server and display it.
At an abstract level, the browsers goes through the contents it gets and performs these steps to display the web pages.
Step 1: Construct the DOM: The browser parses the HTML content and creates the DOM, the tree structure with the nodes as mentioned above, sometimes also referred to as content-tree.
Step 2: Create the Render Tree: A render tree contains styling attributes as well as the dimensions of boxes. For example, a div with red as background, black border and 100 x 100 pixels in dimensions with some texts/images.
Step 3: Create the layout of the Render Tree: In this step, the browser constructs the view of the page, i.e. it determines which box will appear where on the screen based on given attributes.
Step 4: Painting the Render Tree: The browser traverses the render tree [with layout] and paints each node on the page.
For the detailed information on how browsers work, check this page. The browser needs to go through these steps every time to display a page.
Why do you see a frozen or flickering screen?
Now that we have established that the browser has to perform these tasks to render a web page, we can tell this is valid for every new web page that it has to display. But what is a new page?
When we click on a link, the page refreshes and, we see a different page. In case of an SPA (single page application), the browser does not go through a full page reload but displays a new content. In such scenarios, the browser gets a new page.
Now let us assume, we click on a close icon to remove an element from the page. Does it create a new page? We would say no. But for the browsers, it is a new page. Why?
Because when we remove an element from the page, it changes the structure of the page. Technically, the DOM tree changes, and if the DOM tree changes, we are back to step 1. If you have read the article I linked above, you know it’s an expensive job. Consider removing or even adding elements one by one, back to back.
Repaint Vs Reflow
A repaint happens when the browsers don’t have to go back to step 1 all the way. In this case, the structure of the page does not change. For example, consider modifying properties like opacity, visibility, background colour, font-colour, and so on.
A reflow happens when the structure of the page changes and the browsers have to go back to step 1. For example, hiding/showing/removing/adding elements on the page, changing dimension of a box, etc.
A reflow is more expensive than a repaint.
As a thumb rule, the number of reflows or repaints [DOM Manipulation] done on the page is directly proportional to the amount of work the browsers have to do. And if this happens, you get a frozen or flickering screen.