DEV Community

4 Steps to Self-Hosted Fonts in Gatsby

Ian Gloude on October 17, 2019

I finally got around to setting up fonts for my site, but everywhere I looked were articles that overly complicated self-hosting fonts in Gatsby. ...
Collapse
Β 
stevegriesel profile image
Stephan Griesel πŸ‡ΏπŸ‡¦ πŸ‡³πŸ‡± β€’ β€’ Edited

Plugin not even needed. Just add font to your static folder and refer to it in your css file. Keep in mind that when using the static folder you can just refer to it in the subfolder in your static folder, for example my static folder looks like this: static/fonts, then in your css your will just refer to /fonts/yourfont.otf

Collapse
Β 
allysonsouza profile image
Allyson Souza β€’

Great, this comment was the one that helped me understand that, I was trying to do it for almost a day... Thanks.

Collapse
Β 
zendannyy profile image
zendannyy β€’

is this global.css where font is referred to?

Collapse
Β 
stevegriesel profile image
Stephan Griesel πŸ‡ΏπŸ‡¦ πŸ‡³πŸ‡± β€’ β€’ Edited

Your own preference, I like to "componentize" my css files but global should work

Collapse
Β 
robmarshall profile image
Robert Marshall β€’

I think this plugin has been depreciated.

Collapse
Β 
alientang profile image
alientang β€’

:point-up: yup, use typefaces: github.com/kyleamathews/typefaces

Collapse
Β 
bsgreenb profile image
Ben Greenberg 🧒 β€’

only works if your desired font is in that repo, no? ^ also seems to be unmaintained

Collapse
Β 
kellenmace profile image
Kellen Mace β€’ β€’ Edited

I had to include /static in the path to the CSS file, like this: "/static/fonts/fonts.css".

Collapse
Β 
kellenmace profile image
Kellen Mace β€’ β€’ Edited

Update: I thought the fonts were supposed to go in /public/static/fonts directory, but that is incorrect. They need to go in /static/fonts. If you don't already have a /static directory in the root of your project, you need to create it first, then put the /fonts dir inside. Then using urls: ["/fonts/fonts.css"], as the article states will work like a charm.

Collapse
Β 
molamk profile image
molamk β€’

Short & sweet! Btw I think you meant to number the list 1.2.3.4 instead of 1.2.1.2

Nice post!

Collapse
Β 
iangloude profile image
Ian Gloude β€’

Ah, good catch! I had to break out of the list to drop a code block. Thanks!

Collapse
Β 
petrmrkvicka profile image
Petr Mrkvicka β€’

That's a life saving article! Thank you so much, Ian!

Collapse
Β 
iangloude profile image
Ian Gloude β€’

Glad it helped!

Collapse
Β 
janakact profile image
Janaka β€’

Hi thank you very much. but it seems like it's not working with --prefix-paths.

Any ideas on how to fix it. Seems like the prefix path is not taken from urls: ["/fonts/fonts.css"].

Collapse
Β 
aquasar profile image
Alex Quasar β€’

Can you use this together with Google Fonts?

Collapse
Β 
reza profile image
Reza Majidi β€’

yes. but you need to change the 'custom' key to 'google'

options: {
    google: {
        families: ['Droid Sans', 'Droid Serif']
    }
}

gatsbyjs.org/packages/gatsby-plugi...

Collapse
Β 
petrasp profile image
Petra Spirkova β€’

so helpful, many thanks Ian

Collapse
Β 
txndai profile image
GT Fari β€’

Worked like a charm, thank you

Collapse
Β 
husniadil profile image
Husni Adil Makmur β€’

This is what I'm looking for. Thank you.

Collapse
Β 
yasir991925 profile image
Yasir Jafar β€’

People who are using sass in the project.
in the sass file use css file to import and create fonts.css file instead of fonts.sass
and use the same code above, it should work then.