Tutorial

Using Google Fonts in Your Web Pages

Published on August 22, 2019
author

Cooper Makhijani

Using Google Fonts in Your Web Pages

Google Fonts is a service provided for free by Google that allows access to thousands of fonts. All the available fonts are under Open Source licenses, meaning they’re free to use for both commercial and non-commercial projects.

Getting started

This article will go over using Google Fonts on a webpage. Below is what a boilerplate web page looks like right now:

Screenshot of boilerplate page

And here’s the boilerplate HTML markup:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>My web page</title>
</head>
<body>
  <h1>Welcome to my website</h1>
</body>
</html>

Pretty boring, huh? Let’s spice it up a little with a better font.

Choosing your fonts

It’s now time for us to choose our fonts. Head on over to fonts.google.com and select a font you like by pressing the little (+) (plus) button. I’m going to use Karla. Once you’ve picked out your font, expand the drawer on the bottom of the page.

There are two ways to import the font for use. For the first method, copy the code in the code box under the Standard label. Now head on over back to your markup and add a the copied code and a style tag to the head of the document like this.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>My web page</title>

  <link href="https://fonts.googleapis.com/css?family=Karla&display=swap" rel="stylesheet">
</head>
<body>
  <h1>Welcome to my website</h1>
</body>
</html>

You’ll notice from the URL example above that Google Fonts now supports the font-display property! 🎉

If you already have a separate CSS stylesheet, copy the code under the @import label and add it to the top of your stylesheet like so.

@import url('https://fonts.googleapis.com/css?family=Karla&display=swap');

.element {
  /* ... */
}

Using the Fonts

We’ve imported our fonts, now it’s time to use them. Let’s set the body of our HTML markup to use Karla, like so:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>My web page</title>

  <link href="https://fonts.googleapis.com/css?family=Karla&display=swap" rel="stylesheet">

  <style>
      body {
        font-family: 'Karla', sans-serif;
      }
  </style>
</head>
<body>
  <h1>Welcome to my website</h1>
</body>
</html>

Now, if we take a look at our web page, it looks like this:

Screenshot with our custom font

That looks a lot better!

✨ That was easy, wasn’t it? Now you can make your web pages look prettier with the free font hosting offered by Google Fonts! Thanks for reading!

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Cooper Makhijani

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more