Font Family and Font Stacks
You'll learn how to specify fonts in CSS and create reliable fallback chains that work across all devices.
The Core Idea
When you design a website, you want text to appear in a specific typeface—but here's the catch: not every computer or device has the same fonts installed. The font-family property lets you specify which font to use, and by listing multiple options (a "font stack"), you create a safety net that ensures your text always looks intentional, even if your first-choice font isn't available.
How Font Stacks Work
Think of a font stack like a restaurant order: "I'll have salmon, but if you're out, I'll take chicken, or just give me a vegetarian option." CSS tries each font in order until it finds one that exists on the user's device:
p {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
In this stack:
- CSS tries "Helvetica Neue" (requires quotes because of the space)
- Falls back to Helvetica if unavailable
- Then tries Arial
- Finally uses the system's default sans-serif font
Generic Font Families
Always end your stack with a generic family (serif, sans-serif, monospace, cursive, or fantasy). This guarantees the browser can always render something appropriate:
h1 {
font-family: Georgia, Times, serif;
}
code {
font-family: "Courier New", Courier, monospace;
}
Quote Multi-Word Names
Font names with spaces or special characters need quotes:
font-family: "Times New Roman", Times, serif; /* Correct */
font-family: Arial, sans-serif; /* No quotes needed */
Key Takeaway: Font stacks provide cross-platform reliability—list preferred fonts first, include web-safe alternatives, and always end with a generic family.