Lesson 13 of 29
In Progress

Using Web Fonts and Font Stacks

One of the key aspects of web design is choosing the right fonts for your content. While there are a limited number of fonts available on most devices, web fonts allow you to use a wider range of fonts by linking to them in your HTML or CSS.

What are Web Fonts?

Web fonts are fonts that are hosted on a web server and can be linked to in your HTML or CSS. This allows you to use a wider range of fonts than what is available on the user’s device, as long as they have an internet connection.

There are several formats for web fonts, including TrueType (TTF), OpenType (OTF), and Web Open Font Format (WOFF). The most widely supported format is WOFF, which provides good compression and fast loading times.

How to use Web Fonts

There are several ways to use web fonts in your web design:

  • Link to a web font service: There are several web font services, such as Google Fonts and Adobe Fonts, that allow you to easily search for and use web fonts in your web design. To use a web font from one of these services, you need to add a link to the font in the head of your HTML document, and then specify the font in your CSS.

Here’s an example of how you might use the “Roboto” font from Google Fonts in your web design:

<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>
body {
  font-family: 'Roboto', sans-serif;
}

This adds a link to the “Roboto” font in the head of the HTML document, and then specifies the “Roboto” font for the body element in the CSS.

  • Host the web font files yourself: Alternatively, you can host the web font files yourself by uploading them to your web server and linking to them in your HTML or CSS. This allows you to use web fonts without relying on a third-party service.

Here’s an example of how you might use a self-hosted web font in your web design:

<link href='/fonts/roboto.woff' rel='stylesheet'>
body {
  font-family: 'Roboto', sans-serif;
}

This adds a link to the “Roboto” font in the head of the HTML document, and then specifies the “Roboto” font for the body element in the CSS.

What are Font Stacks?

Font stacks are lists of fonts that are specified as fallbacks in case the first choice is not available on the user’s device. Font stacks are commonly used in web design to ensure that the intended font is displayed as closely as possible, even if the user doesn’t have the exact font installed on their device.

Font stacks are specified in the “font-family” property in CSS. The first font in the stack is the primary choice, and the subsequent fonts are the fallbacks.

Here’s an example of a font stack for the “Roboto” font:

body {
  font-family: 'Roboto', sans-serif;
}

In this example, the “Roboto” font is the primary choice, and any other sans-serif font is the fallback. This ensures that the “Roboto” font is displayed if it is available, and a similar sans-serif font is displayed if it is not.

Tips for Using Web Fonts and Font Stacks

Here are a few tips for using web fonts and font stacks in your web design:

  • Choose web fonts that are legible and easy to read: Make sure to choose web fonts that are legible and easy to read, especially for body text. Avoid using decorative or hard-to-read fonts for large blocks of text.
  • Test web fonts in different browsers: Make sure to test your web fonts in different browsers and devices to ensure that they are displayed correctly. Some web fonts may not be available on all platforms, so it’s important to have fallback fonts in place.
  • Use font stacks wisely: Don’t overuse font stacks or include too many fonts in your stack. This can slow down your website and make it difficult to maintain. Instead, choose a few well-designed fonts that complement your design and use them consistently throughout your website.

More Tips

Additionally, it’s important to choose web fonts that are legible and easy to read, especially for body text. Avoid using decorative or hard-to-read fonts for large blocks of text, as they can make your website difficult to read and navigate.

Another tip is to use font stacks wisely by not overusing them or including too many fonts in your stack. This can slow down your website and make it difficult to maintain. Instead, choose a few well-designed fonts that complement your design and use them consistently throughout your website.

Finally, make sure to choose web fonts that are appropriate for your content and audience. Different fonts convey different moods and tones, so it’s important to choose fonts that match the tone and style of your website. For example, a formal website might benefit from a more traditional font, while a creative website might benefit from a more unique font.

By following these tips, you can effectively use web fonts and font stacks to enhance the appearance and readability of your website.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Explain the purpose of web fonts.

The purpose of web fonts is to allow web designers to use a wider range of fonts than what is available on the user’s device. Web fonts are hosted on a web server and can be linked to in the HTML or CSS, allowing the desired font to be displayed as closely as possible, even if the user doesn’t have the exact font installed on their device.

How do you use a web font from a web font service in your web design?

To use a web font from a web font service in your web design, you need to add a link to the font in the head of your HTML document, and then specify the font in your CSS. For example:

<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>
body {
  font-family: 'Roboto', sans-serif;
}

Explain the purpose of font stacks.

Font stacks are lists of fonts that are specified as fallbacks in case the first choice is not available on the user’s device. Font stacks are commonly used in web design to ensure that the intended font is displayed as closely as possible, even if the user doesn’t have the exact font installed on their device.

Create a font stack for the “Open Sans” font that includes fallbacks for sans-serif and serif fonts.

body {
  font-family: 'Open Sans', sans-serif, serif;
}

Explain one tip for using web fonts and font stacks in your web design.

One tip for using web fonts and font stacks in your web design is to test them in different browsers and devices to ensure that they are displayed correctly. Some web fonts may not be available on all platforms, so it’s important to have fallback fonts in place. Testing your web fonts in different environments can help you ensure that your website looks and performs consistently across devices.