On this page10 sections
Lecture 010: Fonts
| Category | 3: Text, Fonts & Content |
| Lecture | 010 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
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-familyand safe fallbacks - Set
font-size,font-weightandfont-style - Load a web font from Google Fonts
- Write the
fontshorthand
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
<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 family | Look |
|---|---|
serif | With small strokes, or "feet" (Georgia) |
sans-serif | Without them (Arial, Inter) |
monospace | Fixed width, for code |
system-ui | The phone's or computer's own interface font |
2. Size, Weight and Style
Example
<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
- Go to fonts.google.com and choose a font, for example Poppins.
- Copy the
<link>it gives you into your page's<head>. - Use the font name in
font-family.
Example
<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:
@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
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
- Why list more than one font in
font-family? - What should the last item in a font list be?
- Which
font-weightnumber means bold? - Write a rule that makes
<h1>use Georgia, falling back to any serif font. - Where do you put the Google Fonts
<link>? - What does
font-display: swapdo?
Practice Answers
- So the browser has alternatives if the first font isn't available.
- A generic family such as
serif,sans-seriformonospace. 700h1 { font-family: Georgia, serif; }- In the
<head>of the page. - It shows text in a fallback font immediately, then switches when the web font has loaded.
Key Takeaways
font-familylists fonts in order, ending with a generic family.font-size(userem),font-weight(400 normal, 700 bold) andfont-styleshape text.- Google Fonts: add the
<link>, then use the name; load only what you need. - Two fonts per site is usually enough.