Why is it generally a good idea to position CSS <link>s between <head></head> and JS <script>s just before </body>? Do you know any exceptions?

Placing <link>s in the <head>.

Putting <link>s in the <head> is part of proper specification in building an optimized website. When a page first loads, HTML and CSS are being parsed simultaneously; HTML creates the DOM (Document Object Model) and CSS creates the CSSOM (CSS Object Model). Both are needed to create the visuals in a website, allowing for a quick "first meaningful paint" timing.

 

This progressive rendering is a category optimization sites are measured in their performance scores.

Why is it generally a good idea to position CSS <link>s between <head></head> and JS <script>s just before </body>? Do you know any exceptions? (Cont...)

Placing <link>s in the <head>. (Cont...)

Putting stylesheets near the bottom of the document is what prohibits progressive rendering in many browsers.
 

Some browsers block rendering to avoid having to repaint elements of the page if their styles change. The user is then stuck viewing a blank white page. Other times there can be flashes of unstyled content (FOUC), which show a webpage with no styling applied.

Why is it generally a good idea to position CSS <link>s between <head></head> and JS <script>s just before </body>? Do you know any exceptions? (Cont...)

Placing <script>s just before </body>.

<script> tags block HTML parsing while they are being downloaded and executed which can slow down your page. Placing the scripts at the bottom will allow the HTML to be parsed and displayed to the user first.

 

An exception for positioning of <script>s at the bottom is when your script contains document.write(), but these days it's not a good practice to use document.write().

Also, placing <script>s at the bottom means that the browser cannot start downloading the scripts until the entire document is parsed.

Why is it generally a good idea to position CSS <link>s between <head></head> and JS <script>s just before </body>? Do you know any exceptions? (Cont...)

Placing <script>s just before </body>. (Cont...)

This ensures your code that needs to manipulate DOM elements will not throw and error and halt the entire script. If you need to put <script> in the <head>, use the defer attribute, which will achieve the same effect of downloading and running the script only after the HTML is parsed.

 

Keep in mind that putting scripts just before the closing </body> tag will create the illusion that the page loads faster on an empty cache (since the scripts won't block downloading the rest of the document).

Why is it generally a good idea to position CSS <link>s between <head></head> and JS <script>s just before </body>? Do you know any exceptions? (Cont...)

Placing <script>s just before </body>. (Cont...)

However, if you have some code you want to run during page load, it will only start executing after the entire page has loaded. If you put those scripts in the <head> tag, they would start executing before - so on a primed cache the page would actually appear to load faster.

Why is it generally a good idea to position CSS <link>s between <head></head> and JS <script>s just before </body>? Do you know any exceptions?

By Code 100mph

Why is it generally a good idea to position CSS <link>s between <head></head> and JS <script>s just before </body>? Do you know any exceptions?

  • 296