Skip to the workspace
Pebellslearn by building
Fonts and Text Styling  ›  Lesson

Font Families and Web Fonts

Font families 12 minA font-family stack lists preferred fonts in order and ends with an unquoted generic family, and web fonts can be loaded so the preferred font is available.

css / Font Families and Web Fonts

Explore each scene at your own pace.

01 · The rule

The rule

Font Families and Web Fonts

A font-family stack lists preferred fonts in order and ends with an unquoted generic family, and web fonts can be loaded so the preferred font is available.

Build a font fallback stack and inspect what happens before a web font is available.

The café's designer picked a warm serif called Lora for headings. On her laptop it looks perfect. On a customer's phone, where Lora is not installed, the browser needs to know what to use instead. A font stack is that plan.

The rule

List fonts in order of preference, and always end with a generic family.

font-family takes a comma-separated list. The browser walks it left to right and uses the first font it actually has, deciding character by character:

h1,
h2,
h3 {
  font-family: "Lora", Georgia, serif;
}

body {
  font-family: system-ui, sans-serif;
}
  • Specific names first: "Lora", then a widely installed near-match, Georgia. Quote names that contain spaces or digits, such as "Source Serif 4"; quoting simple names is allowed too.
  • A generic family last, never quoted: serif, sans-serif, monospace, cursive, or system-ui (the device's own interface font). The browser always has something for these, so text never falls back to a surprise. Writing "serif" in quotes asks for a font literally named serif, which is not the same thing.

font-family is inherited: set it on body and every element inside uses it unless a rule says otherwise.

Web fonts let you send the font with the page instead of hoping it is installed. A service such as Google Fonts gives you a link for the head:

<link rel="stylesheet"
      href="https://fonts.googleapis.com/css2?family=Lora:wght@400;700&display=swap">

That stylesheet contains @font-face rules pointing at the font files. You can also download the files and write @font-face yourself, which avoids a request to a third-party server. Either way, load only the weights you use; each one is another file to download. The display=swap part tells the browser to show text in the fallback font immediately and swap in Lora when it arrives, so nobody stares at invisible text.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.