Zach Leatherman wrote up a comprehensive list of font loading strategies that have been widely shared in the web development field. I took a look at this list before, but got so scared (and confused), that I decided not to do anything at all. I don’t know how to begin loading fonts the best way and I don’t want to feel like an idiot going through this list.
Today, I gave myself time to sit down and figure it out. In this article, I want to share with you the best loading strategies and how to execute all of them.
The best strategies
Zach recommends two strategies in his article:
- FOUT with Class – Best approach for most situations. (This works whether we use a font-hosting company or hosting our own fonts.)
- Critical FOFT – Most performant approach. (This only works if we host our own fonts.)
Before we dive into these two strategies, we need to clear up the acronyms so you understand what Zach is saying.
FOIT, FOUT, FOFT
The acronyms are as follows:
- FOIT means flash of invisible text. When web fonts are loading, we hide text so users don’t see anything. We only show the text when web fonts are loaded.
- FOUT means flash of unstyled text. When web fonts are loading, we show users a system font. When the web font gets loaded, we change the text back to the desired web font.
- FOFT means flash of faux text. This one is complicated and warrants more explanation. We’ll talk about it in detail when we hit the FOFT section.
Self-hosted fonts vs. cloud-hosted fonts
There are two main ways to host fonts:
- Use a cloud provider.
- Self-host the fonts.
How we load fonts differs greatly depending on which option you choose.
Loading fonts with cloud-hosted fonts
It’s often easier to cloud-hosted fonts. The provider will give us a link to load the fonts. We can simply plunk this link into our HTML and we’ll get our web font. Here’s an example where we load web fonts from Adobe Fonts now (previously known as Typekit).
<link rel="stylesheet" href="https://use.typekit.net/your-kit-id.css">
Unfortunately, this isn’t the best approach. The href
blocks the browser. If loading the web font hangs, users won’t be able to do anything while they wait.
Typekit used to provide JavaScript that loads a font asynchronously. It’s a pity they don’t show this JavaScript version anymore. (The code still works though, but I have no idea when, or if, it will stop working.)
Loading fonts from Google Fonts is slightly better because it provides font-display: swap
. Here’s an example where we load Lato from Google Fonts. (The display=swap
parameter triggers font-display: swap
).
<link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet"
/>
Loading fonts with self-hosted fonts
You can only self-host your fonts if you have the license to do so. Since fonts can be expensive, most people choose to use a cloud provider instead.
There’s a cheap way to get fonts though. Design Cuts runs font deals occasionally where you can get insanely high-quality fonts for just $29 per bundle. Each bundle can contain up to 12 fonts. I managed to get classics like Claredon, DIN, Futura, and a whole slew of fonts I can play around by camping on their newsletter for these deals.
If we want to self-host fonts, we need to understand two more concepts: @font-face
and font-display: swap
.
@font-face
@font-face
lets us declare fonts in CSS. If we want to self-host fonts, we need to use @font-face
to declare your fonts.
In this declaration, we can specify four things:
font-family
: This tells CSS (and JavaScript) the name of our font.src
: Path to find the font so they can get loadedfont-weight
: The font weight. Defaults to 400 if omitted.font-style
: Whether to italicize the font. Defaults to normal if omitted.
For src
, we need to specify several font formats. For a practical level of browser support, we can use woff2
and woff
.
Here’s an example where we load Lato via @font-face
.
@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;
}
If you don’t have woff2
or woff
files, you can upload your font files (either Opentype or Truetype) into a font-face generator to get them.
Next, we declare the web font in a font-family
property.
html { font-family: Lato, sans-serif;
}
When browsers parse an element with the web font, they trigger a download for the web font.
font-display: swap
font-display
takes one of four possible values: auto
, swap
, fallback
, and optional
. swap
instructs browsers to display the fallback text before web fonts get loaded. In other words, swap
triggers FOUT for self-hosted fonts. Find out about other values from in the CSS-Tricks almanac.
Browser support for font-display: swap
is pretty good nowadays so we can use it in our projects.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
60 | 58 | No | 79 | 11.1 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
88 | 85 | 81 | 11.3-11.4 |
FOUT vs. FOUT with Class
FOUT means flash of unstyled text. You always want FOUT over FOIT (flash of invisible text) because it’s better for users to read words written with system fonts compared to words written with invisible ink. We mentioned earlier that font-display: swap
gives you the ability to use FOUT natively.
FOUT with Class gives you the same results—FOUT—but it uses JavaScript to load the fonts. The mechanics are as follows:
- First: Load system fonts.
- Second: Load web fonts via JavaScript.
- Third: When web fonts are loaded, add a class to the
<html>
tag. - Fourth: Switch the system font with the loaded web font.
Zach recommends loading fonts via JavaScript even though font-display: swap
enjoys good browser support. In other words, Zach recommends FOUT with Class over @font-face
+ font-display: swap
.
He recommends FOUT with Class because of these three reasons:
- We can group repaints.
- We can adapt to user preferences.
- We can skip font-loading altogether if users have a slow connection.
I’ll let you dive deeper into the reasons in another article. While writing this article, I found a fourth reason to prefer FOUT with Class: We can skip font-loading when users already have the font loaded in their system. (We do this with sessionStorage
as we’ll see below.)
FOUT with Class (for cloud-hosted fonts)
First, we want to load our fonts as usual from your cloud-hosting company. Here’s an example where I loaded 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, we want to load fonts via JavaScript. We’ll inject a script
into the <head>
section since the code footprint is small, and it’s going to be asynchronous 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
, we want to use the CSS Font Loading API to load the font. Here, we can use Promise.all
to load all fonts simultaneously. When we do this, we’re grouping repaints.
The code looks like this:
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') })
}
When fonts are loaded, we want to change the body text to the web font. We can do this via CSS by using 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;
}
Pay attention here: We need to use the .fonts-loaded
class with this approach.
We cannot write the web font directly in the <body>
‘s font-family
; doing this will trigger fonts to download, which means you’re using @font-face + font-display: swap
. It also means the JavaScript is redundant.
/* DO NOT DO THIS */
body { font-family: Lato, sans-serif;
}
If users visit additional pages on the site, they already have the fonts loaded in their browser. We can skip the font-loading process (to speed things up) by using sessionStorage
.
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') }) }
}
If we want to optimize the JavaScript for readability, we can use an early return pattern to reduce indentation.
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()
FOUT with Class (for self-hosted fonts)
First, we want to load our fonts as usual via @font-face
. The font-display: swap
property is optional since we’re loading fonts via JavaScript.
@font-face { font-family: Lato; src: url('path-to-lato.woff2') format('woff2'), url('path-to-lato.woff') format('woff');
} /* Other @font-face declarations */
Next, we 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') })
}
Then we want to change the body text to the web font 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;
}
Finally, we skip font loading for 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') })
}
CSS Font Loader API vs. FontFaceObserver
The CSS Font Loader API has pretty good browser support, but it’s a pretty cranky API.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
35 | 41 | No | 79 | 10 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
88 | 85 | 81 | 10.0-10.2 |
So, if you need to support older browsers (like IE 11 and below), or if you find the API weird and unwieldy, you might want to use Bramstein’s FontFaceObserver
. It’s super lightweight so there’s not much harm.
The code looks like this. (It’s much nicer compared to the CSS Font Loader API).
new FontFaceObserver('lato') .load() .then(_ => { document.documentElement.classList.add('fonts-loaded') })
Make sure to use fontfaceobserver.standalone.js
if you intend to it to load fonts on browsers that don’t support Promises.
FOFT
FOFT means flash of faux text. The idea here is we split font loading into three stages:
- Step 1: Use fallback font when web fonts aren’t loaded yet.
- Step 2: Load the Roman (also called “book” or “regular”) version of the font first. This replaces most of the text. Bold and italics will be faked by the browser (hence “faux text”).
- Step 3: Load the rest of the fonts
Note: Zach also calls this Standard FOFT.
Using Standard FOFT
First, we load the Roman font.
@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, we load other fonts.
Pay attention here: we’re loading Lato
again, but this time, we set font-family
to Lato
instead of 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') }) })
}
Again, we can optimize it for repeat views.
Here, we can add fonts-loaded-2
to the <html>
straightaway since 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 comes from ‘critical css” (I believe) – where we load only essential CSS before loading the rest. We do this because it improves performance.
When it comes to typography, the only critical things are letters A to Z (both capitals and small letters). We can create a subset of these fonts with unicode-range
.
When we create this subset, we can also create a separate font file with the necessary characters.
Here’s what @font-face
declaration looks like:
@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;
}
We load this subset first.
.fonts-loaded-1 body { font-family: LatoSubset;
}
if('fonts' in document) { document.fonts.load('1em LatoSubset') .then(_ => { document.documentElement.classList.add('fonts-loaded-1') })
}
And we load other font files later.
.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') }) })
}
Again, we can optimize it for repeat views:
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 }) }) }
}
Critical FOFT variants
Zach proposed two additional Critical FOFT variants:
- Critical FOFT with Data URI
- Critical FOFT with Preload
Critical FOFT with data URIs
In this variant, we load the critical font (the subsetted roman font) with data URIs instead of a normal link. Here’s what it looks like. (And it’s scary.)
@font-face { font-family: LatoSubset; src: url("data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAABVwAA0AAAAAG+QAARqgAAAAAAAAAAAAAAAAAAAAAAAAAABHREVGAAABMAAAABYAAAAWABEANkdQT1MAAAFIAAACkQAAA9wvekOPT1MvMgAAA9wAAABcAAAAYNjUqmVjbWFwAAAEOAAAADgAAABEAIcBBGdhc3AAAARwAAAACAAAAAgAAAAQZ2x5ZgAABHgAAA8EAAAUUN1x8mZoZWFkAAATfAAAADYAAAA2DA2UbWhoZWEAABO0AAAAHgAAACQPOga/aG10eAAAE9QAAADIAAAA2PXwFPVsb2NhAAAUnAAAAG4AAABuhQSA721heHAAABUMAAAAGgAAACAAOgBgbmFtZQAAFSgAAAA0AAAANAI2Codwb3N0AAAVXAAAABMAAAAg/3QAegABAAAADAAAAAAAAAACAAEAAQA1AAEAAHicTZO/SxthGMe/d4k2NFbSFE2Maaq2tJ4/qtE4dwnBoUgoHUq5pWBBaCv0h4OCS2ML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9JmbSFMeGUhKbmpsTPgvlO80//hv8pKrZvKqrjd2SG4zxuZKT/mHNKj7JxIJtDUnNjK9AyQsLMUAAAAAAAAAKwBrALEA3QD0AQkBVgFtAXkBnwHYAecCIQJHAocCrwL8AzQDhgOYA8AD5QQtBGIEigSlBPcFLwVtBacF6QYYBpYGuQbgBxwHUQddB5oHwAf1CC4IaAiNCN4JEgk5CV4JqwnfCgoKKAAAeJxjYGRgYDBjiGVgZgABEI+JAQkAAA92AJsAAAAAAAIAHgADAAEECQABAAgAAAADAAEECQACAA4ACABMAGEAdABvAFIAZQBnAHUAbABhAHJ4nGNgZgCD/4UMVQxYAAAsjgHuAA==") format("woff"); /* ... */
}
This code looks scary, but there’s no need to be scared. The hardest part here is generating the data URI, and CSS-Tricks has us covered here.
Critical FOFT with preload
In this variant, we add a link with the preload
tag to the critical font file. Here’s what it looks like:
<link rel="preload" href="path-to-roman-subset.woff2" as="font" type="font/woff2" crossorigin>
We should only preload one font here. If we load more than one, we can adversely affect the initial load time.
In the strategy list, Zach also mentioned he prefers using a data URI over the preload
variant. He only prefers it because browser support for preload
used to be bad. Today, I feel that browser support is decent enough to choose preloading over a data URI.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
50 | 85 | No | 79 | 11.1 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
88 | 85 | 81 | 11.3-11.4 |
Final note from Zach
Chris ran this article via Zach and Zach wished he prioritized a JavaScript-free approach in his original article.
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
)
This switches the verdict a tiny bit, which I’m going to summarize in the next section.
Which font loading strategy to use?
If you use a cloud-hosted provider:
- Use
font-display: swap
if the host provides it. - Otherwise, use FOUT with class
If you host your web fonts, you have a few choices:
@font-face
+font-display: swap
- FOUT with Class
- Standard FOFT
- Critical FOFT
Here’s how to choose between them:
- Choose
@font-face
+font-display: swap
if you’re starting out and don’t want to mess with JavaScript. It’s the simplest of them all. Also choose this option if you use only few font files (fewer than two files) for each typeface. - Choose Standard FOFT if you’re ready to use JavaScript, but don’t want to do the extra work of subsetting the Roman font.
- Choose a Critical FOFT variant if you want to go all the way for performance.
That’s it! I hope you found all of this useful!
If you loved this article, you may like other articles I wrote. Consider subscribing to my newsletter 😉. Also, feel free to reach out to me if you have questions. I’ll try my best to help!
The post The Best Font Loading Strategies and How to Execute Them appeared first on CSS-Tricks.
You can support CSS-Tricks by being an MVP Supporter.