Why is the css model above, the js below more attractive?
Here the important point is that the browser loads the page from top to bottom.
Scenario number 1
There is a page on which scripts and styles above
<html> <head> <link rel="stylesheet" href="styles.css"> <script src="scripts.js" type="text/javascript"></script> </head> <body> <!-- here is page content --> </body> </html>
An example script for downloading it will be:
- Loading styles
- Download scripts
- Load the rest of the markup
In this scenario, there is a drawback, before the content of the page is displayed directly, the browser must load styles and scripts, which gives a rather long delay if it is a large resource.
Scenario number 2
There is a page on which styles are at the top, and scripts before the closing body tag.
<html> <head> <link rel="stylesheet" href="styles.css"> </head> <body> <!-- here is page content --> <script src="scripts.js" type="text/javascript"></script> </body> </html>
An example script for downloading it will be:
- Loading styles
- Load markup
- Download scripts
In this scenario, there are advantages over the first, before the content of the page is displayed directly, the browser should load only the styles, and it will load the scripts last, which allows the user to quickly see the contents of the page.
Why styles should be left at the top? If the styles as well as the scripts are moved to the bottom of the page, then the markup after loading will not be stylized, which will look pretty ugly until the moment the styles are loaded.