Tutorials CSS Fonts & Typography
Ch 6 / 60
Chapter 6 Beginner 7 min read CSS

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.

What you'll learn
font-family — font stacks and generic families
font-size — px, em, rem, %, vw, clamp()
font-weight — numeric and keyword values
font-style, font-variant
line-height and letter-spacing
Google Fonts, @font-face and the font shorthand

font-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.

serif
Aa Bb Cc
Playfair Display — has serifs (small strokes at letter ends). Traditional, editorial feel.
sans-serif
Aa Bb Cc
Roboto — clean, no serifs. Modern UI default. Best for body text on screens.
monospace
Aa Bb Cc
Fira Code — equal-width characters. Used for code editors and terminal output.
cursive
Aa Bb Cc
Script / handwriting style. Use sparingly — best for decorative headings.
fantasy
Aa Bb Cc
Decorative / display fonts. Rarely used in production; avoid for body text.
CSS — Font stacks
/* 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;
}
Always end with a generic family
If none of your named fonts are available, the browser falls back to its default for that generic family. Without a generic keyword at the end, the browser may use a completely different font style. Always close every font stack with a generic family.

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.

Arial — The quick brown fox
Arial
Sans-serif · Universal
Helvetica — The quick brown fox
Helvetica Neue
Sans-serif · macOS/iOS
Segoe UI — The quick brown fox
Segoe UI
Sans-serif · Windows
Verdana — The quick brown fox
Verdana
Sans-serif · Universal · Wide
Tahoma — The quick brown fox
Tahoma
Sans-serif · Universal
Georgia — The quick brown fox
Georgia
Serif · Universal
Times New Roman
Times New Roman
Serif · Universal
Courier — The quick fox
Courier New
Monospace · Universal

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.

1
Visit fonts.google.com and choose a font
Search for a font, select the weights you need (e.g. 400 Regular, 700 Bold), then click "Get font" → "Get embed code".
2
Paste the <link> tag into your HTML <head>
Place it before your own <link rel="stylesheet"> so the font loads first.
3
Use the font name in font-family
Reference it exactly as Google Fonts names it, wrapped in quotes if it contains spaces.
HTML + CSS — Google Fonts setup
<!-- 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 */
Performance tip
The two <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).

CSS — @font-face setup
/* 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; }
Use WOFF2 format
WOFF2 is the most compressed web font format and is supported by all modern browsers. Always offer WOFF2 first, then WOFF as a fallback. Avoid TTF or OTF for web use — they are much larger files.

font-size — Sizing Text

font-size sets how large the text renders. CSS offers multiple units — each with different behaviour and use cases.

12pxCaption / small print
14pxSmall body / sidebar text
16pxBody text — browser default (1rem)
18pxLarge body / lead text
24pxH3 heading level
32pxH2 heading level
48pxH1 / hero heading
CSS — font-size units
/* 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 */
px
Absolute and predictable. Doesn't scale with user browser settings — less accessible. Good for borders and small decorative elements.
rem
Relative to the root <html> font-size. Recommended for font sizes — consistent, accessible, avoids inheritance compounding.
em
Relative to the parent element's font-size. Can compound across nested elements — use carefully. Good for padding/margin relative to local font size.
clamp()
Sets a minimum, preferred and maximum size in one declaration. Ideal for responsive headings — scales fluidly with viewport without media queries.

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.

100 Thin — The quick brown fox Thin
300 Light — The quick brown fox Light
400 Regular — The quick brown fox normal
500 Medium — The quick brown fox Medium
600 SemiBold — The quick brown fox SemiBold
700 Bold — The quick brown fox bold
800 ExtraBold — The quick brown fox ExtraBold
900 Black — The quick brown fox Black
CSS
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 must include the weight
If a font doesn't include a 300 weight and you set 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

CSS
/* 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 */
Normal — The quick brown fox jumps over the lazy dog
Italic — The quick brown fox jumps over the lazy dog
Oblique — The quick brown fox jumps over the lazy dog
Small-caps — The quick brown fox jumps over the lazy dog

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.

line-height: 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore.
line-height: 1.6 ✓
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore.
line-height: 2.4
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor.
CSS
/* 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; }
Best practice
Always use a unitless number for 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: -1px Tight tracking — headlines only
letter-spacing: 0 Normal tracking — body text default
letter-spacing: 0.05em Slightly open — readable at small sizes
letter-spacing: 0.15em Wide — labels and uppercase text
letter-spacing: 0.3em Very wide — decorative uppercase only
CSS
/* 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.

CSS — font shorthand
/* 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)
CSS — Type scale as variables
: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)); }
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
One printable page covering every CSS3 property, selector type, unit and layout system including all typography properties.
Download PDF

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.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js — all in one industry-ready curriculum. Build real projects with AI-powered tools and get placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js Tailwind CSS AI Tools

Quick Check — Chapter 6

Questions & Answers
A font stack is a comma-separated list of font names given to 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).
Three steps:

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.
A unitless number like 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.
Multiple Choice Questions
Question 1 of 5
Which value must always appear at the END of a font-family stack?
Question 2 of 5
Which font-size unit is relative to the root element's font size?
Question 3 of 5
What numeric value for font-weight is equivalent to bold?
Question 4 of 5
Which CSS function creates a fluid font-size that scales between a minimum and maximum?
Question 5 of 5
What file format should you use FIRST in a @font-face declaration for the best compression?
Chapter Summary
  • font-family takes 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. Add display=swap to prevent invisible text flash.
  • @font-face lets 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: use em units 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 :root for a consistent, easy-to-maintain typography system.
~7 min read
Beginner level
Chapter 6 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel