In Text & Fonts we built font stacks from fonts that are already installed on the user's device. That works, but it limits you to a handful of system fonts. What if your design uses Inter, Poppins or Noto Sans Devanagari, which most visitors don't have installed?
The answer is web fonts: font files that the browser downloads along with your page, just like images. In this chapter we'll learn how to declare them with @font-face, which file formats to use, how fonts load (and why text sometimes "flashes"), how to use Google Fonts correctly, and how variable fonts let one file replace many.
What is a Web Font?
A web font is a font file served from a web server and declared in CSS with an
@font-facerule. The browser downloads it when the page needs it and uses it to render text, so the font looks the same whether or not the user has it installed.
The process has two separate steps:
1. DECLARE @font-face { font-family: "Inter"; src: url(inter.woff2); }
→ "a font called Inter exists, and here is its file"
2. USE body { font-family: "Inter", system-ui, sans-serif; }
→ "render this text in Inter"Declaring a font does not download it. Browsers download a web font only when some text on the page actually needs it. A declared-but-unused font costs nothing.
The @font-face Rule
@font-face {
font-family: "Inter"; /* the name YOU choose */
src: url("/fonts/inter-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
body {
font-family: "Inter", system-ui, sans-serif;
}| Descriptor | Purpose |
|---|---|
font-family | The name you will use in font-family elsewhere. It can be any name; it doesn't have to match the file. |
src | Where to get the font: one or more url() or local() sources, tried in order. |
font-weight | Which weight (or range of weights) this file provides. |
font-style | Whether this file is the normal or italic face. |
font-display | What to show while the file is downloading. |
unicode-range | Which characters this file covers (download it only if those characters appear). |
Notice these are called descriptors, not properties. Inside @font-face, font-weight: 400 does not make anything bold or thin; it describes the file, so the browser knows which file to pick when some text asks for weight 400.
One family, several files
A static font usually comes as separate files for each weight and style. You declare each one with the same family name and different descriptors:
@font-face {
font-family: "Poppins";
src: url("/fonts/poppins-400.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Poppins";
src: url("/fonts/poppins-700.woff2") format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Poppins";
src: url("/fonts/poppins-400-italic.woff2") format("woff2");
font-weight: 400;
font-style: italic;
font-display: swap;
}Now normal text uses the 400 file, <strong> (weight 700) uses the bold file, and <em> uses the italic file — all automatically, through normal CSS.
A common mistake is giving each file its own name ("Poppins Bold", "Poppins Regular"). Then <strong> inside text using "Poppins Regular" can't find a bold file in that family, and the browser creates a fake (synthesized) bold by smearing the regular glyphs.
Font File Formats
| Format | format() hint | Notes |
|---|---|---|
| WOFF2 | "woff2" | Best choice today. Compressed with Brotli; typically about 30% smaller than WOFF. Supported by every current browser. |
| WOFF | "woff" | Older web format (zlib compression). Only needed for very old browsers. |
| TrueType / OpenType | "truetype" / "opentype" | Desktop font files (.ttf/.otf). They work, but are larger because they aren't compressed for the web. |
| EOT, SVG fonts | — | Obsolete (old Internet Explorer / old iOS). Don't use them. |
For a modern site, WOFF2 alone is enough. If you only have a .ttf file, convert it to WOFF2 with a font tool before shipping it.
The format() hint lets the browser skip files it can't use without downloading them. The src list is tried in order, so put the best format first:
@font-face {
font-family: "Inter";
src:
local("Inter"), /* use the installed copy if present */
url("/fonts/inter.woff2") format("woff2"),
url("/fonts/inter.woff") format("woff"); /* fallback for very old browsers */
}local() looks for a font installed on the user's computer. It saves a download, but the installed version might be different from yours, so many projects skip it for consistency.
How Fonts Load: FOIT and FOUT
A font file can take a moment to arrive, especially on slow mobile networks. While it is downloading, the browser has two choices:
FOIT — Flash Of Invisible Text: hide the text until the font arrives. The user sees blank space where the content should be.
FOUT — Flash Of Unstyled Text: show the text immediately in a fallback font, then swap to the web font when it arrives. The text is readable at once, but it "jumps" when the font changes.
The font-display descriptor tells the browser which strategy to use. It works with two time windows:
Block period: text is drawn invisibly while waiting for the font.
Swap period: text is shown in the fallback font; if the web font arrives in this window, it is swapped in.
After both periods end, the fallback is used for good on that page view.
| Value | Block period | Swap period | Behaviour |
|---|---|---|---|
auto | Browser decides | Browser decides | Usually behaves like block |
block | Short (about 3s) | Infinite | Invisible text up to ~3s, then fallback, swap whenever the font arrives |
swap | Extremely small (≤ 100ms) | Infinite | Fallback almost immediately; always swap when the font arrives |
fallback | Extremely small | Short (about 3s) | Swap only if the font arrives quickly; otherwise keep the fallback |
optional | Extremely small | None | Use the font only if it's available almost instantly (e.g. cached); never swap later |
(The exact durations are recommendations in the CSS Fonts specification; browsers may adjust them.)
Which one to choose?
swap— the most common choice for body text and headings. Content is readable immediately. Google Fonts uses it when you adddisplay=swap.optional— best for performance and layout stability. The first visit may use the fallback, but the font gets cached and is used on later pages, and the text never jumps.block— only for icon fonts, where showing the fallback (random letters) would be worse than showing nothing.
Reducing the jump
When a web font swaps in, text often changes width and height, which moves content around (a layout shift that hurts the Core Web Vitals score called CLS). Two techniques reduce it:
Pick a fallback that looks similar in size (e.g. Arial for a geometric sans-serif).
Adjust the fallback with
size-adjustin its own@font-faceso it occupies about the same space:
@font-face {
font-family: "Inter Fallback";
src: local("Arial");
size-adjust: 107%; /* scale Arial's glyphs to match Inter's width */
}
body {
font-family: "Inter", "Inter Fallback", sans-serif;
}The exact percentage depends on the two fonts (tools and frameworks such as Next.js calculate it for you). size-adjust works in all current browsers (Safari since version 17). Related descriptors like ascent-override are not yet supported in Safari.
Using Google Fonts
Google Fonts hosts over a thousand free, open-licensed fonts. You choose a font on fonts.google.com, and it gives you code like this to put in the <head>:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap">Then use the family name in your CSS:
body {
font-family: "Inter", system-ui, sans-serif;
}What these lines do:
The two
preconnectlinks open the network connections to Google's servers early, saving time. The font files themselves come fromfonts.gstatic.com, which is why that one needscrossorigin.The stylesheet link downloads a CSS file that contains the
@font-facerules for you. In the URL,wght@400;700requests weights 400 and 700, anddisplay=swapaddsfont-display: swap.For a variable font you can request a range:
family=Inter:wght@100..900.
If you open that stylesheet URL in a browser, you'll see it is just a list of @font-face rules pointing to .woff2 files, each with a unicode-range for a script (Latin, Cyrillic, Greek, Vietnamese ...). Your browser downloads only the pieces the page's text actually uses.
Self-hosting vs Google Fonts
| Google Fonts link | Self-hosting | |
|---|---|---|
| Setup | Copy-paste two or three lines | Download files, write @font-face yourself |
| Speed | Extra connections to other domains | Same server as your site; can be preloaded |
| Caching across sites | No benefit any more — browsers partition caches per site | Same |
| Privacy | Visitor IP addresses go to a third party (a concern under laws like GDPR) | No third party involved |
| Works offline / without third parties | No | Yes |
Many developers still believe "Google Fonts are already cached from other sites". That stopped being true when browsers moved to cache partitioning (Chrome in 2020; Safari earlier), so self-hosting is often slightly faster today.
Preloading an Important Font
Normally the browser discovers a font late: it must download the HTML, then the CSS, then build the page, and only then realise the font is needed. For the one or two most important font files, you can tell the browser early:
<link rel="preload"
href="/fonts/inter-latin.woff2"
as="font"
type="font/woff2"
crossorigin>The crossorigin attribute is required even for fonts on your own domain, because browsers always fetch fonts in CORS mode. Without it, the preloaded file isn't reused and the font is downloaded twice. Don't preload every font — only those used above the fold — or you will slow down more important resources.
Cross-Origin Fonts (CORS)
Fonts are protected by the same-origin policy. If your page is on example.com and the font file is on a different domain (like a CDN), that server must send an Access-Control-Allow-Origin header, or the browser refuses to use the font. Google Fonts and the popular CDNs already do this; you need to care about it when you host fonts on your own separate CDN or storage bucket. When it fails, DevTools shows a CORS error and the text falls back silently.
unicode-range: Download Only What You Need
unicode-range tells the browser which characters a file contains. If no character on the page is in that range, the file is never downloaded.
/* Latin characters */
@font-face {
font-family: "Noto Sans";
src: url("/fonts/noto-latin.woff2") format("woff2");
unicode-range: U+0000-00FF;
}
/* Devanagari characters (Hindi, Marathi, ...) */
@font-face {
font-family: "Noto Sans";
src: url("/fonts/noto-devanagari.woff2") format("woff2");
unicode-range: U+0900-097F;
}An English-only page downloads only the Latin file; a page with Hindi text also downloads the Devanagari file. This is called subsetting, and it is exactly what Google Fonts does behind the scenes.
Variable Fonts
A variable font is a single font file that contains a continuous range of styles along one or more axes, such as weight or width, instead of separate files for each style.
With static fonts, using weights 300, 400, 500, 600 and 700 means five downloads. A variable font gives you all of them — and every value in between, like 450 — from one file, usually smaller than the five combined.
@font-face {
font-family: "Inter";
src: url("/fonts/inter-variable.woff2") format("woff2");
font-weight: 100 900; /* a RANGE: this file covers every weight from 100 to 900 */
font-display: swap;
}
h1 { font-weight: 800; }
p { font-weight: 400; }
.nav { font-weight: 550; } /* any value in the range works */Common axes have standard tags, and most map to normal CSS properties:
| Axis tag | Meaning | Normal CSS property |
|---|---|---|
wght | Weight | font-weight |
wdth | Width (condensed ↔ expanded) | font-stretch (also called font-width in the newest spec) |
ital | Italic on/off | font-style: italic |
slnt | Slant angle | font-style: oblique 10deg |
opsz | Optical size (design tuned for small or large text) | font-optical-sizing (automatic) |
For any axis — including custom ones a font designer adds — there is the low-level property font-variation-settings:
.condensed {
font-variation-settings: "wdth" 75;
}Prefer the normal properties (font-weight and so on) when one exists. font-variation-settings sets all axes at once and is inherited as a whole, so setting it in one place can accidentally undo a value set elsewhere.
We tested this in Chrome with Windows' Bahnschrift variable font declared as font-weight: 300 700: paragraphs with weights 300, 500 and 700 each rendered at a visibly different thickness from the same file, and "wdth" 75 produced a narrower, condensed version.
Controlling Fake Bold and Italic
If text asks for bold or italic and no matching face exists, browsers synthesize one. Fake italics are just slanted letters and fake bold looks smudged. To forbid it — for example for a display font that has no bold — use:
.logo {
font-family: "Fancy Display", serif;
font-synthesis: none; /* never fake bold, italic or small-caps */
}Licensing
Fonts are software and have licences. Fonts on Google Fonts use open licences (mostly the SIL Open Font License) that allow free web use and self-hosting. Commercial fonts — even ones installed on your computer, like many fonts bundled with Windows or macOS — usually may not be uploaded to a website without a separate web licence. Always check before you self-host a font file.
Complete Example
Project structure:
my-site/
├── index.html
├── css/
│ └── style.css
└── fonts/
├── inter-variable.woff2
└── lora-italic.woff2index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web Fonts Demo</title>
<link rel="preload" href="fonts/inter-variable.woff2"
as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Placement Prep Checklist</h1>
<p>Revise <strong>DSA</strong> daily and read
<em>one system design article</em> every week.</p>
</body>
</html>css/style.css:
@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: "Lora";
src: url("../fonts/lora-italic.woff2") format("woff2");
font-weight: 400;
font-style: italic;
font-display: swap;
}
body {
font-family: "Inter", system-ui, sans-serif;
font-weight: 400;
line-height: 1.6;
}
h1 {
font-weight: 800;
}
em {
font-family: "Lora", Georgia, serif;
}Notice the ../ in the font URLs: paths in a CSS file are relative to the CSS file, which lives in css/. Result: the heading renders in extra-bold Inter, the paragraph in regular Inter, "DSA" in bold Inter (weight 700, taken from the same variable file), and the emphasised phrase in Lora italic. Until the files arrive, the text is shown in the system font and then swapped. If a font file is missing, the browser quietly uses the next font in the stack — in our Chrome test a family pointing at a non-existent file reported status error and the text simply appeared in Georgia.
Debugging Web Fonts
In Chrome DevTools, select an element and open the Computed tab: the "Rendered Fonts" section at the bottom shows which font was actually used (and how many glyphs).
The Network tab, filtered by "Font", shows whether the file was requested, its size, and any 404 or CORS error.
A font that is declared but never requested usually means the
font-familynames don't match exactly, or no text uses it.
Common Mistakes
Wrong relative path:
url()is relative to the CSS file.Different family names for each weight instead of one family with several
@font-facerules, leading to fake bold/italic.Loading too many weights: each static weight is a separate download. Use only what the design needs, or a variable font.
No fallback stack:
font-family: "Inter";alone falls back to the browser default (often a serif) if loading fails.Preloading without
crossorigin: the font gets downloaded twice.Shipping .ttf files instead of compressed WOFF2.
Ignoring the licence of a commercial font.
Interview / Exam Questions
1. What is @font-face?
An at-rule that declares a font family name and the font file(s) for it, with descriptors such as src, font-weight, font-style, font-display and unicode-range. After declaring it, you use the name in font-family like any other font.
2. What is the difference between FOIT and FOUT?
FOIT (Flash Of Invisible Text) hides text while the web font loads. FOUT (Flash Of Unstyled Text) shows the text in a fallback font first and swaps to the web font later. font-display: block leads to FOIT; swap leads to FOUT.
3. Which font format should you use today and why?
WOFF2. It is supported by all current browsers and uses Brotli compression, making files significantly smaller than WOFF, TTF or OTF.
4. What does font-display: optional do?
It gives the font an extremely short block period and no swap period. If the font isn't available almost immediately (for example from the cache), the fallback is used for that page view and never swapped, avoiding layout shift. The downloaded font is cached for later pages.
5. Why does a font preload need the crossorigin attribute?
Fonts are always fetched in CORS (anonymous) mode. A preload without crossorigin is made in a different mode, so the browser can't reuse it for the font request and downloads the font again.
6. What is a variable font and what is its advantage?
A single font file that contains continuous ranges along axes such as weight (wght) and width (wdth). One file replaces many static files, allows in-between values like 550, and usually reduces the total download size.
7. Inside @font-face, does font-weight: 700 make the text bold?
No. Inside @font-face it is a descriptor that tells the browser "this file is the 700 weight". The text becomes bold only when an element has font-weight: 700 (or bold), and the browser then picks this file.
Summary
Web fonts are declared with
@font-faceand used withfont-family; they download only when needed.Use WOFF2, one family name for all weights/styles, and always a fallback stack.
font-displaycontrols loading behaviour:swapfor readability,optionalfor stability.Google Fonts is quick to set up; self-hosting is often faster and better for privacy.
Preload only critical fonts, and always with
crossorigin.unicode-rangesplits fonts by script; variable fonts put many styles in one file.
With boxes, backgrounds and text covered, we are ready to understand how boxes are laid out on the page. The next chapter, The Display Property, is where layout begins.