Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to load Google fonts <link/> , @import or javascript

I wonder what is the best way to load fonts from Google Fonts. I googled and founded <link/> way better than @import. Are there any tools to measure speed and performance over network and browser? What about javascript method?

<link href='http://fonts.googleapis.com/css?family=Oswald:300' rel='stylesheet' type='text/css'>

or

@import url(http://fonts.googleapis.com/css?family=Oswald:300);

or

<script type="text/javascript">
  WebFontConfig = {
    google: { families: [ 'Oswald:300:latin' ] }
  };
  (function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
      '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
  })(); </script> 
like image 672
Nishantha Avatar asked Apr 17 '15 18:04

Nishantha


People also ask

Is it better to link or import Google Fonts?

Should I use <link> or @import ? # Loading Google fonts in the HTML reduces the critical request depth and speeds up page load Always import your fonts from HTML, not CSS.

How do I import a Google font into Javascript?

Google Fonts can be loaded through Javascript using the Web Font Loader Library. However Google Fonts can be loaded using JavaScript also. This is done using the Web Font Loader Javascript library.

Is it better to import font in CSS or HTML?

CSS files support web fonts basically in every browser by naming the font-family declaring the source file. The best way is for me is to include it into my . css file and linking it into my header.


1 Answers

<link> is preferred to @import, as it doesn't block parallel downloads. See this link for more details: http://www.stevesouders.com/blog/2009/04/09/dont-use-import/

If the fonts you want to use are static and served by Google, the JavaScript solution is probably worse off performance-wise than both <link> and @import, since it has to load an external script (//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js) which then, after what I can see, injects the same link element you could just put directly in your HTML source.

like image 134
Jacob Bundgaard Avatar answered Jan 03 '23 12:01

Jacob Bundgaard