DEV Community

loading...
Cover image for How to use Google Fonts

How to use Google Fonts

dailydevtips1 profile image Chris Bongers Originally published at daily-dev-tips.com ・3 min read

Google fonts are an awesome way of adding a cool font to your website, I know I'm not using them, but that is for other purposes.
I use them in client websites all the time, and they are amazing and simple to use.

Let's get started on implementing a custom font for your website.

Finding your Google Font

To find a cool font, we can go to the Google Font library and pick one we like.

Once you have found the one you want, open it up you will see the whole set's letters and Styles.
Google Fonts will allow you to include on our more styles on your website. In my case, I'm using Amatic SC Bold. Click the Select this style button for each style you want.
You will see a side menu on the right. It will have a Review and Embed section; in the embed, we can get the code.

Embedding the Google Font in your website

There are two ways of embedding the font into your website

  • <link> attribute; this sets a link like any external stylesheet
  • @import this imports it directly in the CSS

The Google Fonts website will also show you which font-family rule you must apply to each element you want to have this specific font.

Should I use Link or @import?

This is a question that keeps floating around on the internet, and Steve Souders did an excellent job of comparing the two.
As his article title suggests, is the best way to go according to him and Yahoo.

Using the Google Font on our website

<head>
  <link
    href="https://fonts.googleapis.com/css2?family=Amatic+SC:wght@700&display=swap"
    rel="stylesheet"
  />
</head>
Enter fullscreen mode Exit fullscreen mode

We embed the Google Font in the head of our HTML file using the following code. This is the code you get from Google Fonts.

Then in our CSS we can do the following:

h1 {
  font-family: 'Amatic SC', cursive;
}
Enter fullscreen mode Exit fullscreen mode

It's important to use the name as stated in Google Fonts website.

We then have the following result:

See the Pen KKdXJQK by Chris Bongers (@rebelchris) on CodePen.

What to think about

So important to note when using Google Fonts, they tend to load slowly, the more you use, the more load time it will add.
And Google itself will not like that very much. My suggestion is that you keep it to one custom font.
We can use font-display: swap to not interfere with the load, more on this in another article!

Thank you for reading, and let's connect!

Thank you for reading my blog, feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Discussion

pic
Editor guide