Skip to main content

Using Web Fonts

This guide covers how to add web fonts to your Gatsby site.

Web fonts and Gatsby

Web fonts provide a variety of typography styling options for your site. Hosting your fonts within a Gatsby project increases your site’s speed by up to ~300 milliseconds on desktop and 1+ seconds on 3G connections.

Prerequisites

This guide uses the Gatsby default starter.

Adding web fonts

Some examples of web font services include Google Fonts and Typekit Web Fonts.

Using Google Fonts

The fastest way to get started using Google Fonts is by choosing a font from the typefaces project. This example shows how you can add Open Sans to your project.

First, install the typeface package with npm:

Or with yarn:

In your layout.js file, import the typeface.

Next, add the typeface name to the appropriate place in your CSS. In this case, you will override the body element’s font-family default values.

Using Typekit Web Fonts

You can add Typekit Web Fonts to your project by using the gatsby-plugin-web-font-loader and your Adobe Fonts project id. For example, this is how you can add Futura to your project.

First, install the Gatsby plugin with npm:

Or with yarn:

Then, create an environment variable to store your Adobe Fonts project ID. (Make sure this file is in your .gitignore file so your ID doesn’t get committed!) For example, if your Adobe Fonts project ID is abcdefg, your .env file will look like this:

Now you can add the gatsby-plugin-web-font-loader plugin to your gatsby-config.js file, located in your root directory. In your plugin configuration, pass in the environment variable you created.

Next, add the typeface name to the appropriate font-family value in your CSS.

Other resources

Edit this page on GitHub