Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order of linking font, CSS and scripts

If I have a website project with:

  • reset.css
  • remote bootstrap lib
  • master.css
  • remote font awesome CSS
  • two google fonts
  • JQuery lib
  • main.js

Consider the best loading speed and possible override. What would be the best order to link them in <head>

I know what is added later will override the same rule with same priority previous style sheets applied and browser rendering from top to bottom, <head> first then <body>

I also learned from google that there is something called prefetch in the modern browsers.

Personally, I would do reset.css, font awesome, google font, bootstrap lib, master.css, Jquery lib, main.js. Universal rules first, lib first. But I don't know exactly how to deal with font since they are stylesheet as well.

like image 980
Donghui Ma Avatar asked Sep 14 '17 09:09

Donghui Ma


People also ask

How to use font-family in CSS?

In CSS, we use the font-family property to specify the font of a text. The font-family property should hold several font names as a "fallback" system, to ensure maximum compatibility between browsers/operating systems. Start with the font you want, and end with a generic family (to let the browser pick a similar font in the generic family, ...

What are the different types of fonts in CSS?

Some Font Examples 1 Times New Roman Georgia Garamond 2 Arial Verdana Helvetica 3 Courier New Lucida Console Monaco 4 Brush Script MT Lucida Handwriting 5 Copperplate Papyrus. In CSS, we use the font-family property to specify the font of a text. ...

Where do I define my own fonts in CSS?

Your "own" fonts are defined within the CSS @font-face rule. TrueType is a font standard developed in the late 1980s, by Apple and Microsoft. TrueType is the most common font format for both the Mac OS and Microsoft Windows operating systems.

What is a font stack in CSS?

A font stack is a list of fonts in the CSS font-family declaration. The fonts are listed in order of preference that you would like them to appear in case of a problem, such as a font not loading.


2 Answers

I would like to point out that the orders suggested in previous answers are not fully in sync with the developers' best practices as suggested by Google and MDN.

CSS should be loaded first in the head followed by font stylesheets or google font stylesheets so that the layout doesn't appear broken for a while especially on slow connections.

So, Bootstrap css can be loaded into head while Bootstrap js should be loaded later after jQuery.

JS, jQuery and its dependencies can be moved to the bottom of the page to promote faster page loading because JS code can affect the content and layout of a web page, browsers delay rendering any content that follows a script tag until that script has been downloaded, parsed and executed.

And as bootstrap js has jQuery as a dependency. So, jQuery should be loaded first followed by boootstrap js and main js file.

So, the correct order in accordance with best developer practices:

<head>

 1. Bootstrap CSS    
 2. Reset CSS
 3. Master CSS
 4. Google Font CSS
 5. Font Awesome CSS

</head>
<body>

Your content goes here    

<!-- add js files to the bottom of the page-->

 6. jQuery 
 7. Bootstrap js
 8. Main js

</body>
like image 184
Arun Sharma Avatar answered Oct 03 '22 13:10

Arun Sharma


It is important to load jQuery before Bootstrap and all custom CSS after bootstrap. It is better to load the google font stylesheet in the beginning.

The order should be libraries first followed by custom scripts and styles. Since bootstrap depends on jQuery, jQuery should be loaded before loading the Bootstap's JavaScript file.

  1. google font
  2. fontawesome
  3. JQuery lib
  4. remote bootstrap lib
  5. reset.css
  6. master.css
  7. main.js

Loading the JavaScript files at the end of the body (just before </body>) will improve site loading speed when compared to having JavaScript files between the head tags.

like image 32
Parthipan Natkunam Avatar answered Oct 03 '22 14:10

Parthipan Natkunam