Fonts
🧠 Overview
Proper font loading: woff2, preload, font-display.
⚙️ Preload Critical Fonts
html
<link rel="preload" href="/fonts/main.woff2" as="font" type="font/woff2" crossorigin />Notes:
as="font"— required for fonts, without it the browser ignores the preloadtype="font/woff2"— helps the browser assign the correct loading prioritycrossorigin— required even for self-hosted fonts, otherwise the browser downloads the file twice (anonymous mode is default for fonts)- Preload only above-the-fold fonts — the rest will load when CSS requests them
💻 @font-face
css
@font-face {
font-family: "Inter";
src: url("/fonts/inter.woff2") format("woff2");
font-display: swap;
}font-display values:
auto— default, browser decidesblock— text is invisible until the font loads (FOIT)swap— system font shown first, then swapped (FOUT)optional— like swap, but no swap if font hasn't loaded in time
⚠️ Pitfalls
font-display: swap→ FOUT (text flashes on font swap)- Don't preload all fonts → only those above the fold
- WOFF2 is the only format you need
🚀 Best Practice
- WOFF2 only
- Preload critical fonts
font-display: swap- Subset for non-Latin scripts (smaller file size)