Choosing a Font Loading Approach

Zach Leatherman's comprehensive list of font loading strategies recommends two primary approaches:

  1. FOUT with Class — the best approach for most situations; works with both brand hosting companies and self-hosted fonts.
  2. Critical FOFT — the most performant approach; requires self-hosting.

Understanding the approach first requires understanding the acronyms:

  • FOIT — flash of invisible text: text is hidden while web fonts load, and only shown once they arrive.
  • FOUT — flash of unstyled text: a system font is shown during loading and swapped to the web font afterward.
  • FOFT — flash of faux text: covered in detail later.

Cloud-Hosted vs. Self-Hosted Fonts

Fonts can come from a cloud provider or be self-hosted, and the loading mechanics differ greatly between the two. Self-hosting requires a license; since fonts can be expensive, most people default to a cloud provider (deal sites that bundle fonts occasionally exist as a cheaper alternative).

The Cloud Provider Problem

Dropping a provider's link into the HTML is easy — here's an example loading from Adobe Fonts (formerly Typekit):

<link rel="stylesheet" href="https://use.typekit.net/your-kit-id.css">

The problem is that the href blocks the browser: if the font load hangs, users can't do anything while they wait. Typekit previously offered JavaScript to load fonts asynchronously (the code still works, though it's no longer documented).

Google Fonts is slightly better because it provides font-display: swap via the display=swap parameter:

<link
  href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,400;0,700;1,400;1,700&display=swap"
  rel="stylesheet"
/>

Declaring Self-Hosted Fonts

Self-hosted fonts must be declared with @font-face, which takes four specifications:

  • font-family — the name CSS and JavaScript use to reference the font
  • src — the font path
  • font-weight — defaults to 400 if omitted
  • font-style — defaults to normal if omitted

For practical browser support, specify both woff2 and woff sources. Opentype or Truetype files without either format can be run through a font-face generator.

@font-face {
  font-family: Lato;
  src: url('path-to-lato.woff2') format('woff2'),
       url('path-to-lato.woff') format('woff');
}

@font-face {
  font-family: Lato;
  src: url('path-to-lato-bold.woff2') format('woff2'),
       url('path-to-lato-bold.woff') format('woff');
  font-weight: 700;
}

@font-face {
  font-family: Lato;
  src: url('path-to-lato-italic.woff2') format('woff2'),
       url('path-to-lato-italic.woff') format('woff');
  font-style: italic;
}

@font-face {
  font-family: Lato;
  src: url('path-to-lato-bold-italic.woff2') format('woff2'),
       url('path-to-lato-bold-italic.woff') format('woff');
  font-weight: 700;
  font-style: italic;
}

Then declare the web font in a font-family property:

html {
  font-family: Lato, sans-serif;
}

Browsers begin downloading the font when they parse an element that uses it.

The font-display Property

font-display accepts auto, swap, fallback, or optional. The swap value shows fallback text before web fonts load, which produces FOUT natively for self-hosted fonts. Browser support for font-display: swap is solid today and safe to use in projects.

FOUT with Class in Detail

FOUT with Class produces the same visible outcome as FOUT, but loads fonts with JavaScript:

  • Load system fonts first.
  • Load web fonts via JavaScript.
  • When they finish loading, add a class to the <html> tag.
  • Swap the system font for the web font.

Zach recommends this over @font-face + font-display: swap despite the latter's browser support, for three reasons: repaints can be grouped, user preferences can be respected, and font loading can be skipped entirely on slow connections. A fourth reason surfaced while writing this: loading can be skipped when users already have the font (handled via sessionStorage).

Implementation with a Cloud Provider

Load the fonts from the provider as usual — here's Lato from Google Fonts:

<head>
  <link
    href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,400;0,700;1,400;1,700&display=swap"
    rel="stylesheet"
  />
</head>

Next, load the fonts via JavaScript by injecting a small script into <head> (it runs asynchronously anyway):

<head>
  <link
    href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,400;0,700;1,400;1,700&display=swap"
    rel="stylesheet"
  />
  <script src="js/load-fonts.js"></script>
</head>

In load-fonts.js, use the CSS Font Loading API with Promise.all to load all fonts simultaneously, which groups the repaints:

if ('fonts' in document) {
  Promise.all([
    document.fonts.load('1em Lato'),
    document.fonts.load('700 1em Lato'),
    document.fonts.load('italic 1em Lato'),
    document.fonts.load('italic 700 1em Lato')
  ]).then(_ => () {
    document.documentElement.classList.add('fonts-loaded')
  })
}

Switch the body text to the web font through the .fonts-loaded class:

/* System font before [[webfont]]s get loaded */
body {
  font-family: sans-serif;
}

/* Use [[webfont]] when [[webfont]] gets loaded*/
.fonts-loaded body {
  font-family: Lato,  sans-serif;
}

Important: the .fonts-loaded class is mandatory with this approach. Writing the web font directly in the <body>'s font-family triggers the download anyway — putting you in @font-face + font-display: swap territory and making the JavaScript redundant.

/* DO NOT DO THIS */
body {
  font-family: Lato, sans-serif;
}

For repeat visits within the same session, sessionStorage lets you skip the loading process, since the fonts are already in the browser:

if (sessionStorage.fontsLoaded) {
  document.documentElement.classList.add('fonts-loaded')
} else {
  if ('fonts' in document) {
  Promise.all([
    document.fonts.load('1em Lato'),
    document.fonts.load('700 1em Lato'),
    document.fonts.load('italic 1em Lato'),
    document.fonts.load('italic 700 1em Lato')
  ]).then(_ => () {
    document.documentElement.classList.add('fonts-loaded')
  })
  }
}

An early-return pattern reduces indentation for readability:

function loadFonts () {
  if (sessionStorage.fontsLoaded) {
    document.documentElement.classList.add('fonts-loaded')
    return 
  } 
  
if ('fonts' in document) {
  Promise.all([
    document.fonts.load('1em Lato'),
    document.fonts.load('700 1em Lato'),
    document.fonts.load('italic 1em Lato'),
    document.fonts.load('italic 700 1em Lato')
  ]).then(_ => () {
    document.documentElement.classList.add('fonts-loaded')
  })  
}

loadFonts()

Implementation with Self-Hosted Fonts

Declare the fonts via @font-face (font-display: swap is optional here since JavaScript handles loading):

@font-face {
  font-family: Lato;
  src: url('path-to-lato.woff2') format('woff2'),
       url('path-to-lato.woff') format('woff');
}

/* Other @font-face declarations */

Load the web fonts via JavaScript:

if ('fonts' in document) {
  Promise.all([
    document.fonts.load('1em Lato'),
    document.fonts.load('700 1em Lato'),
    document.fonts.load('italic 1em Lato'),
    document.fonts.load('italic 700 1em Lato')
  ]).then(_ => () {
    document.documentElement.classList.add('fonts-loaded')
  })
}

Switch the body text via CSS:

/* System font before webfont is loaded */
body {
  font-family: sans-serif;
}

/* Use webfont when it loads */
.fonts-loaded body {
  font-family: Lato,  sans-serif;
}

And skip font loading on repeat visits:

if ('fonts' in document) {
  Promise.all([
    document.fonts.load('1em Lato'),
    document.fonts.load('700 1em Lato'),
    document.fonts.load('italic 1em Lato'),
    document.fonts.load('italic 700 1em Lato')
  ]).then(_ => () {
    document.documentElement.classList.add('fonts-loaded')
  })
}

The CSS Font Loading API vs. FontFaceObserver

The CSS Font Loading API has good browser support, but it's a cranky, unwieldy API. For older browsers (IE 11 and below) or when the API feels awkward, Bramstein's FontFaceObserver is a lightweight alternative:

new FontFaceObserver('lato')
  .load()
  .then(_ => {
    document.documentElement.classList.add('fonts-loaded')
  })

If targeting browsers without Promise support, use fontfaceobserver.standalone.js.

The FOFT Strategies

FOFT splits loading into three stages: fall back first, then load the Roman ("book"/"regular") version so most text swaps, with the browser faking bold and italics ("faux text"), and finally load the remaining styles.

Standard FOFT

Load the Roman font first:

@font-face {
  font-family: LatoInitial;
  src: url('path-to-lato.woff2') format('woff2'),
       url('path-to-lato.woff') format('woff');
  unicode-range: U+65-90, U+97-122;
}

.fonts-loaded-1 body {
  font-family: LatoInitial;
}
if('fonts' in document) {
  document.fonts.load("1em LatoInitial")
    .then(_ => {
      document.documentElement.classList.add('fonts-loaded-1')
    })
}

Then load the other fonts. Note that Lato is loaded again, but this time font-family is set to Lato rather than LatoInitial:

/* Load this first */
@font-face {
  font-family: LatoInitial;
  src: url('path-to-lato.woff2') format('woff2'),
       url('path-to-lato.woff') format('woff');
  unicode-range: U+65-90, U+97-122;
}

/* Load these afterwards */
@font-face {
  font-family: Lato;
  src: url('path-to-lato.woff2') format('woff2'),
       url('path-to-lato.woff') format('woff');
  unicode-range: U+65-90, U+97-122;
}

/* Other @font-face for different weights and styles*/

.fonts-loaded-1 body {
  font-family: LatoInitial;
}

.fonts-loaded-2 body {
  font-family: Lato;
}
if ('fonts' in document) {
  document.fonts.load('1em LatoInitial')
    .then(_ => {
      document.documentElement.classList.add('fonts-loaded-1')

      Promise.all([
        document.fonts.load('400 1em Lato'),
        document.fonts.load('700 1em Lato'),
        document.fonts.load('italic 1em Lato'),
        document.fonts.load('italic 700 1em Lato')
      ]).then(_ => {
        document.documentElement.classList.add('fonts-loaded-2')
      })
    })
}

For repeat views, fonts-loaded-2 can be added to <html> immediately since the fonts are already loaded:

function loadFonts () {
  if (sessionStorage.fontsLoaded) {
    document.documentElement.classList.add('fonts-loaded-2')
    return
  }

  if ('fonts' in document) {
    document.fonts.load('1em Lato')
      .then(_ => {
        document.documentElement.classList.add('fonts-loaded-1')

        Promise.all([
          document.fonts.load('400 1em Lato'),
          document.fonts.load('700 1em Lato'),
          document.fonts.load('italic 1em Lato'),
          document.fonts.load('italic 700 1em Lato')
        ]).then(_ => {
          document.documentElement.classList.add('fonts-loaded-2')

          // Optimization for Repeat Views
          sessionStorage.fontsLoaded = true
        })
      })
  }
}

Critical FOFT

The "critical" part derives from "critical CSS": load only the essentials before the rest. Typographically, the essentials are letters A–Z, both cases, which can be subset with unicode-range into a separate font file:

@font-face {
  font-family: LatoSubset;
  src: url('path-to-optimized-lato.woff2') format('woff2'),
       url('path-to-optimized-lato.woff') format('woff');
  unicode-range: U+65-90, U+97-122;
}

The subset is loaded first, then the other font files afterward, with repeat views optimized the same way:

.fonts-loaded-1 body {
  font-family: LatoSubset;
}
if('fonts' in document) {
  document.fonts.load('1em LatoSubset')
    .then(_ => {
      document.documentElement.classList.add('fonts-loaded-1')
    })
}
.fonts-loaded-2 body {
  font-family: Lato;
}
if ('fonts' in document) {
  document.fonts.load('1em LatoSubset')
    .then(_ => {
      document.documentElement.classList.add('fonts-loaded-1')

      Promise.all([
        document.fonts.load('400 1em Lato'),
        document.fonts.load('700 1em Lato'),
        document.fonts.load('italic 1em Lato'),
        document.fonts.load('italic 700 1em Lato')
      ]).then(_ => {
        document.documentElement.classList.add('fonts-loaded-2')
      })
    })
}
function loadFonts () {
  if (sessionStorage.fontsLoaded) {
    document.documentElement.classList.add('fonts-loaded-2')
    return
  }

  if ('fonts' in document) {
    document.fonts.load('1em LatoSubset')
      .then(_ => {
        document.documentElement.classList.add('fonts-loaded-1')

        Promise.all([
          document.fonts.load('400 1em Lato'),
          document.fonts.load('700 1em Lato'),
          document.fonts.load('italic 1em Lato'),
          document.fonts.load('italic 700 1em Lato')
        ]).then(_ => {
          document.documentElement.classList.add('fonts-loaded-2')

          // Optimization for Repeat Views
          sessionStorage.fontsLoaded = true
        })
      })
  }
}

Zach proposed two Critical FOFT variants: Critical FOFT with Data URI and Critical FOFT with Preload. In the first, the subsetted Roman font is embedded via a data URI rather than linked. It looks intimidating, but the hardest part is generating the data URI. In the second, a link with the preload tag points at the critical font file:

<link rel="preload" href="path-to-roman-subset.woff2" as="font" type="font/woff2" crossorigin>

Only one font should be preloaded; preloading more than one can adversely affect initial load time. Zach originally preferred the data URI variant only because preload support used to be poor, and support is now decent enough that preloading is a defensible choice over a data URI.

Zach's Updated Guidance

Asked to review the article, Zach said he wished he had prioritized a JavaScript-free approach in his original:

I think the article is good but I think my article that its based off of is probably a little dated in a few ways. I wish it prioritized no-JS approaches more when you're only using one or two font files (or more but only 1 or 2 of each typeface). The JS approaches are kind of the exception nowadays I think (unless you're using a lot of font files or a cloud provider that doesn't support font-display: swap)

Decision Guide

With a cloud-hosted provider:

  • Use font-display: swap if the host provides it.
  • Otherwise, use FOUT with Class.

When self-hosting, the options are @font-face + font-display: swap, FOUT with Class, Standard FOFT, and Critical FOFT. Choose between them as follows:

  • @font-face + font-display: swap — if you're starting out and want to avoid JavaScript entirely; also the choice when a typeface uses fewer than two font files. It's the simplest option of all.
  • Standard FOFT — if you're ready to use JavaScript but don't want the extra work of subsetting the Roman font.
  • A Critical FOFT variant — if you're going all the way for performance.