Font Loading Techniques

By  on  

This post is a work in progress and will be updated over time. Some techniques are browser-specific and experimental. Thank you for looking!

It seems as though most websites use non-default fonts these days and who can blame them?  System fonts are all pretty boring and custom fonts add a bit of flare and freshness to a site's design.  The problem with using custom fonts, however, is they can really slow down your site load.  Font files are large, can require separate font files for bold and italic, and can block rendering if the developer doesn't work around them.  Let me show you a strategy for faster font loading!

1. Put Fonts on CDN

One simple solution for improving site speed is using a CDN, and that's no different for fonts.  It's important to ensure the CDN has proper CORS settings, as I discussed in Service Fonts from CDN:

# Apache config
<FilesMatch ".(eot|ttf|otf|woff)">
	Header set Access-Control-Allow-Origin "*"
</FilesMatch>

# nginx config
if ($filename ~* ^.*?\.(eot)|(ttf)|(woff)$){
	add_header Access-Control-Allow-Origin *;
}

You'll see AJAX / cross-domain errors in the console if the CDN's CORS settings are not properly configured.

2.  Use Non-Blocking CSS Loading

My Mozilla colleague Stephanie Hobson shared Loading CSS without blocking render with me, which outlines a strategy for preventing CSS loading from blocking render.  It's actually genius:

<link rel="stylesheet" type="text/css" href="fonts.css" media="none" onload="this.media='all';">
<link rel="stylesheet" type="text/css" href="style.css" media="none" onload="this.media='all';">

Essentially using media=none let's the browser download the stylesheet without blocking rendering, so when the stylesheet has loaded, setting the media to its desired setting will then render the screen as it should.  Nice!

Note: I've had a few issues with this technique within Firefox but I've not yet figured out the exact reasoning for it, as sometimes no issue occurs. Read Keith Clark's post for precise details and support.

3.  Separate Font Selectors

If a font hasn't loaded by the time it's used, the user will see (nothing, kind of) empty space until the font has loaded.  This is, of course, not good if the fonts fail to load.  At the very least, the user will be burdened with staring at empty space for a few seconds.  What's best is keeping custom font declarations protected with a class that is added to the body after the fonts have loaded:

h1 { font-family: Arial, serif; } /* basic system font */
.fontsloaded h1 { font-family:  'MySpecialFont', serif; } /* custom system font */
<link href="fonts.css" onload="document.body.className+=' fontsloaded';" rel="stylesheet" type="text/css" >

By using the font declaration strategy above, a system font is loaded initially and only after the custom font is loaded will it be enabled, thus the screen wont show empty content for any period of time.  I recommend creating a Stylus/Sass/Less mixin to set the font-family settings so that the custom selector is automatically set.

Scott Jehl describes that font load events are much more effective in his post Font Loading Revisited with Font Events. Please read it!

4. Storing Fonts in localStorage

Did you know you can store fonts in localStorage?! Crazy, right?! Check out this post which details the process and even provides the code to do it!

Speed kills on the web and the strategies provided above will supercharge your custom font loading and CSS speed.  The solutions are all easy to implement, you just need to take the time!

Recent Features

  • By
    How to Create a RetroPie on Raspberry Pi &#8211; Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

Incredible Demos

  • By
    jQuery UI DatePicker:  Disable Specified Days

    One project I'm currently working on requires jQuery. The project also features a datepicker for requesting a visit to their location. jQuery UI's DatePicker plugin was the natural choice and it does a really nice job. One challenge I encountered was the...

  • By
    HTML5 Datalist

    One of the most used JavaScript widgets over the past decade has been the text box autocomplete widget.  Every JavaScript framework has their own autocomplete widget and many of them have become quite advanced.  Much like the placeholder attribute's introduction to markup, a frequently used...

Discussion

  1. Michal Hewelt

    Thanks for the apache code example.
    It’s a small thing but helps a lot.

  2. Thanks ! Very cool!
    But, it must be one more – put css with font dataUri to locastorage.

    • Will it improve if we put css with font dataUri to locastorage?

    • Yes. Google page speed up to 100%. All have any disadvantages.

  3. Arnaud

    Very nice article, but I don’t really get it, does the “separate font selector” allow some sort of font lazy-loading?

    • Yes, it does. Browsers (i.e. smart browsers) won’t load the fonts when they are not used in a font-family property. Even when the font is used in a font-family property and the target elements do not exist, the font is not loaded.

  4. I don’t think media=none is a good way to download css as unstyled HTML appears on the webpage for a duration which is visible to naked eye.

    • > I don’t think media=none is a good way to download css as unstyled HTML appears on the webpage for a duration which is visible to naked eye.

      Understood, but that is why you only use media=none on the font stylesheet (use a separate stylesheet, it’s probably worth the extra request). You should style your site to be tolerable with out @fontface anyways, as some browsers like Opera Mini don’t load web fonts and never will.

      Think of it this way. Without deferring the font it will block the rendering of entire page. If I’m a blind user on a screen reader, why are you making me wait for content until something that isn’t relevant to me, and is a large file size, downloads?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!