Sub topic 2
Writing `font-family` Stacks with Fallbacks
Assume you know basic HTML, CSS selectors, the link element, and browser developer tools. A font stack is an ordered list of font family names. The browser tries the first available font, then moves right when a font is missing or cannot render a character.
Writing a fallback stack
Put a preferred web font first, then compatible system fonts, and finish with a generic family. Quote names containing spaces. Use sans-serif, serif, or monospace as the final category.
body {
font-family: "Inter", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
}
code,
pre {
font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Consolas, monospace;
}A fallback is not only a backup for a failed download. It also covers users whose operating system lacks the font and characters that the chosen font does not support. Choose fallbacks with similar width and shape to reduce reflow when the web font arrives.
Adding Google Fonts to a page
Google Fonts provides hosted families and generates the CSS URL for the selected weights and styles. Prefer a link element in the document head. Preconnect lets the browser start connecting before it requests the stylesheet.
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">Then reference the family in a stack:
body {
font-family: "Inter", system-ui, sans-serif;
}An equivalent CSS-only approach is @import, but it can delay stylesheet discovery. Use it only when your project requires it.
Adding Adobe Fonts through a web project
Adobe Fonts uses a web project. Add the families to the project, then copy its generated stylesheet URL into the page. Replace the project ID below with the ID Adobe gives you.
<link rel="stylesheet" href="https://use.typekit.net/your-project-id.css">Adobe’s project page supplies the exact CSS family name. Use that name, with fallbacks, in your stylesheet:
h1,
h2 {
font-family: "proxima-nova", Helvetica, Arial, sans-serif;
font-weight: 700;
}The browser’s choice follows this path:
Exercise
Why should sans-serif appear at the end of a stack such as "Inter", system-ui, sans-serif?
Show answer
It guarantees a broadly supported final fallback when the web font and preferred system fonts are unavailable.