Maybe everyone knows that js execution will block the parsing and rendering of the DOM tree, so will CSS loading block the parsing and rendering of the DOM tree? Next, let’s analyze it together. Principle Analysis So why does the above phenomenon occur? Let's analyze it from the browser's rendering process. Different browsers use different kernels, so their rendering processes are also different. There are currently two main WebKit rendering process Gecko rendering process From the two flowcharts above, we can see that the browser rendering process is as follows:
From the process we can see
DOMContentLoaded For browsers, there are two main events for page loading, one is DOMContentLoaded and the other is onLoad. There is nothing much to say about onLoad. It will only be triggered after all resources of the page are loaded. These resources include css, js, pictures, videos, etc. DOMContentLoaded, as the name implies, is triggered when the content of the page is parsed. Well, as we discussed above, css will block Dom rendering and js execution, and js will block Dom parsing. Then we can make the assumption that
Let's test the first case first: <!DOCTYPE html> <html lang="en"> <head> <title>css blocking</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script> document.addEventListener('DOMContentLoaded', function() { console.log('DOMContentLoaded'); })  </script> <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css" rel="stylesheet"> </head> <body> </body> </html> The experimental results are as follows: From the animated picture we can see that the DOMContentLoaded event has been triggered before the css is loaded. Because there is no js code behind the css. Next, we will test the second case. It is very simple. Just add a line of code after the css. <!DOCTYPE html> <html lang="en"> <head> <title>css blocking</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script> document.addEventListener('DOMContentLoaded', function() { console.log('DOMContentLoaded'); }) </script> <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css" rel="stylesheet"> <script> console.log('Is it my turn yet?'); </script> </head> <body> </body> </html> We can see that the DOMContentLoaded event is triggered only after the css loading is complete. Therefore, we can conclude that:
Summarize From the above, we can draw the following conclusions:
Therefore, in order to avoid users seeing a long white screen time, we should increase the CSS loading speed as much as possible, such as using the following methods:
This is the end of this article on whether CSS will block page rendering. For more information about CSS blocking page rendering, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: My CSS framework - base.css (reset browser default style)
>>: Details of watch monitoring properties in Vue
When making web pages, you often encounter the pr...
This article is intended to be a starting point f...
1. Unzip to the location where you want to instal...
Install MySQL under Windows for your reference. T...
This article shares the specific code for Vue to ...
There are three types of MySQL stored procedure p...
1. Install MySQL database ① Download and unzip an...
In this article, the blogger will take you to lea...
Table of contents 1. What is Promise? 2. Why is t...
1. Preparation Middleware: Tomcat, Redis, Nginx J...
The reason is that this type of web page originate...
The two parameters innodb_flush_log_at_trx_commit...
In this article, we would like to share with you ...
Table of contents 1. Project Requirements Second,...
1. MS SQL SERVER 2005 --1. Clear the log exec(...