CSS Web Fonts
You already know font-family, size, and weight. Web fonts let you load a typeface that is not already on the visitor’s device — declare it with @font-face, then use the family name like any other font, still ending with a calm fallback.
Pieces of @font-face
📊 @font-face at a glance
| Piece | Everyday job |
|---|---|
font-family |
The name you invent and later use in CSS |
src |
Where the font file(s) live (url(...) and often format(...)) |
font-weight / font-style |
Which face this file represents (regular, bold, italic…) |
font-display |
How text behaves while the font is loading (e.g. swap) |
Think of it as two calm steps: register the face with @font-face, then call that same family name in your normal rules. Skipping either step leaves you with a stack that never finds the custom file.
A minimal @font-face
Give the face a clear name and point src at a font file on your site (or CDN).
Basic @font-face
css@font-face {
font-family: "Site Sans";
src: url("/fonts/site-sans.woff2") format("woff2");
font-weight: 400;
font-style: normal;
}
Offer more than one format
Modern browsers prefer woff2. Listing woff next keeps older browsers covered. Put the best format first.
woff2 then woff
css@font-face {
font-family: "Site Sans";
src:
url("/fonts/site-sans.woff2") format("woff2"),
url("/fonts/site-sans.woff") format("woff");
font-weight: 400;
font-style: normal;
}
font-display — show text sooner
font-display: swap lets the browser show fallback text first, then swap in the web font when it arrives — often better than a long invisible gap.
Display swap
css@font-face {
font-family: "Site Sans";
src: url("/fonts/site-sans.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
Use the family you declared
After @font-face, call the same font-family name in your rules — and keep a generic fallback.
Apply the custom family
cssbody {
font-family: "Site Sans", system-ui, "Segoe UI", sans-serif;
font-size: 1rem;
line-height: 1.6;
}
A calm self-host habit
Host only the weights you need, prefer woff2, set font-display, and always keep a system fallback stack.
Heading with loaded bold face
css@font-face {
font-family: "Site Sans";
src: url("/fonts/site-sans-bold.woff2") format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}
h1 {
font-family: "Site Sans", Georgia, serif;
font-weight: 700;
}
Try it yourself
- Write an
@font-faceblock with a customfont-familyname and awoff2src. - Add a second
srcline forwoff. - Set
font-display: swap. - Apply
"Your Name", sans-serifonbody. - Add a separate
@font-facefor weight700if you have a bold file — then usefont-weight: 700on a heading.
Common mistakes
- Declaring
@font-facebut never using that exact family name infont-family - Skipping a fallback stack (broken look if the file fails to load)
- Loading many weights and styles you never use (slow pages)
- Leaving text invisible for a long time with no
font-displaystrategy - Mixing this lesson with text-shadow / effects (those come next)
Summary
@font-faceregisters a custom family name and points at font files- Prefer
woff2, optionally fall back towoff font-display: swapoften keeps text readable while fonts load- Use the same family name in CSS with a system/generic fallback
- Load only the faces you need; effects like text-shadow are a later lesson
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top