A font is a collection of characters with a similar design. Each character is commonly referred to as a glyph.
The goal of the developer is to include a font on a page and ensure it loads quickly. Then developer must apply a font to desired elements.
Fonts come in a variety of formats. The most common are ttf
and woff
. However, for best performance, consider using the woff2
format. This format is currently only not supported in Internet Explorer, just like React, pretty much, thus, you can safely use it on projects.
<aside>
➡️ Do not have a woff2
font? Convert it using one of the following links:
The woff2
is multiple times more efficient than any other font format "on the market". Please use it under any circumstances.
The fonts may be considered a render-blocking resource. This could delay the page display. In order to avoid it, the powerful font-display
property is introduced. It allows controlling the way a font load on a page. The 3 font display values you will usually choose from:
block
- instructs the browser to briefly hide the text until the font has fully downloaded. More accurately, the browser draws the text with an invisible placeholder then swaps it with the custom font face as soon as it loads. This is also known as a “flash of invisible text” or FOIT.swap
- instructs the browser to use the fallback font to display the text until the custom font has been fully downloaded. This is also known as a “flash of un-styled text” or FOUT.fallback
- acts as a compromise between the auto
and swap
values. The browser will hide the text for about 100ms and, if the font has not yet been downloaded, will use the fallback text. It will swap to the new font after it is downloaded, but only during a short swap period (probably 3 seconds).<aside> ➡️ When fonts on your page blink, that is likely because the font sources fail to load in time. You can fix this by following:
swap
font display and playing with fallback fonts to match the desired font as close as possible.preconnect
links to the page in order to skip TLS handshake and DNS lookup while loading fonts.
</aside>All elements may use different font sizes, but there should be no more than 5 font faces on a page. Here are few tips to avoid a large number of fonts loaded on a page:
Eliminate all custom italic font faces. The browser can automatically generate them from non-italic fonts.
<aside> ➡️ Automatically generated italic font (faux italics) will differ from the original italic font. Please consider the frequency of occurrence of italic font. Is it worth loading an additional ±100kb on every page?
</aside>
Decrease the number of font weights on a page. Inspect the page for different font weights. If some font is used on a page just once, feel free to drop it.
<aside>
➡️ If not sure how to decrease the number of fonts on the page, please reach out to the company CTO via email ([email protected]
).
</aside>
A font face is a CSS declaration of a font. It declares a combination of the following properties:
font-family
- a font name to be used, followed by a list of fallback fontsfont-weight
- an amount of boldness the font should usefont-style
- a variation of your font (italic or not)font-display
- a way how the font will be displayed (How does font display work?)src
- a URL that should lead to a woff2
file to be used as a font sourceAn average font-face declaration may look as follows:
@font-face {
font-family: "Open Sans", sans-serif;
font-weight: normal;
font-display: swap;
src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2");
}
src/style/fonts
folderSaving to the src
folder will allow easier bundling of your resource. There will be no need to play with public URLs in font source declarations. What are font source declarations? Here is how to import your fonts into the project.
woff2
of your desired font. How to convert my font to woff2
?src/style/fonts
folder of your projectNow we need to override (How to override CSS files?) a few CSS files in order to register the newly imported fonts:
Override the src/style/base/_font.scss
file. Register your fonts there using the following template:
@font-face {
font-family: '<FONT FAMILY>';
src: url(/style/fonts/<NAME OF A FONT FILE #1>.woff2) format('woff2');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: '<FONT FAMILY>';
src: url(/style/fonts/<NAME OF A FONT FILE #2>.woff2) format('woff2');
font-weight: bold;
font-style: normal;
}
<FONT FAMILY>
- an family name of the font, i.e. SourceSansPro<NAME OF A FONT FILE #N>
- a name of the file. How to name a font file? For example sourcesanspro-regular-webfontOverride the src/style/main.scss
file, if not already. Copy the file from the parent (likely, ScandiPWA) theme.
Override the src/style/abstract/_abstract.scss
file, if not already. Copy the file from the parent (likely, ScandiPWA) theme.
Override the src/style/abstract/variables
file, if not already. Put the following content inside:
@import '../../../node_modules/@scandipwa/scandipwa/src/style/abstract/variables';
$font-<FONT NAME>: '<FONT FAMILY>', <FONT FALLBACK>;
<FONT NAME>
- a font name to reference when changing the appearance of elements.<FONT FAMILY>
- a family name of the font, i.e. SourceSansPro<FONT FALLBACK>
- a family name of the font fallback, i.e. sans-serifYou can now set the following in your styles, which should change the fonts of an element:
font-family: $font-<FONT NAME>;
<FONT NAME>
- a font name to reference when changing the appearance of elements.