How to Change Font in CSS
What you’ll build or solve
Change a page’s font with CSS so headings and body text use the typeface you want. You’ll set font-family, add sensible fallbacks, and target the right elements so the font applies where you expect.
When this approach works best
This approach works best when you want consistent typography across a page or site.
Learn CSS on Mimo
Use it when you:
- Set a default font for the whole site.
- Use one font for headings and another for body text.
- Apply a different font to a component like a navbar or code block.
Skip this approach when:
- You need a custom web font but haven’t added the font files or a font service.
font-familyalone can’t download fonts. - You want per-character styling like a rainbow headline. That’s harder to maintain and usually not worth it.
Prerequisites
- An HTML file linked to a CSS file or a
<style>tag - Basic comfort with CSS selectors, such as element selectors and classes
Step-by-step instructions
Step 1: Use font-family to set the font
Set a font with font-family, then list fallbacks in case the first font isn’t available.
Option A: Set a default font for the whole page (most common)
CSS
body {
font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
Option B: Use different fonts for headings and body text
CSS
body {
font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
h1,
h2 {
font-family: Georgia, "Times New Roman", Times, serif;
}
Option C: Apply a font to one component with a class
HTML
<nav class="nav">
<a href="/docs">Docs</a>
<a href="/pricing">Pricing</a>
</nav>
CSS
.nav {
font-family: Arial, sans-serif;
}
What to look for
- Put quotes around font names with spaces, like
"Segoe UI". - Always end with a generic family like
sans-serif,serif, ormonospace. - If the font doesn’t change, the font may not be installed or loaded, or another rule may override yours.
Examples you can copy
Example 1: Clean default font for an app-like UI
CSS
body {
font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
Example 2: Serif headings, sans-serif body
CSS
body {
font-family: Arial, sans-serif;
}
h1,
h2,
h3 {
font-family: Georgia, "Times New Roman", Times, serif;
}
Example 3: Use monospace for code-like text
HTML
<p>Run <span class="code">npm install</span> to install packages.</p>
CSS
.code {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}
Common mistakes and how to fix them
Mistake 1: Forgetting fallbacks
What you might do:
CSS
body {
font-family: FancyFont;
}
Why it breaks:
If FancyFont isn’t installed or loaded, the browser picks a default that may not match your design.
Correct approach:
Add fallbacks and a generic family at the end.
CSS
body {
font-family: FancyFont, Arial, sans-serif;
}
Mistake 2: Forgetting quotes when a font name isn't a valid identifier
What you might do:
CSS
body {
font-family: Source Sans 3,
Arial, sans-serif;
}
Why it breaks:
Unquoted multi-word names work only when each word is a valid CSS identifier. 3 starts with a digit, so the browser ignores the entire font-family declaration, including the fallbacks, unless you quote the name or escape the digit. (Segoe UI happens to work unquoted, but quoting every name with spaces or digits is the safe habit.)
Correct approach:
Wrap the font name in quotes.
CSS
body {
font-family: "Source Sans 3",
Arial, sans-serif;
}
Mistake 3: Setting the font on one element, expecting it to change everywhere
What you might do:
CSS
h1 {
font-family: Georgia, serif;
}
Why it breaks:
Only the h1 changes. Everything else keeps the old font.
Correct approach:
Set a base font on body, then override where needed.
CSS
body {
font-family: Arial, sans-serif;
}
h1 {
font-family: Georgia, serif;
}
Troubleshooting
- If the font doesn’t change, check if the font is installed or loaded. If it’s a web font, confirm you added it correctly.
- If only some text changes, look for a more specific selector overriding your rule.
- If the font name has spaces, confirm it’s quoted.
- If a component won’t pick up the font, check for
font-familyrules applied directly to children. - If nothing updates, confirm your stylesheet is loading and not returning a 404.
Quick recap
- Use
font-familyto change fonts in CSS. - List fallbacks, ending with a generic family like
sans-serif. - Quote font names with spaces.
- Set a base font on
body, then override specific sections as needed. - If it doesn’t apply, check font loading, selector overrides, and CSS file loading.
Join 40M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot