Sub topic 12
Loading custom fonts with @font-face
This chapter assumes you know how to link a stylesheet, write selectors, and set font-family with a fallback stack (covered in the previous chapters). Here you will load your own font files, control how the browser shows text while they download, and avoid the layout problems that custom fonts commonly cause.
What @font-face does
A font-family name such as "Inter" only works if the user's device has that font installed. Most visitors will not. The @font-face at-rule tells the browser where to download a font file and which family name, weight, and style that file represents.
@font-face {
font-family: "Inter";
src: url("/fonts/inter.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
body {
font-family: "Inter", system-ui, sans-serif;
line-height: 1.5;
}The rule does not download anything by itself. The browser fetches the file only when some element on the page actually uses the family and needs a glyph from it. The body rule below the @font-face block applies the font and lists system-ui and the generic sans-serif as fallbacks.
The descriptors
Inside @font-face, the properties are called descriptors. They describe the font file, not an element.
| Descriptor | Purpose |
|---|---|
| `font-family` | The name you will use later in `font-family` declarations. You choose it. |
| `src` | One or more `url()` sources, each with an optional `format()` hint. |
| `font-weight` | The weight this file provides, such as `400` or `700`. |
| `font-style` | `normal`, `italic`, or `oblique`. |
| `font-display` | How text behaves while the file loads. |
| `unicode-range` | The characters this file covers. |
The family name is only a label. You could call it "Body Font" and use that name everywhere. Keeping the name of the real font avoids confusion.
Font formats and the src descriptor
Use WOFF2. It is compressed, supported by all current browsers, and usually the smallest option. Older formats such as WOFF, TTF, and OTF are only needed if you must support very old browsers.
src accepts a comma-separated list. The browser tries each source in order and uses the first one it supports.
@font-face {
font-family: "Inter";
src:
local("Inter"),
url("/fonts/inter.woff2") format("woff2"),
url("/fonts/inter.woff") format("woff");
font-weight: 400;
font-style: normal;
font-display: swap;
}The format() hint lets the browser skip a file it cannot decode without downloading it. The local() source checks for an installed copy first. It saves a download, but it has a catch: a locally installed font with the same name may be a different version from the one you tested. Many teams leave local() out for that reason.
Controlling loading with font-display
While a font file downloads, the browser must decide what to show. font-display sets that behavior. Two periods matter:
- The block period: text is invisible, drawn with an invisible fallback.
- The swap period: the fallback font shows and is replaced when the real font arrives.
| Value | Behavior | Typical use |
|---|---|---|
| `auto` | Browser default, usually like `block`. | Rarely a deliberate choice. |
| `block` | Short invisible text, then swap whenever the font arrives. | Icon fonts, where a fallback letter would be wrong. |
| `swap` | Almost no invisible period. Fallback shows at once, swaps when loaded. | Body text where readability matters most. |
| `fallback` | Very short invisible period, short swap window, then the fallback stays. | Text where a late swap would be jarring. |
| `optional` | Very short invisible period. The browser may skip the font entirely on slow connections. | Non-essential decorative fonts. |
swap is a good default for body text. Its cost is a visible change when the font arrives, which can move text around. The next section explains why and how to reduce it.
Layout shift from font swapping
Two fonts rarely have identical letter widths and heights. When the browser swaps the fallback for the custom font, lines can wrap differently and content below can jump. This counts as layout shift and hurts the Cumulative Layout Shift (CLS) score that browsers report.
You can reduce it in three ways:
- Preload the font file so it arrives earlier.
- Choose a fallback with similar proportions.
- Adjust the fallback's metrics so it matches the custom font.
Preloading
A preload hint in the HTML <head> starts the download before the browser has parsed your CSS and found the @font-face rule.
<link
rel="preload"
href="/fonts/inter.woff2"
as="font"
type="font/woff2"
crossorigin
>The crossorigin attribute is required even when the font is on your own domain. Fonts are fetched in a CORS mode, and without the attribute the preloaded file will not match the later request. The browser then downloads the font twice. Preload only the one or two files used above the fold, because every preload competes with other critical resources.
Metric overrides
You can define a local fallback face and adjust it with size-adjust, ascent-override, descent-override, and line-gap-override. This makes the fallback occupy nearly the same space as the web font.
@font-face {
font-family: "Inter Fallback";
src: local("Arial");
size-adjust: 107%;
ascent-override: 90%;
descent-override: 22%;
line-gap-override: 0%;
}
body {
font-family: "Inter", "Inter Fallback", system-ui, sans-serif;
}The percentages above are illustrative. Real values depend on the two fonts, and tools that compute them from the font files give accurate numbers. Measure before you trust them.
Several weights and styles
Each @font-face rule describes one file. To support regular and bold, declare two rules with the same font-family and different font-weight values.
@font-face {
font-family: "Inter";
src: url("/fonts/inter-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Inter";
src: url("/fonts/inter-bold.woff2") format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Inter";
src: url("/fonts/inter-italic.woff2") format("woff2");
font-weight: 400;
font-style: italic;
font-display: swap;
}The browser picks the file matching the requested weight and style. If a style has no file, the browser fakes it. A missing bold becomes a thickened version of the regular glyphs, and a missing italic becomes a slanted one. These synthesized styles look worse than real ones, so declare every weight and style you use, or avoid using the ones you did not load.
Variable fonts
A variable font packs a continuous range of weights, and sometimes widths, into one file. In @font-face you give a range instead of a single value.
@font-face {
font-family: "Inter";
src: url("/fonts/inter-variable.woff2") format("woff2");
font-weight: 100 900;
font-style: normal;
font-display: swap;
}
h1 {
font-weight: 650;
}With the range declared, any weight between 100 and 900 works, including values like 650. One variable file is often smaller than three or four static files, though a single variable file can be larger than one static file. Compare total bytes for the weights you actually use.
Subsetting with unicode-range
Many fonts include thousands of glyphs for scripts you may never display. The unicode-range descriptor splits a font into pieces, and the browser downloads a piece only when the page contains characters in its range.
@font-face {
font-family: "Inter";
src: url("/fonts/inter-latin.woff2") format("woff2");
font-weight: 400;
unicode-range: U+0000-00FF, U+0131, U+0152-0153;
}
@font-face {
font-family: "Inter";
src: url("/fonts/inter-cyrillic.woff2") format("woff2");
font-weight: 400;
unicode-range: U+0400-045F, U+0490-0491;
}A page in English never downloads the Cyrillic file. Font services such as Google Fonts use this technique. If you host your own files, you can create subsets with a tool like pyftsubset from the fontTools library.
Common errors
- Wrong path. A
url()in a stylesheet resolves relative to the CSS file, not the HTML page. Check the Network panel in developer tools for 404 responses. - Missing CORS headers. Fonts loaded from another origin need an
Access-Control-Allow-Originheader from the server. Without it, the browser blocks the font. - Mismatched names. The name in
font-family: "Inter"must match the descriptor exactly, ignoring case. A typo silently falls back to the next family. - Undeclared weights. Using
font-weight: 600with only 400 and 700 files makes the browser pick the nearest file, which may not be what you expect. - Too many files. Each weight and style is a request. Four or five faces is usually the practical limit.
- Unchecked licenses. Many fonts forbid self-hosting or require a paid web license. Confirm before you ship.
Putting it together
A small, complete setup for a site that uses one variable font:
@font-face {
font-family: "Inter";
src: url("/fonts/inter-variable.woff2") format("woff2");
font-weight: 100 900;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Inter Fallback";
src: local("Arial");
size-adjust: 107%;
ascent-override: 90%;
descent-override: 22%;
line-gap-override: 0%;
}
:root {
--font-body: "Inter", "Inter Fallback", system-ui, sans-serif;
}
body {
font-family: var(--font-body);
font-size: 1rem;
line-height: 1.5;
}
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
font-weight: 700;
}This reuses earlier ideas: the font stack lives in a custom property, sizes use rem, the heading size is fluid with clamp(), and the line height is unitless.
Exercise
A page loads a single "Inter" file for weight 400 only. A heading uses font-weight: 700. What does the browser do, and how do you fix it?
Show answer
The browser has no bold file, so it synthesizes bold by thickening the regular glyphs, which looks worse than a real bold face. Fix it by adding a second @font-face rule with the same font-family, a src pointing to the bold file, and font-weight: 700, or by using a variable font declared with font-weight: 100 900.
Exercise
You preload a self-hosted font with <link rel="preload" as="font" type="font/woff2" href="/fonts/inter.woff2"> and notice in the Network panel that the file downloads twice. What is missing?
Show answer
The crossorigin attribute. Fonts are fetched in CORS mode, so a preload without crossorigin does not match the later request from @font-face, and the browser fetches the file again.
Exercise
Which font-display value would you choose for an icon font, and why not swap?
Show answer
Use block. With swap, users would briefly see the fallback font's letters in place of icons, such as the word "menu" instead of a menu symbol. Block hides the text for a short time instead, which is less confusing for icons.