Including Google Web Fonts link or import?

kajo picture kajo · Sep 7, 2012 · Viewed 87.9k times · Source

What is the preferred way of including Google Web Fonts to a page?

  1. via the link tag?

    <link href='http://fonts.googleapis.com/css?family=Judson:400,400italic,700' rel='stylesheet' type='text/css'>
  2. via import in a stylesheet?

    @import url(http://fonts.googleapis.com/css?family=Kameron:400,700);
  3. or use the web font loader

    https://developers.google.com/webfonts/docs/webfont_loader

Answer

igrigorik picture igrigorik · Sep 12, 2012

For 90%+ of the cases you likely want the <link> tag. As a rule of thumb, you want to avoid @import rules because they defer the loading of the included resource until the file is fetched.. and if you have a build process which "flattens" the @import's, then you create another problem with web fonts: dynamic providers like Google WebFonts serve platform-specific versions of the fonts, so if you simply inline the content, then you'll end up with broken fonts on some platforms.

Now, why would you use the web font loader? If you need complete control over how the fonts are loaded. Most browsers will defer painting the content to the screen until all of the CSS is downloaded and applied - this avoids the "flash of unstyled content" problem. The downside is.. you may have an extra pause and delay until the content is visible. With the JS loader, you can define how and when the fonts become visible.. for example, you can even fade them in after the original content is painted on the screen.

Once again, the 90% case is the <link> tag: use a good CDN and the fonts will come down quick and even more likely, be served out of the cache.

For more info, and an in-depth look at Google Web Fonts, check out this GDL video