CSS Basics

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-family: 'Inter', Arial, sans-serif;

Font Size

Control the size of text using various units.

font-size: 16px; /* or 1rem, 1.2em */

Font Weight

Set the thickness or boldness of font characters.

font-weight: 400; /* 100-900 or keywords */

Font Style

Apply italic or oblique styling to text.

font-style: italic; /* normal, italic, oblique */

Font Variant

Enable special font features like small caps.

font-variant: small-caps;

Font Shorthand

Combine multiple font properties in one declaration.

font: 400 16px/1.5 'Inter', sans-serif;

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

Traditional fonts with decorative strokes. Great for print and formal content.
Examples: Georgia, Times New Roman
Use for: Articles, books, formal documents
Web fonts: Playfair Display, Merriweather

Sans-serif Fonts

Clean, modern fonts without decorative strokes. Excellent for digital screens.
Examples: Arial, Helvetica
Use for: UI, websites, mobile apps
Web fonts: Inter, Roboto, Open Sans

Monospace Fonts

Fixed-width fonts where each character takes the same space.
Examples: Courier New, Monaco
Use for: Code, data tables
Web fonts: Fira Code, JetBrains Mono

Modern Font Stack Example

/* System font stack - Fast loading */ font-family: 'Inter', /* Web font (if loaded) */ -apple-system, /* macOS Safari */ BlinkMacSystemFont, /* macOS Chrome */ 'Segoe UI', /* Windows */ Roboto, /* Android */ sans-serif; /* Generic fallback */ /* Performance optimized */ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');

Font Loading Best Practices

  • •Use font-display: swap for 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

1 / 4
1

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.

1 of 4
💡Always Self-Host Critical Fonts
Self-hosting fonts in /public/fonts avoids third-party DNS lookups, gives you full control over caching headers, and eliminates GDPR concerns from loading fonts from Google servers.

🎯 Quick Check

Which font-display value shows fallback text immediately while the custom font loads?

CSS Fonts: summary

Key points

  • rem sizes are relative to the root font size and respect the user's browser setting; em is relative to the parent.
  • font-display: swap shows fallback text immediately while the web font loads.
  • Use woff2 files; 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.