Working with Google Fonts

Linking your web pages to Google Fonts and formatting text elements by adding Google Font names to your stylesheets.

Learning Goals

At the end of this Tutorial, you will be able to:

  • Select font-weight/style combinations for selected font-families (typefaces) from the Google Fonts website.
  • Add selected Google Fonts CSS links to the <head> of a web page.
  • Add selected Google Fonts names to the CSS font-family property in a stylesheet file.

About Google Fonts

Google Fonts offers the Internet’s largest collection of open-source, royalty-free fonts. All 900-plus fonts can be used without restriction for personal, not-for-profit or commercial web projects.

Below is a screenshot of the Google Fonts home page at:

https://fonts.google.com

Introduction to Google Fonts

Google Fonts: the three steps

There are three basic steps to using Google Fonts. They are as follows.

  1. Select your font: From the Google Fonts website you choose the weight(s) and style(s) of the font-family you want to use.
  2. Copy-and-paste: You copy the HTML code generated by the Google Fonts website into the <head> of every page on which you want to use you chosen font(s). Introduction to Google Fonts In the example above, the name of the font is ‘Merriweather’.
  3. Update your CSS file: Edit the font-family style rules in your CSS file with the names of your chosen fonts.   For example, to use the Kinsta font for your <h1> headings, you would edit the h1 selector in your CSS file as follows. Introduction to Google Fonts To use the PT Sans font for your <h3> headings, you would edit the h3 selector in your CSS file as follows. Introduction to Google Fonts And if you wanted to use the Work Sans font for your text paragraphs, you would edit the p selector in your CSS file as follows. Introduction to Google Fonts Note that the font name is always wrapped in a pair of single quotation marks (‘ ’).   You can also see that every Google Font name is followed by a comma (,) and what is called a fallback font – either serif or sans-serif. This is the font that is displayed when the web page is viewed without a live Internet connection.

As with much else, there are fashions and trends in fonts. Here are the more widely-used serif and sans-serif fonts at the moment.

Introduction to Google Fonts

Adding fonts to section-selector-1

Follow the steps below:

Obtaining the Google Fonts code

Your first task is to go to the Google Fonts website, select the weight/styles of your required font-family, and then copy the related HTML code.

  1. In your web browser, go to the Google Fonts website at the address below:   https://fonts.google.com
  2. In the Search box at the top of the screen, type the font-family (typeface) name of Work Sans. Sample image You can type some sample text in the box on the left to see how your selected font-family will look.
  3. Next, in the search results displayed in the lower half of the screen, click on Work Sans. Sample image
  4. At the top-right of the next screen, click the Get font button. Sample image
  5. On the next screen, at the top-right you can see an icon indicating you have selected one font family. Sample image Click the Get embed code button.
  6. On the next screen displayed, click the Copy button for the code on the top-right. Sample image

Updating your web page with the Google Fonts code

Next, you will paste the HTML code you copied from Google Fonts into your web page.

  1. In VS Code, open the page-10.html web page.
  2. Just before the closing </head> tag, paste in the copied HTML code.   Optionally, add the following comment line just above the pasted Google Fonts code.
    <!-- Google Fonts -->
    
    Your web page <head> should now look similar to that below. Sample image
  3. Save your page-10.html web page.

When you view the page-10.html page in a web browser, you can see nothing has changed. For your selected Google Font to display, you also need to update the style-10.css stylesheet file linked to the web page.

Updating your stylesheet with the font-family name

Your final task is to update the linked stylesheet with the font-family name of the Google Font you want to use in your web page. Here are the steps.

  1. In VS Code, open the following stylesheet file:   style-10.css
  2. Add the following new style rule for the .section-selector-1 container class as shown below. Sample image
  3. Save your style-10.css file and view the page-10.html page in your web browser.

✅  Task completed.

Adding fonts to section-selector-2

Follow these steps:

  1. In the Google Fonts tab of your web browser, click the Fonts option in the menu at the top right. Sample image This returns you to the Google Fonts home page.
  2. In the Search box at the top-left of the screen, type the font-family (typeface) name of Outfit and then click this font-family in the search results. Sample image You can delete the sample text from the Preview box.
  3. At the top-right of the next screen, click the Get font button. Sample image
  4. On the next screen displayed, Click the Get embed code button. Sample image
  5. On the next screen displayed, click the Copy button for the code on the top-right.
  6. In the <head> of the page-10.html web page, select the existing Google Fonts code, and paste over it. Sample image You can see that Google Fonts has remembered your previous font selection. Save the page-10.html file.
  7. In VS Code, display your style-10.css stylesheet, and add the following new style rule. Sample image
  8. When finished, save the style-10.css file, and view the page-10.html web page in your browser.

✅  Task completed.

Adding fonts to section-selector-3

Follow these steps:

  1. Click the Fonts option at the top right to return the home page of Google Fonts.
  2. In the Search box at the top-left of the screen, type the font-family (typeface) name of Inter and then click this font-family in the search results.
  3. At the top-right of the next screen, click the Get font button.
  4. On the next screen displayed, Click the Get embed code button.
  5. On the next screen displayed, click the Copy button for the code on the top-right.
  6. In the <head> of the page-10.html web page, select the existing Google Fonts code, and paste over it. Sample image You can see that Google Fonts has remembered your previous font selection. Save the page-10.html file.
  7. In VS Code, display your style-10.css stylesheet, and add the following new style rule. Sample image
  8. When finished, save the style-10.css file, and view the page-10.html web page in your browser.

✅  Task completed.

Adding fonts to section-selector-5

Follow these steps:

  1. Click the Fonts option at the top right to return the home page of Google Fonts.
  2. In the Search box at the top-left of the screen, type the font-family (typeface) name of Playfair Display and then click this font-family in the search results.
  3. At the top-right of the next screen, click the Get font button.
  4. On the next screen displayed, Click the Get embed code button.
  5. On the next screen displayed, click the Copy button for the code on the top-right.
  6. In the <head> of the page-10.html web page, select the existing Google Fonts code, and paste over it.
  7. In VS Code, display your style-10.css stylesheet, and add the following new style rule. Sample image
  8. When finished, save the style-10.css file, and view the page-10.html web page in your browser.

✅  Task completed.

Adding fonts to section-selector-6

For this section, you will use two different Google Fonts: Bebas Neue for the h2 heading and Inter, which you have already selected, for the p paragraph text.

Follow these steps:

  1. Click the Fonts option at the top right to return the home page of Google Fonts.
  2. In the Search box at the top-left of the screen, type the font-family (typeface) name of Bebas Neue and then click this font-family in the search results.
  3. At the top-right of the next screen, click the Get font button.
  4. On the next screen displayed, Click the Get embed code button.
  5. On the next screen displayed, click the Copy button for the code on the top-right.
  6. In the <head> of the page-10.html web page, select the existing Google Fonts code, and paste over it.
  7. In VS Code, display your style-10.css stylesheet, and add the following new style rule. Sample image
  8. When finished, save the style-10.css file, and view the page-10.html web page in your browser.

✅  Task completed.

Uploading your files to GitHub

After finishing your web pages and stylesheets, you are now ready to upload them to your account on GitHub.

  1. Open a new tab in your web browser and go to GitHub.com. If you are not already signed in to your GitHub account, sign in now. github-signin
  2. On your GitHub home page, click the ‘repo’ that holds your web pages. Its name will look as follows, where username is your chosen username on GitHub.   username.github.io github-signin
  3. On the next GitHub screen displayed, near the right of the screen, you can see a button named Add file. Click on it. github-upload-portfolio
  4. From the dropdown list displayed, choose the option Upload files. Project Animation Google Fonts
  5. In File Explorer (Windows) or Finder (Apple Mac), drag-and-drop your index.html file and your 📁 assets and 📁 exercises sub-folders to upload them to your repository on GitHub. Introduction to HTML
  6. Scroll down to the bottom of the GitHub screen, and accept or edit the short message (Add files via upload) in the Commit changes box.
  7. Finally, click the green Commit changes button to upload your files. Project Animation Google Fonts

Your updated sample web page is now published on GitHub at a web address similar to the following:

https://username.github.io/exercises/page-10.html

It may take a few minutes for your upload files to appear on GitHub.