You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/blog/2022-04-08-adding-fonts.md
+16-14Lines changed: 16 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,13 +11,14 @@ hide_table_of_contents: false
11
11
Every project is unique. Logo, colors, fonts, etc. are what define the visual identity of your website. In this post, we'll cover adding fonts to a project.
12
12
<!--truncate-->
13
13
14
-
### Importing fonts into your project
15
-
[Our documentation](/docs/basics/fonts) covers the necessary steps to add a font to your project, but here we'll cover the process in a bit more detail. To start, we need a font (or two). For this example, I'll use **_SourceSansPro_** and **_NotoSerif_** which I've downloaded from [Google Fonts](https://fonts.google.com/). These are in the `.ttf` format, so convert them to `.woff` and `.woff2. Whatever approach you use is okay - we recommend the following tools:
16
-
-https://convertio.co/ttf-woff/
17
-
-https://cloudconvert.com/ttf-to-woff
18
-
-https://everythingfonts.com/ttf-to-woff
19
-
20
-
Fonts should go inside your theme's **_/assets/fonts_** folder. Copy the fonts you want to use there. You will also notice that this folder contains an **_index.js_** file. This file is used to import fonts into your project. Here's an example of how I imported my fonts:
14
+
## Importing fonts into your project
15
+
[Our documentation](/docs/basics/fonts) covers the necessary steps to add a font to your project, but here we'll cover the process in a bit more detail. To start, we need a font (or two). For this example, I'll use **_Source Sans Pro_** and **_Noto Serif_** which I've downloaded from [Google Fonts](https://fonts.google.com/). They are in the `.ttf` format (you may find some which are `.otf`, which will work in the same way), so you need to convert them to `.woff` and `.woff2`. Whatever approach you use is okay - we recommend the following tools:
Fonts should go inside your theme's **_/assets/fonts_** folder. Copy the fonts you want to use there. You will also notice that this folder contains an **_index.js_** file, used to import fonts into your project. Here's an example of how I imported my fonts:
21
22
```js
22
23
// SourceSansPro WOFF
23
24
import'./SourceSansPro-Bold.woff';
@@ -73,8 +74,9 @@ Next, you can create a new file called **__typography.scss_** inside your **_/as
If you would like to know more about the `font-face` mixin, you can take a look at our [Sass documentation](/sass).
76
78
77
-
Because this is a new file, we have to include it. You can do that inside **_/assets/styles/parts/_shared.scss_** file:
79
+
Because this is a new file, we have to include it. You can do that inside **_/assets/styles/parts/\_shared.scss_** file:
78
80
```scss
79
81
// Project specific.
80
82
@import'utils/shared-variables';
@@ -83,28 +85,28 @@ Because this is a new file, we have to include it. You can do that inside **_/as
83
85
84
86
Run `npm start` to rebuild your **_public_** folder and assets. If you did everything correctly, your build should pass and you will see your fonts inside the **_public_** folder.
85
87
86
-
###Using only one font in a block
88
+
## Using only one font in a block
87
89
88
90
There are multiple ways of using fonts in a block. The simplest example is if you have only one font you want to use for that specific block. In this case, we want the Heading block to only use _Noto Serif_.
89
91
90
-
To make our secondary font available for use, we need to first define it as a variable. We can do that in **_/assets/styles/parts/utils/_shared-variables.scss_**. We can see there that the base font is already defined, so all we need to do is add our secondary font below.
92
+
To make our secondary font available for use, we need to first define it as a variable. We can do that in **_/assets/styles/parts/utils/\_shared-variables.scss_**. We can see there that the base font is already defined, so all we need to do is add our secondary font below.
91
93
92
-
To make things a bit more consistent, we may also want to rename `--global-font-family` CSS variable to `--base-font-family`. Just don't forget to search/replace this new variable name across your project! Once we're done, it should look like this:
94
+
To make things a bit more consistent, we may also want to rename `--global-font-family` CSS variable to `--base-font-family`. Just don't forget to search/replace this new variable name across your project! Please note that the fallbacks for the fonts can be anything, this is just an example. Once we're done, it should look like this:
After defining `--secondary-font-family` CSS variable, we can go to **_/src/Blocks/components/heading/heading-style.scss_** and add the following rule to the `.heading` class:
102
+
After defining the `--secondary-font-family` CSS variable, we can go to **_/src/Blocks/components/heading/heading-style.scss_** and add the following rule to the `.heading` class:
101
103
```scss
102
104
font-family: var(--secondary-font-family);
103
105
```
104
106
105
107
And that's it! The Heading block will now use the _Noto Serif_ font.
106
108
107
-
###Adding an option for selecting fonts
109
+
## Adding an option for selecting fonts
108
110
109
111
In some cases, you may want to give users the option to choose between fonts that they want to use in their block. For this example, we'll use the `paragraph` block where we want users to have both _Source Sans Pro_ and _Noto Serif_ available.
110
112
@@ -189,7 +191,7 @@ The control for selecting a font should now be available under Paragraph options
189
191
190
192
After adding this single line of CSS code, your new option for selecting fonts will now be fully functional.
191
193
192
-
###Closing thoughts
194
+
## Closing thoughts
193
195
Adding fonts to a project is something you will usually only do when setting up a new project and then forget about it. As you could see in this blog post, this isn't a complicated process, but it has a specific set of steps that have to be taken in order for custom fonts to work in your project.
194
196
195
197
Of course, there are other ways to include fonts in your project, but the described process is what we recommend and use. This is the (Eightshift) Way.
0 commit comments