Enea Xharja Logo

Using Google Fonts in Gatsby

Google Fonts is a great resource for adding beautiful fonts to your website. Adding fonts to a Gatsby project is as easy as installing the gatsby-plugin-google-fonts plugin:

shell
1npm install gatsby-plugin-google-fonts --save

Then, add your favorite fonts to your gatsby-config.js file:

js
1module.exports = {
2 siteMetadata: {
3 title: `I like Google Fonts`,
4 },
5 plugins: [
6 {
7 resolve: `gatsby-plugin-google-fonts`,
8 options: {
9 fonts: [`Roboto:300,300i,400,400i,700`],
10 display: 'swap',
11 },
12 },
13 ],
14};

In the example above, I am using display: swap. The font-display property defines how font files are loaded and displayed by the browser. swap tells the browser to use the fallback font to display the text, until the custom font has fully downloaded (aka "flash of unstyled text").

If you are looking for inspiration or help in choosing the right fonts for your project, be sure to check out Google Fonts and FontPair.

© 2021 Enea Xharja