10 ways to speed up web font loading
By Jacob Jackson on February 11, 2022 (Updated August 18, 2022)
Fonts are popular tools on the web nowadays. Most modern websites use custom fonts, usually from Google Fonts, because they add a custom touch to the interface and allow for more options. However, there are many performance implications with fonts on the web. Fonts are often hundreds of kilobytes and are hosted on another domain, slowing down the website. Luckily, you can solve these problems. Here are ten tips to improve font loading performance on a website.
How to make web fonts load faster
1. Self host your fonts
When you go on a website and look at the HTML, you often see something like this:
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Roboto&display=swap"
rel="stylesheet"
/>
That means the website is using Google Fonts to load fonts. Google Fonts is excellent for prototyping, as it is straightforward. Sadly, it is terrible for performance. Performance is bad because Google Fonts has a complicated string of requests to get a font, which adds significant latency.
As you can see, the font loading with Google Fonts can be complicated. However, you can reduce how many requests are needed to just one by self-hosting the font and putting the @font-face
declaration inside your CSS bundle instead of an external stylesheet. A tool like Google Webfonts Helper can be helpful for this.
2. Use modern web font formats
Fonts are like images on the web because of how their formats work. With images, modern web formats like AVIF and WebP replaced less compressed formats like PNG and JPEG. Similarly, web fonts have WOFF and WOFF2, which provide superior compression to formats like TrueType and OpenType (TTF and OTF). Additionally, web fonts are almost universally supported:
At this point, because of the almost universal browser support, the best thing to do is use WOFF2 with a WOFF fallback, although you could even just use WOFF2, as ~96% of users support it.
3. Subset your fonts
Font subsetting means trimming your fonts down to only the characters you need. Subsetting can yield massive amounts of size savings without having any drawbacks as long as you are not using the characters you remove. The simplest way to do this is subsetting to remove languages that you do not use. For example, take Inter, one of the most popular fonts. If you include all languages, which includes the Latin alphabet, Cyrillic, Vietnamese, and Greek, the size of the WOFF2 font is 95kb. However, chances are you are not using all of those languages. If you remove all characters outside of the English language, the size is reduced to just 16kb! There are many ways to subset fonts, including Google Fonts (and by extension, Google Webfonts Helper), Everything Fonts, and fontTools.
The size reduction is beneficial for performance, but you can do even better if you are willing to take on more complexity. Instead of subsetting by wide character ranges, you can subset to include precisely what you use. Tools like fontTools allow you to subset fonts by arbitrary character lists. The problem with this approach is that you need a complicated build process to get every character you use. Or, of course, you can manually add each character, but that is a lot of work š.
4. Use font-display
By default, text will not appear in many browsers until the correct font is loading. That behavior is called Flash of Invisible Text, or FOIT. Some other browsers display text using the fallback font while it is loading, ensuring text is not invisible but can cause a layout shift.
Luckily, you can customize font loading through the font-display
option in your @font-face
declaration. The two recommended values for font-display
are swap
and optional
. swap
uses a fallback font and then switches to the custom font once it is loaded, and optional
blocks page load for a maximum of 100ms to let the font load, and if it does not, it uses a fallback font.
@font-face {
font-family: "Example";
font-style: normal;
font-weight: 400;
src: local("Example"), local("Example"),
url(https://example.com/example.woff2) format("woff2");
font-display: swap;
}
font-display: optional
is the best for when you donāt need the font to be loaded because it prevents any layout shift and ensures the text is not invisible. However, font-display: swap
is best when you need the font to load because it swaps in the font even if it takes more than 100ms.
5. Match your fallback font with your custom font
Fonts are usually spaced and sized differently. For example, Merriweather is larger than Georgia, even if the CSS font size is the same. These inconsistencies can cause a layout shift if you use font-display: swap
and make fonts less consistent if you use font-display: optional
. Luckily, you can configure your fallback font to look much more like the web font you are using. Matching both fonts can be done by customizing spacing and font size to remove the inconsistencies. A helpful tool for this is Font Style Matcher, which allows you to look at two different fonts, configure various spacing properties, and see a demo of the layout shift.
6. Use a CDN
CDNs are great for speeding up static content delivery. They deliver content closer to your users and often offer other ways of speeding up delivery. Hosting your fonts on a CDN with all your other static assets is a good idea. Using a CDN also reduces server costs, as CDNs are generally cheaper compared to serving the request from your source server.
7. Preload your fonts
Preloading is often a good idea for various resources, as it makes the browser know it needs to download a resource sooner and increases that resourceās priority. It is the same idea for fonts. However, you do not always want to preload your fonts because it might make a font load unnecessarily. That is because the browser automatically detects whether the website uses a font on the page before downloading it, even if the @font-face
is present. Now, you might know that you always use the fonts you include with @font-face
. There is one other potential problem. If your font is not immediately needed, the drawbacks of making other things load slower might outweigh the benefits. However, if both of these things are false, preloading can be a great way to speed up your font loading.
8. Use local()
Depending on the font, users can often have the font you are using locally installed on their machine. If they do, you can easily use the font locally to prevent any performance degradation from a custom font. You do this using a local()
statement in your @font-face
src
rule.
@font-face {
font-family: "Awesome Font";
font-style: normal;
font-weight: 400;
src: local("Awesome Font"),
url("example.com/awesome-font.woff2") format("woff2");
}
This @font-face
checks if the user has the font locally, and if they do not, downloads it remotely. That means that users with the font get a free performance boost while adding little complexity to the code and no disadvantage to users who do not have the font locally. If you want to focus on fonts that computers usually have, you can check out Windows 11ās font list and macOS Catalinaās font list.
9. Implement Caching
Caching is very important, especially if you have lots of repeat visitors. Caching allows the font to load from the disk after the first time it is downloaded. You can implement caching through the Cache-Control
header. If you use a CDN like Cloudflare, it is simple to do from the dashboard. Otherwise, you can do it by simply sending the header with your font responses.
10. Donāt use custom fonts
I bet you didnāt see that one coming š. I am talking about system font stacks. System fonts are great for body text or other text that isnāt important for branding, as it is built-in and makes the interface more comfortable because it is the same font as the operating system.
You can implement system UI fonts through the system font stacks linked above or the system-ui
font family.
Conclusion
That is all! I hope you enjoyed learning how to optimize your web fonts, and I hope this has helped you optimize fonts on your websites. If you enjoyed reading this, sign up for the mailing list below and subscribe to RSS. Thanks for reading!