CSS Fonts & Typography
Typography is the single most impactful design decision you will make for a website. Great type choices — the right font family, size, weight and line height — transform a cluttered page into a confident, readable experience.
font-family — font stacks and
generic familiesfont-size — px, em, rem, %, vw,
clamp()font-weight — numeric and
keyword valuesfont-style, font-variantline-height and letter-spacing@font-face and
the font shorthandfont-family — Typeface & Font Stacks
The font-family property sets the typeface. You
provide a font stack — a comma-separated priority list. The browser tries each font from
left to right and uses the first one it finds installed. Always end with a generic family
keyword as a guaranteed fallback.
/* Font stack — browser tries each font left to right */
body {
font-family: 'Roboto', 'Segoe UI', Arial, sans-serif;
/* ↑ preferred ↑ Windows ↑ fallback ↑ generic */
}
h1, h2 {
font-family: 'Playfair Display', Georgia, serif;
}
code, pre {
font-family: 'Fira Code', 'Cascadia Code', Consolas, monospace;
}
/* Multi-word font names must be quoted */
.fancy {
font-family: 'Times New Roman', Times, serif;
}
/* System font stack (fast, no network request needed) */
.system {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
Roboto, Helvetica, Arial, sans-serif;
}
Web-Safe Fonts
Web-safe fonts are fonts that are pre-installed on the vast majority of operating systems — Windows, macOS, Linux, iOS and Android. Using them requires no network request, making your page load instantly with correct typography even without internet.
Google Fonts — Free Web Fonts
Google Fonts offers over 1,500 free, open-source typefaces hosted on Google's
CDN. No sign-up required — just copy the <link> tag and start
using the font in CSS.
<link rel="stylesheet"> so the font loads first.<!-- Step 1 & 2: Paste in <head>, before your stylesheet -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Playfair+Display:wght@400;700&display=swap" rel="stylesheet">
/* Step 3: Use in CSS */
body { font-family: 'Inter', sans-serif; }
h1, h2 { font-family: 'Playfair Display', serif; }
/* Popular Google Fonts pairings */
/* Inter (body) + Playfair Display (headings) — editorial */
/* Roboto (body) + Roboto Slab (headings) — clean, modern */
/* Open Sans (body) + Montserrat (headings) — corporate */
/* Lato (body) + Raleway (headings) — elegant minimal */
<link rel="preconnect"> tags tell the browser to connect
to Google's servers early, before the font request is made — reducing load time. The display=swap parameter in the URL ensures text stays visible during font
loading (no invisible text flash).
@font-face — Self-Hosted Fonts
@font-face lets you host font files directly on
your own server. This gives you full control over performance, versioning and privacy (no third-party
requests to Google).
/* Declare the font — place at the TOP of your stylesheet */
@font-face {
font-family: 'MyFont'; /* name you'll use in CSS */
src: url('fonts/myfont.woff2') format('woff2'),
url('fonts/myfont.woff') format('woff'); /* fallback */
font-weight: 400;
font-style: normal;
font-display: swap; /* show fallback while loading */
}
@font-face {
font-family: 'MyFont'; /* same family name */
src: url('fonts/myfont-bold.woff2') format('woff2');
font-weight: 700; /* separate declaration for bold */
font-style: normal;
font-display: swap;
}
/* Now use it like any font */
body { font-family: 'MyFont', Arial, sans-serif; }
font-size — Sizing Text
font-size sets how large the text renders. CSS
offers multiple units — each with different behaviour and use cases.
/* px — absolute, predictable, most used */
p { font-size: 16px; }
/* rem — relative to root (html) font-size (default 16px) */
/* Best for body text — respects user's browser font settings */
p { font-size: 1rem; } /* = 16px */
h2 { font-size: 2rem; } /* = 32px */
small { font-size: 0.875rem; } /* = 14px */
/* em — relative to the PARENT element's font-size */
/* Can compound (1.2em inside 1.2em = 1.44em) — use carefully */
.card { font-size: 1.1em; } /* 1.1 × parent size */
.caption{ font-size: 0.85em; } /* 85% of parent size */
/* % — same as em for font-size */
p { font-size: 100%; } /* same as 1em */
/* vw / vh — relative to viewport width/height (fluid type) */
h1 { font-size: 5vw; } /* 5% of viewport width */
/* clamp(min, ideal, max) — fluid and bounded — BEST for headings */
h1 { font-size: clamp(1.8rem, 4vw, 3.5rem); }
/* ↑min ↑scales ↑max — never too small or too big */
<html>
font-size. Recommended for font sizes — consistent, accessible, avoids inheritance
compounding.font-weight — Text Thickness
font-weight controls how thick or thin the text
strokes appear. Use numeric values (100–900) for maximum control, or keyword shorthands.
p { font-weight: 400; } /* or: normal */
strong, b { font-weight: 700; } /* or: bold */
h1 { font-weight: 800; } /* ExtraBold for impact */
.label { font-weight: 600; } /* SemiBold for labels */
.caption { font-weight: 300; } /* Light for captions */
/* Relative keywords */
.bolder { font-weight: bolder; } /* one step heavier than parent */
.lighter{ font-weight: lighter; } /* one step lighter than parent */
font-weight: 300, the
browser will use the nearest available weight (usually 400). This is called font synthesis.
When using Google Fonts, always select the weights you need in the URL query string.
font-style & font-variant
/* font-style */
em, cite { font-style: italic; } /* true italic if available */
.oblique { font-style: oblique; } /* slanted (faked if no italic) */
.normal { font-style: normal; } /* reset back to upright */
/* font-variant — small caps */
.smallcaps{ font-variant: small-caps; }
/* Text renders in uppercase but at a smaller size for lowercase letters */
/* Useful for acronyms, section labels, pull quotes */
/* Modern: font-variant-numeric */
.tabular { font-variant-numeric: tabular-nums; }
/* Numbers take equal widths — great for tables and timers */
line-height — Vertical Spacing
line-height sets the distance between lines of
text. It is one of the most impactful readability settings — too tight and text feels cramped, too loose and
it feels disconnected.
/* Unitless number — RECOMMENDED. Scales with font-size automatically. */
body { line-height: 1.6; } /* good for body text */
h1 { line-height: 1.15; } /* tighter for large headings */
h2 { line-height: 1.25; }
/* px — absolute, doesn't scale with font-size changes */
p { line-height: 28px; }
/* em / % — relative to the element's font-size */
p { line-height: 160%; } /* same as 1.6 if font-size is the default */
/* normal — browser default (~1.2). Usually too tight for body text. */
p { line-height: normal; }
line-height (e.g.
1.6 not 1.6em). A unitless value is
inherited as a multiplier, so child elements with different font sizes scale proportionally. A fixed px or em value won't scale when children
have different sizes.
letter-spacing & word-spacing
/* letter-spacing — space between characters */
h1 { letter-spacing: -0.02em; } /* tight for large display text */
p { letter-spacing: 0.01em; } /* slightly open body text */
.label { letter-spacing: 0.08em; } /* uppercase labels */
.badge { letter-spacing: 0.12em; } /* all-caps badge */
/* word-spacing — extra space between words */
p { word-spacing: 0.05em; } /* slightly more space */
.headline { word-spacing: -0.02em; } /* tighten for display text */
font Shorthand & Building a Type Scale
The font shorthand combines multiple font
properties into one declaration. The order is strict — if you use it, you must include at least font-size and font-family.
/* font: [style] [variant] [weight] size[/line-height] family */
p { font: 400 16px/1.6 'Roboto', sans-serif; }
h1 { font: 700 2.5rem/1.15 'Playfair Display', serif; }
code { font: 500 0.9em/1.5 'Fira Code', monospace; }
/* italic + small-caps example */
cite { font: italic small-caps 400 1rem/1.6 Georgia, serif; }
A type scale is a set of pre-defined font sizes with a consistent ratio. Using a scale (rather than arbitrary numbers) creates visual harmony across an entire site.
| Variable | Use | Value (1.25 ratio) |
|---|---|---|
| --text-xs | Captions, badges, labels | 0.64rem (≈10px) |
| --text-sm | Secondary text, metadata | 0.8rem (≈13px) |
| --text-base | Body text — base size | 1rem (16px) |
| --text-lg | Lead paragraph, large body | 1.25rem (20px) |
| --text-xl | H3 / subheading | 1.563rem (25px) |
| --text-2xl | H2 / section heading | 1.953rem (31px) |
| --text-3xl | H1 / page heading | 2.441rem (39px) |
| --text-4xl | Hero / display heading | 3.052rem (49px) |
:root {
--text-xs: 0.64rem;
--text-sm: 0.8rem;
--text-base: 1rem;
--text-lg: 1.25rem;
--text-xl: 1.563rem;
--text-2xl: 1.953rem;
--text-3xl: 2.441rem;
--text-4xl: 3.052rem;
}
body { font-size: var(--text-base); }
small { font-size: var(--text-sm); }
h3 { font-size: var(--text-xl); }
h2 { font-size: var(--text-2xl); }
h1 { font-size: clamp(var(--text-2xl), 4vw, var(--text-4xl)); }
Try It Yourself
This demo shows a complete typography system using all the properties covered in this
chapter. Try changing the --font-body or --font-heading variables to see the whole page update.
Quick Check — Chapter 6
font-family. The browser tries each font from left to right and uses the
first one it finds installed on the user's device.Example:
font-family: 'Roboto', 'Segoe UI', Arial, sans-serif;It must always end with a generic family keyword (
serif, sans-serif, monospace, etc.) because generic families are guaranteed to have a match
on every device. Without it, if all named fonts are unavailable, the browser picks its own default,
which could be completely wrong for your design.
em is relative to the parent element's
font size. If a parent has font-size: 20px and a child sets font-size: 1.2em, the child is 24px. If there's another nested child at
1.2em, it becomes 28.8px — compounding inheritance makes this
tricky.rem is relative to the root <html> element's font size (typically 16px). 1rem is always 16px regardless of nesting depth. This makes layouts
consistent and avoids compounding.Recommendation: Use
rem for font sizes across your
design system. Use em for padding/margin that should scale with the
local font size (e.g. button padding).
1. Go to fonts.google.com, find a font, select weights and click "Get embed code".
2. Paste the two
<link rel="preconnect"> and
the main <link> tag into your HTML <head> — before your own stylesheet.3. Use the exact font name in
font-family in your
CSS: font-family: 'Inter', sans-serif;The
display=swap parameter in the Google Fonts URL ensures text
remains visible during font loading, preventing invisible text flashes.
line-height: 1.6 is inherited by child
elements as a multiplier — each child multiplies it by its own font size. A child with
a 12px font and inherited 1.6 line-height gets 19.2px of line
spacing, which is proportional.A fixed value like
line-height: 24px or 1.5em is inherited as a computed pixel value. A small child
element at 12px inherits 24px line-height — far too much — and a large heading at 48px also inherits
24px — far too little.Unitless is always the safest choice for
line-height.
clamp(min, preferred, max) sets a value that scales fluidly between
a minimum and maximum — perfect for responsive typography without media queries.Example:
font-size: clamp(1.8rem, 4vw, 3.5rem);On a small screen (320px wide): 4vw = 12.8px → but minimum is 1.8rem (28.8px) → uses 28.8px
On a medium screen (900px): 4vw = 36px → within range → uses 36px
On a large screen (1400px): 4vw = 56px → but maximum is 3.5rem (56px) → capped at 56px
This replaces multiple media query breakpoints for font-size with a single, elegant declaration.
font-family stack?font-size unit is relative to the
root element's font size?
font-weight is
equivalent to bold?@font-face declaration for the best compression?font-familytakes a prioritised font stack — browser tries each font left to right. Always end with a generic family (sans-serif,serif,monospace).- Web-safe fonts (Arial, Georgia, Verdana, Courier) are pre-installed everywhere and need no network request.
- Google Fonts — copy the
<link>tag into your HTML<head>before your stylesheet, then use the font name in CSS. Adddisplay=swapto prevent invisible text flash. @font-facelets you self-host fonts. Use WOFF2 format first for best compression.font-size: use rem for body text (consistent, accessible), clamp() for responsive headings (fluid between min and max).font-weight: numeric values 100–900 give full control.400= normal,700= bold. Font must include that weight.line-height: always use a unitless number (e.g.1.6) so it scales proportionally with inherited font sizes. 1.5–1.7 for body, 1.1–1.25 for headings.letter-spacing: useemunits so spacing scales with font size. Negative values tighten display headings; positive values open uppercase labels.- Define font sizes as CSS variables in a type scale on
:rootfor a consistent, easy-to-maintain typography system.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.