When does a browser request to download a font?
By default, all fonts are lazily loaded on the browsers and the request to download fonts is only initiated during the construction of CSSOM (CSS Object Model) which happens only after the creation of DOM (Document Object Model).
While parsing a CSS file, if a browser finds a custom font is used, it will make an HTTP request to download the font either on the same server or on a cross-origin server depending on what is provided on the src attribute of @font-face.
Let's take an example where we have an index.html and main.css
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Preloading Fonts</title>
<link rel="stylesheet" href="main.css" as="style" />
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
</p>
</body>
</html>
main.css
@font-face {
font-family: Tangerine;
src: url("Tangerine-Regular.ttf");
}
p { font-family: "Tangerine";}
For example, in the case of Chrome, we can observe the following actions:
Note: crossorigin must be added on the link tag below even if the font is served from the same origin.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Preloading Fonts</title>
<link rel="preload" href="Tangerine-Regular.ttf" as="font" crossorigin />
<link rel="stylesheet" href="main.css" as="style" />
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
</p>
</body>
</html>
However, if the latency to download the font is very much high even after we start the download process early in the stage, then preloading will also not help and we need to look for another alternative which we will discuss shortly.
Note: If all the resources are on the same origin server, then this is not useful.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Preloading Fonts</title>
<link rel="dns-prefetch" href="https://example.com" />
<link rel="stylesheet" href="main.css" as="style" />
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
</p>
</body>
</html>
We can use the font-display CSS property. If we want to get rid of the FOIT completely, we can use swap value. With this, it is guaranteed that we will see the system font right away without the flash of invisible text and when the custom font gets downloaded, it will swap the system font (FOUT).
@font-face {
font-family: Tangerine;
src: url("Tangerine-Regular.ttf");
font-display: swap; /*auto | block | optional | fallback | optional*/
}
p {
font-family: "Tangerine";
}There are 5 different possible values of font-display property, and different browsers have their specific block and swap period for each of these values (See the table below).
Following are some of the ways to optimize the loading and rendering of web fonts which ultimately improves the loading speed of the web application.
I also have a video of this blog post on youtube.
https://www.youtube.com/watch?v=wnpMeYARV4g&ab_channel=FrontendMentor
Find out if MentorCruise is a good fit for you – fast, free, and no pressure.
Tell us about your goals
See how mentorship compares to other options
Preview your first month