Using a font that is not built into operating systems can cause layout shifts and significantly decrease user experience.

Luckily - it's something rather easy to fix!

🧵 Let's break this problem down and see how to solve it!

#webperf #javascript
When you use a font that is not available in the operating system the browser has to download it.

This leads to one of two ways how it's handling the time between the page load and the font load:

- Flash of Invisible Text (FOIC)
- Flash of Unstyled Text (FOUC)
Flash of Invisible Text (FOIT)

As the name suggests, FOIT happens when the browser waits for the desired font to be downloaded until displaying any text.

This method significantly decreases user experience when the network is slow and generally should be avoided. Image
Instead its much better to display fallback font that is already available immediately and swap it with the desired one once it is available.

This delivers a much better experience because users can see the text as soon as the page loads.
To prevent FOIT and fallback to system fonts, you should use a "font-display" property.

It tells the browser how to handle the time between the page load and font load if the desired font is unavailable.

The recommended "font-display" value for best performance is "optional". Image
This is not solving the whole problem, though!

In many (almost every) cases, the fallback font occupies a different amount of space than the desired font.

This is exactly what causes FOUT and increases your Cumulative Layout Shift, one of the key performance metrics!
To deal with FOUT we have to ensure that the fallback font occupies exactly the same space as the desired font. This will require us to play with a few CSS properties.

Doing it with *just* CSS can be a nightmare tho.
A tool called Font Matcher from Google engineer Monica Dinculescucan can save you hours of unpleasant work, tears, and meltdowns.

meowni.ca/font-style-mat…
It can get even easier!

You can automate the whole process of generating the font fallback and required CSS classes by using the amazing package from @danielcroe called Fontaine

github.com/unjs/fontaine
And if you're using @nuxtjs there is a Nuxt module in the making:

github.com/danielroe/nuxt…

If you end up using the package don't forget to say something nice to Daniel (or sponsor him)!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Filip Rakowski 🇺🇦

Filip Rakowski 🇺🇦 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(