CSS Fonts
CSS Font properties define the typeface characteristics of text, including font-family, font-size, font-weight, font-style, and font-variant, enabling precise typography control and web font integration.
How do you change the font in CSS?
Set font-family to a list of fonts, ending with a generic family as a fallback: font-family: "Inter", system-ui, sans-serif;. Control size with font-size (prefer rem), weight with font-weight (100–900) and style with font-style. Load custom fonts with @font-face or a service like Google Fonts.
@font-face {
font-family: "Inter";
src: url("/fonts/inter.woff2") format("woff2");
font-weight: 100 900;
font-display: swap;
}
body {
font-family: "Inter", system-ui, sans-serif;
font-size: 1rem; /* usually 16px */
}Core Font Properties
Font Family
Specify the typeface for text elements with fallback options.
Font Size
Control the size of text using various units.
Font Weight
Set the thickness or boldness of font characters.
Font Style
Apply italic or oblique styling to text.
Font Variant
Enable special font features like small caps.
Font Shorthand
Combine multiple font properties in one declaration.
Interactive Font Demo
Explore comprehensive font styling with Google Fonts integration, interactive controls, and performance optimization techniques. Test different typography combinations!
Code Editor
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap">
<p class="system">System font stack: loads instantly.</p>
<p class="serif">Georgia, a classic serif.</p>
<p class="inter">Inter from Google Fonts (400 and <b>700</b>).</p>
<p class="mono">Monospace for code: const x = 1;</p>Preview
Font Categories & Best Practices
Serif Fonts
Sans-serif Fonts
Monospace Fonts
Modern Font Stack Example
Font Loading Best Practices
- •Use
font-display: swapfor better loading performance - •Preload critical fonts with
<link rel="preload"> - •Limit font weights and styles to reduce load times
- •Use system fonts as fallbacks for instant rendering
- •Consider variable fonts for multiple weights in one file
Your Learning Path
Master CSS font properties and typography
What You've Learned
- ▶Font family, size, weight, and style properties
- ▶Different font categories and their use cases
- ▶Web font integration and Google Fonts
- ▶Font loading optimization techniques
- ▶Typography best practices and font stacks
What's Next
- ▶CSS Icons - Icon fonts and SVG integration
- ▶CSS Links - Link styling and states
- ▶CSS Lists - List styling and customization
- ▶CSS Tables - Table styling and responsive design
Pro Font Performance Tip
Always include system fonts in your font stack for instant text rendering, then progressively enhance with web fonts. Use font-display: swapto prevent invisible text during font load.
CSS Fonts — step by step
font-family Stack
Always provide a font stack: list your custom font first, then fallbacks, ending with a generic family like serif or sans-serif.
🎯 Quick Check
Which font-display value shows fallback text immediately while the custom font loads?
CSS Fonts: summary
Key points
remsizes are relative to the root font size and respect the user's browser setting;emis relative to the parent.font-display: swapshows fallback text immediately while the web font loads.- Use
woff2files; they're the smallest and supported everywhere. - A system font stack (
system-ui, sans-serif) loads instantly with no download.
Frequently asked questions
›Should I use px, em or rem for font size?
Use rem for most font sizes. It scales with the user's preferred text size and doesn't compound the way nested em values do.
›How do I use Google Fonts?
Copy the <link> tag from fonts.google.com into your HTML <head>, then use the font name in font-family. For better performance, self-host the font or use a framework loader such as Next.js next/font.