Why use custom fonts?
A font (or typeface) is the design of the letters on your page, and it shapes how your whole site feels. A bold, rounded font feels friendly; a fine serif feels elegant; a mono font feels technical.
Without help, a web page can only use fonts already installed on the visitor's device, and every device has a different set. The CSS @font-face rule solves this. It lets you supply your own font file, which the browser downloads and uses just like an installed font.
CSS sorts fonts into broad families. Here are the main generic families, each shown in whatever font your device uses for it:
serif: the quick brown fox
sans-serif: the quick brown fox
monospace: the quick brown fox
cursive: the quick brown fox
Generic families are useful as backups, but they look different on every device. With @font-face, everyone sees the exact font you chose.
Key rule: fonts are software with licences. Before using a font on your site, check its licence allows web use. Fonts from Google Fonts and Fontsource are free for web projects; many commercial fonts need a separate web licence.
The modern @font-face rule
@font-face is an at-rule, a special CSS instruction that starts with an @ sign. It doesn't style anything by itself; it simply tells the browser "here is a font, and here's its name". You then use that name in normal rules.
@font-face {
font-family: "CustomFont";
src: url("/fonts/customfont.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
body {
font-family: "CustomFont", system-ui, sans-serif;
}
Line by line
| Line | What it does |
|---|---|
font-family | The name you'll use to refer to this font. You can choose any name you like. |
src | Where to download the font file, and its format |
font-weight | Which weight this file contains: 400 is regular, 700 is bold |
font-style | Whether this file is the upright (normal) or italic version |
font-display | What to show while the font downloads (more on this later) |
In the body rule, the font names after the first are fallbacks. If the custom font can't load, the browser moves along the list until it finds one it can use.
Tip: the path in url() is relative to the stylesheet, not the web page. Starting it with a /, as in /fonts/customfont.woff2, makes it relative to your site's root folder, which avoids confusion.
Font formats, then and now
When this post was first written, every browser wanted a different font format, so you had to supply four or five versions of each font:
/* The old way: no longer needed */
@font-face {
font-family: "CustomFont";
src: url("customfont.eot");
src: url("customfont.eot?#iefix") format("embedded-opentype"),
url("customfont.woff") format("woff"),
url("customfont.ttf") format("truetype"),
url("customfont.svg#CustomFont") format("svg");
}
Thankfully, those days are over. Here's what each format was for, and where it stands today:
| Format | Originally for | Today |
|---|---|---|
| WOFF2 | Modern browsers | Supported everywhere. Use this. |
| WOFF | Firefox, then most browsers | Only needed for very old browsers |
| TTF / OTF | Desktop fonts, older Safari and Opera | Works, but files are much larger |
| EOT | Internet Explorer | Obsolete, as Internet Explorer is retired |
| SVG fonts | Early iPhone Safari | Obsolete, dropped by most browsers years ago |
WOFF2 (Web Open Font Format 2) is a compressed format designed specifically for the web. It's typically about 30% smaller than WOFF and often less than half the size of a TTF, so pages load faster.
Correction: the original list said Chrome used SVG fonts. Chrome removed SVG font support back in 2014, and no current browser needs them.
Converting a font to WOFF2
If you only have a TTF or OTF file, you can convert it with a free tool such as the Font Squirrel Webfont Generator or Transfonter. Remember to check the font's licence allows conversion.
Adding bold and italic
Most fonts come as a family of separate files: regular, bold, italic and so on. Give every file the same font-family name, but a different font-weight or font-style:
@font-face {
font-family: "CustomFont";
src: url("/fonts/customfont-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "CustomFont";
src: url("/fonts/customfont-bold.woff2") format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "CustomFont";
src: url("/fonts/customfont-italic.woff2") format("woff2");
font-weight: 400;
font-style: italic;
font-display: swap;
}
Now <strong>, <em> and headings automatically pick the right file. The browser reads the weight and style it needs, then finds the matching @font-face rule.
Why not just one name per file? Some older tutorials give each file its own name, like "CustomFont-Bold". Avoid this. If the browser can't find a bold file under the main name, it creates a fake bold by smearing the regular letters, which looks blurry and uneven.
Common weight numbers
| Number | Usual name |
|---|---|
300 | Light |
400 | Regular (same as normal) |
500 | Medium |
600 | Semibold |
700 | Bold (same as bold) |
800 | Extra bold |
Variable fonts
A variable font packs many weights, and sometimes widths or slants, into one single file. Instead of fixed steps like 400 and 700, you can use any weight in its range, such as 450 or 625.
To use one, give font-weight two numbers in the @font-face rule: the lightest and heaviest weights the file supports.
@font-face {
font-family: "CustomFont";
src: url("/fonts/customfont-variable.woff2") format("woff2");
font-weight: 100 900; /* every weight from thin to black */
font-style: normal;
font-display: swap;
}
h1 { font-weight: 800; }
p { font-weight: 400; }
.lead { font-weight: 450; } /* any value in between works */
Many devices use a variable font for their system font, so you can see the idea here. On those devices, each line below is a different weight drawn from one font:
300: light and airy
400: regular body text
500: a touch heavier
600: semibold headings
700: bold
800: extra bold
Tip: if you use three or more weights of a font, a variable version is usually smaller overall than separate files, and it needs only one download.
Controlling loading with font-display
A custom font takes a moment to download. What should the browser show in the meantime? Without instructions, many browsers hide the text for up to three seconds, which leaves visitors staring at a blank page. This is called a flash of invisible text.
The font-display descriptor, placed inside @font-face, lets you decide:
| Value | What happens while the font loads |
|---|---|
swap | Shows a fallback font straight away, then swaps in the custom font when it arrives. Best for most body text and headings. |
fallback | Hides text very briefly, then uses the fallback. Swaps only if the font arrives within about 3 seconds. |
optional | Uses the custom font only if it's ready almost instantly; otherwise keeps the fallback for this visit. Great for speed. |
block | Hides text for up to about 3 seconds while waiting. Only suitable for icon fonts. |
auto | Lets the browser decide, which usually behaves like block. |
Key rule: use font-display: swap unless you have a reason not to. Visitors can start reading immediately, and the text simply changes font a moment later.
Making fonts load faster
Preload your most important font
Normally the browser only discovers a font after it has downloaded and read your CSS. A preload link in your HTML <head> tells it to start downloading straight away:
<link rel="preload" href="/fonts/customfont-regular.woff2"
as="font" type="font/woff2" crossorigin>
The crossorigin attribute is required for fonts, even when they're on your own site, or the browser will download the file twice. Only preload the one or two fonts used near the top of the page.
Only load the characters you need
Many fonts include characters for dozens of languages. If your site is in English, you don't need Greek, Cyrillic or Vietnamese letters. Removing unused characters is called subsetting, and it can shrink a font file dramatically. Most font services do this for you automatically.
You can also use unicode-range to tell the browser which characters a file covers. It will only download that file if the page actually uses one of those characters:
@font-face {
font-family: "CustomFont";
src: url("/fonts/customfont-latin.woff2") format("woff2");
unicode-range: U+0000-00FF; /* basic Latin letters and symbols */
font-display: swap;
}
Self-hosting or a font service?
You can link to a service such as Google Fonts, which writes the @font-face rules for you, or download the files and host them yourself. Self-hosting is now often recommended: browsers no longer share cached fonts between sites, so there's no speed advantage to a shared service. Hosting fonts yourself also avoids sending visitors' details to another company, which matters for privacy laws in some countries.
Tip: every extra weight and style is another download. Most sites look great with just two or three font files, for example regular, bold and italic.
Fallbacks and font stacks
The list of fonts in a font-family rule is called a font stack. Always end it with a generic family, so there's a sensible backup on every device:
body {
font-family: "CustomFont", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
Fallbacks matter more than you might think. They're shown while the custom font loads, and permanently if it fails. Pick fallbacks that are similar in style, so the swap isn't jarring:
Serif stack: Georgia, "Times New Roman", serif
Elegant serif text for articles
Sans-serif stack: system-ui, "Segoe UI", Roboto, sans-serif
Clean sans-serif text for interfaces
Monospace stack: ui-monospace, Menlo, Consolas, monospace
code { color: blue; }
Reducing the jump when fonts swap
Even similar fonts have slightly different sizes, so text can shift when the custom font arrives. You can fine-tune a fallback with size-adjust, which scales a local font to better match your custom one:
@font-face {
font-family: "CustomFont Fallback";
src: local("Arial");
size-adjust: 105%;
}
body {
font-family: "CustomFont", "CustomFont Fallback", sans-serif;
}
Here local() uses a font already installed on the device instead of downloading one. The right percentage depends on your font, so adjust it until the two look about the same size.
Wrapping up
Using custom fonts has become much simpler. One WOFF2 file per weight and style, or a single variable font, covers every modern browser. Add font-display: swap so text appears straight away, preload your main font, keep the number of files small, and finish every font stack with a sensible fallback.