On this page10 sections
  1. Learning Objectives
  2. 1. font-family and Fallbacks
  3. 2. Size, Weight and Style
  4. 3. Web Fonts From Google Fonts
  5. 4. Your Own Font Files With @font-face
  6. 5. The font Shorthand
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 010: Fonts

Category3: Text, Fonts & Content
Lecture010 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

A font sets the mood of a page: friendly, serious, modern. CSS picks the typeface, the size, the weight and the style, and can load free web fonts so the page looks the same on every device.


Learning Objectives

  • Choose fonts with font-family and safe fallbacks
  • Set font-size, font-weight and font-style
  • Load a web font from Google Fonts
  • Write the font shorthand

1. font-family and Fallbacks

List several fonts, separated by commas. The browser uses the first one it has, and the last item should be a generic family.

Example

html
<style>
  .serif { font-family: Georgia, "Times New Roman", serif; }
  .sans  { font-family: Arial, Helvetica, sans-serif; }
  .mono  { font-family: "Courier New", monospace; }
</style>

<p class="serif">Serif fonts have small feet. They feel classic.</p>
<p class="sans">Sans-serif fonts are clean. They feel modern.</p>
<p class="mono">Monospace: every letter is the same width.</p>

Font names with spaces need quotes: "Times New Roman".

Generic familyLook
serifWith small strokes, or "feet" (Georgia)
sans-serifWithout them (Arial, Inter)
monospaceFixed width, for code
system-uiThe phone's or computer's own interface font

2. Size, Weight and Style

Example

html
<style>
  .title  { font-size: 2rem; font-weight: 800; }
  .light  { font-weight: 300; }
  .italic { font-style: italic; }
</style>

<p class="title">Bold 2rem title</p>
<p class="light">Light weight text</p>
<p class="italic">Italic text</p>

font-weight goes from 100 (thin) to 900 (black). 400 is normal and 700 is bold. A weight only shows if the font includes it.


3. Web Fonts From Google Fonts

  1. Go to fonts.google.com and choose a font, for example Poppins.
  2. Copy the <link> it gives you into your page's <head>.
  3. Use the font name in font-family.

Example

html
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap" rel="stylesheet">
<style>
  body { font-family: "Poppins", sans-serif; }
</style>

<h2>Hello from Poppins</h2>
<p>A friendly, rounded font loaded from Google Fonts.</p>

Load only the weights you use. Every extra weight is another file to download.


4. Your Own Font Files With @font-face

If you have a font file, declare it once and then use it like any other font:

css
@font-face {
  font-family: "MyBrand";
  src: url("fonts/mybrand.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}

h1 { font-family: "MyBrand", sans-serif; }

font-display: swap shows text in a fallback font straight away, then swaps in the web font when it has loaded.


5. The font Shorthand

css
p { font: italic 700 1.1rem/1.6 Georgia, serif; }
/*   style  weight size/line-height family     */

Size and family are required; the rest is optional.


Common Beginner Mistakes

1. No fallback. font-family: Poppins; falls back to the browser default if the font fails. End with sans-serif or serif.

2. Too many fonts. One font for headings and one for body text is plenty.

3. Missing quotes. font-family: Times New Roman; should be "Times New Roman".

4. Loading every weight. It slows the page down for no reason.


Practice Questions

  1. Why list more than one font in font-family?
  2. What should the last item in a font list be?
  3. Which font-weight number means bold?
  4. Write a rule that makes <h1> use Georgia, falling back to any serif font.
  5. Where do you put the Google Fonts <link>?
  6. What does font-display: swap do?

Practice Answers

  1. So the browser has alternatives if the first font isn't available.
  2. A generic family such as serif, sans-serif or monospace.
  3. 700
  4. h1 { font-family: Georgia, serif; }
  5. In the <head> of the page.
  6. It shows text in a fallback font immediately, then switches when the web font has loaded.

Key Takeaways

  • font-family lists fonts in order, ending with a generic family.
  • font-size (use rem), font-weight (400 normal, 700 bold) and font-style shape text.
  • Google Fonts: add the <link>, then use the name; load only what you need.
  • Two fonts per site is usually enough.