Once the custom font is ready, the system font is swapped out. The swap value tells the browser that text using this font should be displayed immediately using a system font. In the previous example you’ll notice the font-displaydeclaration. You can avoid the “flash” and show content to users immediately using a system font initially, then replacing it. To deal with this, some browsers hide text until the font loads (the “flash of invisible text”). Resourcesįonts are often large files that take a while to load even when gzipped. If you really want to use eot and ttf fonts make sure to add them at the end of the src declaration.
#How to use font glyphs in a web page download
If it supports the woff2 format it will download the font, or if it doesn’t recognise the resource format it will proceed to the next one, and so on.
The font declaration order is also important because the browser will start fetching the resources by following the declaration order. think about the Roboto fonts that are pre-installed on Android) instead of downloading it. The local() function allows users to use their local copy of the font if present (e.g.
There are also two more things to note, the local() function and the font declaration order. Here’s the Unicode range from Google Web Fundamentals.Īs you can see we use only optimised fonts ( woff and woff2) and we tell the browser to load only the required glyphs range (from U+000 to U+5FF), but this property doesn’t prevent browsers to download the entire font. Here a correct example declaring a custom font family: Correct font-face declarationĭeclaring a font-face family is very simple but we must take care with certain things when we do it. Consider using ?? instead for easier maintenance. Fonts can also be regularly updated, and if you’re preloading an old version while using the CSS for a newer one, you may end up downloading two versions of the same font and wasting your users’ bandwidth. If you’re using a CDN like Google Fonts, be sure that the font files you’re preloading match the ones in the CSS. You can instead use the rel="prefetch" attribute to tell the browser to prepare the download of the resources that may be required later during page load or user actions so it will assign a low priority to those resources. Browsers that support preload and prefetch hints will start downloading web fonts as soon as they have seen the hint in the HTML file and no longer need to wait for the CSS. Using the preload hints will have a dramatic impact on web font performance and initial page load. They also tell the browser that this is a font, so it can appropriately prioritise it in its resource queue. In the above example, the rel="preload" as="font" attributes will ask the browser to start downloading the required resource as soon as possible.
This is because fonts are expected to be fetched anonymously by the browser, and the preload request is only made anonymous by using the this attribute. Note that the use of crossorigin here is important without this attribute, the preloaded font is ignored by the browser, and a new fetch takes place. When using custom fonts you should tell the browser to preload them using the appropriate rel="" tag and attributes: These are the only two file types you should use because they are compressed in the gzip format by default (so they are very small), are optimised for the web, and are fully supported by IE 9+ and all other evergreen browsers. There are many font formats that can be used on the web, but only two formats are really needed if you don’t have to support Internet Explorer (IE) 8 or lower: woff and woff2. Let’s break down these points one at a time.