Sub topic 12
Loading Custom Fonts with @font-face
This chapter assumes you know basic HTML, CSS selectors and the cascade, and how to link a stylesheet. You should also know how font-family fallback stacks work from the previous chapters. Here you will load your own font file, control how the browser displays text while it downloads, and avoid the most common mistakes.
What @font-face does
A font stack can only use fonts already installed on the visitor's device. The @font-face at-rule lets you point the browser at a font file on your server. The browser downloads the file when a page needs it and makes it available under a name you choose.
The rule has two jobs. It declares a family name, and it tells the browser where the file is. You then use that name in font-family like any other font.
A working declaration
@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;
}Each descriptor has a purpose:
font-familyis the name you will use in the rest of your CSS. It does not have to match the name inside the file.srcgives the file location inurl(). The optionalformat()hint lets the browser skip formats it cannot read without downloading them.font-weightandfont-stylesay which variant of the family this file provides.font-displaycontrols what the user sees while the file loads.
The body rule puts "Inter" first. If the file fails to load, the browser uses system-ui, then the generic sans-serif. The unitless line-height of 1.5 multiplies each element's own font size.
Choosing a file format
Use WOFF2 (.woff2). It is compressed, so files are small, and every current browser supports it. Older formats such as WOFF, TTF, and EOT are rarely needed now. If you must support an old browser, list several sources. The browser uses the first one it can read.
@font-face {
font-family: "Inter";
src:
url("/fonts/inter.woff2") format("woff2"),
url("/fonts/inter.woff") format("woff");
}One face per weight and style
Each @font-face rule describes one face. If your design uses regular and bold text, declare both, and give each the matching weight.
@font-face {
font-family: "Inter";
src: url("/fonts/inter-regular.woff2") format("woff2");
font-weight: 400;
}
@font-face {
font-family: "Inter";
src: url("/fonts/inter-bold.woff2") format("woff2");
font-weight: 700;
}Both rules share the family name "Inter". When an element uses font-weight: 700, the browser picks the second file. If you declare only the regular face, the browser fakes bold by thickening the letters, which usually looks worse.
A variable font is one file that covers a range of weights. You declare it with a range, such as font-weight: 100 900;.
Controlling the loading experience with font-display
While a font downloads, the browser must decide what to show. The font-display descriptor sets that behavior.
| Value | Behavior | Typical use |
|---|---|---|
| `swap` | Shows fallback text immediately, then swaps to the custom font when it arrives | Body text |
| `fallback` | Very short invisible period, then fallback, with a limited window to swap | Text where a late swap would be distracting |
| `optional` | The browser may skip the custom font if it is not ready quickly | Non-essential decoration |
| `block` | Hides text for a short time, then swaps | Icon fonts and brand type |
Common errors
- Wrong path. A
url()in a stylesheet is resolved relative to the stylesheet, not the HTML page. A path that starts with/is resolved from the site root. Check the Network tab in your browser's developer tools for a 404 response. - Name mismatch. If
@font-facedeclares"Inter"butfont-familyasks for"Inter Variable", the browser ignores your file and uses the next font in the stack. - No fallback. Always end the stack with a generic family, so text stays readable if the download fails.
- Cross-origin files. Fonts loaded from another domain need the server to send CORS headers (
Access-Control-Allow-Origin). Without them, the browser refuses the file. - Layout shift. When the swap happens, the new font may have different letter widths, so text can move. Choose a fallback with similar proportions, and avoid loading more faces than the design needs.
Exercise
You declared one @font-face rule for "Inter" with font-weight: 400. Your headings use font-weight: 700, and they look smeared. What is the cause, and how do you fix it?
Show answer
The browser has no bold face for "Inter", so it synthesizes bold by thickening the regular glyphs. Add a second @font-face rule with the same font-family name, a bold WOFF2 file in src, and font-weight: 700. Alternatively, use one variable font file declared with font-weight: 100 900.
Exercise
A page shows invisible text for about a second before the custom font appears. Which font-display value gives readable text immediately, and what is the trade-off?
Show answer
Use font-display: swap. The browser draws text in the fallback font right away and redraws it in the custom font when the file arrives. The trade-off is a visible change in the text, and possible layout shift if the two fonts have different widths.