This document provides code snippets and instructions for working with and implementing fonts, including overrides and font faces. It includes examples and implementations of font variables and font styles.
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
swap
fallback
<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?) 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>;
Fonts can be seen throughout the entire application, as they define the appearance of each character on the page!
Developers should only receive fonts used in designs. Otherwise, they will include unused fonts onto a page and they will never be used. This will affect the page performance. The developer will blame you for that.
The page should not have many font faces loaded. 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.
You can use the Figma text styles feature (How to use text styles?) in order to quickly glance over all used fonts (if you of course have configured and used this feature), or you can use the Font Fascia plugin to list all used fonts throughout the project.
<aside> ℹ️ Usually, styling this element takes from-to: 2-4h
</aside>
Fonts can be visible in the typography section.
Import a new font into the application to match your theme, and create SCSS variables for using that font.
<aside> ➡️ Important: never ever should the same font-face but different weights are different font families!
</aside>
You need to add your fonts in the /src/style/fonts
directory (just simply copy the font files in here, rename them if appropriate), then you will need to define your font faces in /src/style/base/_font.scss
:
@font-face { // Medium
font-family: 'Recoleta';
src: URL(../fonts/Recoleta-Medium.woff2) format('woff2'),
URL(../fonts/Recoleta-Medium.woff) format('woff');
font-weight: 500;
font-style: normal;
font-display: swap;
}
Note that we have given 2 sources because .woff2
fonts might not always work, and so it’s better to have a fallback.
Make sure not to give a different font-family
name to a different weight of the same font!
The correct way to add a differently weighted font in the same family:
@font-face { // Bold
font-family: 'Recoleta';
src: URL(../fonts/Recoleta-Bold.woff2) format('woff2'),
URL(../fonts/Recoleta-Bold.woff) format('woff');
font-weight: 700;
}
This way, you’ll be able to just simply change the font-weight
property to 700, and the font from the Recoleta-Bold.woff2
file would be used!
You should also define some variables to be able to refer to the same font across the project.
Here, the value of the variable must be the name of the font face. In this case, it will be 'Recoleta'
, as we have named our font: font-family: 'Recoleta'
above.
It’s also a good idea to provide a fallback font family here.