How to add self-hosted fonts in Gatsby
September 16, 2020
Introduction
I struggled to add self-hosted fonts to my Gatsby project. This is a note to show how I solved it.
If you're adding web fonts, you can directly add @import url("")
into your style sheet.
What didn't work?
I created a fonts directory inside of static and added downloaded fonts files. After that, I tried a few things.
- I placed
@font-face
inGlobal.js
, inside of my main styling file. - I created
fonts.js
undersrc/styles
with other styling sheets and import it inside ofcreateGlobalStyle
. - I imported the
fonts.js
file inLayout.js
file.
Those approaches did work for me.
How to solve this
- After place downloaded font files in
static/fonts
, create afont.css
at the same level. - Add your fonts data in side of
fonts.css
file using@font-face
. Myfonts.css
looks like this:
@font-face {
font-family: 'Benne Regular';
src: url('Benne-Regular.woff') format('woff');
font-weight: normal;
font-style: normal;
}
/* You can add more fonts in the same file. */
- Import
fonts.css
in the same file that hascreateGlobalStyles
.
This is itπ
Conclusion
The key to solve this problem was to create fonts.css
inside of /static/fonts/
!