CSS Web Fonts
The fonts installed on a visitor's device vary a lot from one system to another. Web fonts solve this: you ship a font file with your site, register it with the @font-face rule, and every visitor sees the same typeface.
The @font-face rule
@font-face gives a font file a family name. After that you use the name in font-family like any other font.
@font-face {
font-family: "Brand Sans"; /* the name you'll use */
src: url("/fonts/brand-sans.woff2") format("woff2"),
url("/fonts/brand-sans.woff") format("woff");
font-weight: 400; /* which weight this file is */
font-style: normal;
font-display: swap; /* show fallback text while loading */
}
@font-face {
font-family: "Brand Sans"; /* same family, different file */
src: url("/fonts/brand-sans-bold.woff2") format("woff2");
font-weight: 700;
font-display: swap;
}
body { font-family: "Brand Sans", system-ui, sans-serif; }Register each weight and style as a separate @font-face block that uses the same family name. When an element asks for font-weight: 700, the browser picks the matching file.
| Descriptor | Purpose |
|---|---|
font-family |
The name you choose for the font |
src |
One or more url() or local() sources, tried in order |
font-weight |
Weight of this file, or a range like 100 900 for variable fonts |
font-style |
normal or italic |
font-display |
Loading behaviour (see below) |
unicode-range |
Download this file only if the page uses these characters |
Font formats
| Format | Notes |
|---|---|
| WOFF2 | Best compression and supported everywhere. Use this. |
| WOFF | Older, slightly larger. Only needed for very old browsers. |
| TTF / OTF | Desktop font files. They work, but they're big. |
| EOT, SVG fonts | Obsolete. Don't use them. |
Tip: Check the font's licence before self-hosting it. Many fonts are free for the web (for example under the SIL Open Font License), but some are licensed only for desktop use.
Font hosting services
Services such as Google Fonts host the files for you. You add a <link> or an @import, and they generate the @font-face rules:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap">
<style>
body { font-family: "Inter", system-ui, sans-serif; }
</style>Self-hosting the files on your own domain is often faster and avoids sending visitors' data to a third party.
Using local() and fallbacks
local() looks for a font that is already installed on the device. The playground below uses only local() sources, so it downloads nothing. If no listed font is found, the generic fallback takes over. The result depends on your system, which shows why web fonts exist in the first place.
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: "My Heading";
src: local("Georgia"), local("Times New Roman"), local("DejaVu Serif");
font-weight: 700;
}
@font-face {
font-family: "My Code";
src: local("Consolas"), local("Menlo"), local("DejaVu Sans Mono"), local("Courier New");
}
body { font-family: system-ui, sans-serif; padding: 10px; }
h1 { font-family: "My Heading", serif; font-weight: 700; color: #6a1b9a; margin: 0 0 8px; }
code { font-family: "My Code", monospace; background: #f3e5f5; padding: 4px 8px; border-radius: 4px; }
.missing { font-family: "Font That Does Not Exist", cursive; color: #00796b; font-size: 22px; }
</style>
</head>
<body>
<h1>Custom family names</h1>
<p><code>const x = 42;</code></p>
<p class="missing">Unknown font: falls back to cursive</p>
</body>
</html>font-display: avoiding invisible text
While a web font downloads, the browser has to decide what to show. font-display tells it.
| Value | Behaviour |
|---|---|
auto |
Browser default (usually like block) |
block |
Hide text for up to about 3s, then swap in the font |
swap |
Show fallback text immediately and swap when the font loads |
fallback |
Hide very briefly; give up on the font if it takes too long |
optional |
Use the font only if it is already available almost instantly |
swap is the usual choice for body text. Readers see content right away instead of a blank page.
Matching the fallback
When the web font swaps in, text can jump if the fallback has different metrics. Pick a fallback with a similar width, and tune the fallback with size-adjust if needed. The demo shows how much the size and width of system fonts differ:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 10px; }
p { font-size: 20px; margin: 6px 0; padding: 4px 8px; display: inline-block; background: #e3f2fd; }
.a { font-family: Georgia, serif; }
.b { font-family: Verdana, sans-serif; }
.c { font-family: Arial, sans-serif; }
.d { font-family: "Courier New", monospace; }
</style>
</head>
<body>
<p class="a">The same sentence, Georgia</p><br>
<p class="b">The same sentence, Verdana</p><br>
<p class="c">The same sentence, Arial</p><br>
<p class="d">The same sentence, Courier</p>
</body>
</html>Note: Variable fonts pack many weights (and sometimes widths or slants) into a single file. Declare a range such as
font-weight: 100 900;in@font-face, then use any value in between, likefont-weight: 650.
Test yourself
Which font format should you serve to modern browsers?
- EOT
- TTF
- WOFF2
- SVG
What does font-display: swap do?
- Hides text until the font loads
- Shows fallback text immediately and switches when the font arrives
- Never uses the web font
- Swaps bold and normal weights
How do you provide a bold version of a custom font family?
- Add font-weight: bold to body
- Add a second @font-face with the same family name, the bold file and font-weight: 700
- Rename the family to Family-Bold and nothing else
- It's impossible