Skip to main content

CSS Text & Fonts

Web Fonts & Typography

Written by Updated Web Fonts & Typography exercises

Set readable type, load web fonts responsibly, and understand the properties that shape a line of text. You will work with readable line height and measure, resilient font stacks, @font-face loading, font-display, and variable font axes.

Good typography is mostly about three numbers: the font size, the line height, and how long a line is. Loading a pretty web font comes after those, and it needs care too, or your text flickers or stays invisible while the font downloads.

Choosing fonts is covered in CSS Font Family and sizes in CSS Font Size. This lesson brings them together.

Readable defaults

Example

css

body {
  font-family: Inter, system-ui, sans-serif;
  font-size: 1rem;
  line-height: 1.6;
  font-synthesis: none;
}

h1, h2, h3 {
  line-height: 1.15;
  text-wrap: balance;
}
  • line-height: 1.6 without a unit gives comfortable body text. A unitless value inherits as a ratio, so headings with bigger text still get the right spacing. Headings look better tighter, around 1.15.
  • font-synthesis: none stops the browser from faking bold or italic when the font file does not have them. Fake bold looks smudged.
  • text-wrap: balance evens out heading lines.

Keep paragraphs to about 45 to 75 characters per line. In CSS, max-width: 65ch on the text container is a good start.

Loading a web font

Example

css

@font-face {
  font-family: "Newsreader";
  src: url("https://fonts.gstatic.com/s/newsreader/v26/cY9AfjOCX1hbuyalUrK439HyjJBG.woff2") format("woff2");
  font-weight: 300 800;
  font-style: normal;
  font-display: swap;
}

@font-face gives a font file a name you can use in font-family. Use the WOFF2 format, which is the smallest. font-display: swap shows the text in a fallback font straight away and swaps to the web font when it arrives, instead of hiding the text.

Always list fallbacks after the web font, such as system-ui, sans-serif, so there is something sensible to show while it loads, or if it fails.

Variable fonts

Example

css

.headline {
  font-family: "Newsreader", serif;
  font-weight: 720;
  font-variation-settings: "opsz" 48;
}

A variable font is one file that contains every weight and width. You can use any weight, like 720, not just 400 and 700. One variable file is often smaller than downloading four separate weights.

Typography for other languages and notation

Example

css

.east-asian-emphasis {
  text-emphasis: filled sesame;
  text-emphasis-position: over right;
}

ruby {
  ruby-position: over;
}

math {
  font-family: math;
}

.formula-label {
  baseline-shift: 15%;
}

text-emphasis adds the emphasis dots used in Chinese and Japanese, ruby-position places ruby annotations, and the math font family picks a font made for formulas. Use the right HTML first (ruby, MathML); CSS only adjusts how it looks.

A big first letter

Example

css

.article-intro::first-letter {
  initial-letter: 3 2;
  margin-inline-end: 0.35rem;
}

initial-letter: 3 2 makes the first letter three lines tall, sunk two lines into the paragraph, like in a magazine. Browsers that do not support it show a normal first letter, which is fine.

Typography Properties

Property or descriptorPurposeExample
font-familySets the prioritized font family list.font-family: Inter, sans-serif
font-sizeSets the text size.font-size: 1rem
line-heightSets the height of each line box.line-height: 1.6
font-displayControls how a downloaded font is displayed.font-display: swap
text-emphasisAdds typographic emphasis marks.text-emphasis: filled sesame
ruby-positionPositions ruby annotations.ruby-position: over
mathSelects a mathematical generic font.font-family: math
baseline-shiftAdjusts the baseline of inline or SVG content.baseline-shift: 15%
initial-letterSizes and sinks an initial letter.initial-letter: 3 2

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Web Fonts & Typography example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

body {
  font-family: Inter, system-ui, sans-serif;
  font-size: 1rem;
  line-height: 1.6;
  font-synthesis: none;
}

h1, h2, h3 {
  line-height: 1.15;
  text-wrap: balance;
}

@font-face {
  font-family: "Newsreader";
  src: url("https://fonts.gstatic.com/s/newsreader/v26/cY9AfjOCX1hbuyalUrK439HyjJBG.woff2") format("woff2");
  font-weight: 300 800;
  font-style: normal;
  font-display: swap;
}

.headline {
  font-family: "Newsreader", serif;
  font-weight: 720;
  font-variation-settings: "opsz" 48;
}

.east-asian-emphasis {
  text-emphasis: filled sesame;
  text-emphasis-position: over right;
}

ruby {
  ruby-position: over;
}

math {
  font-family: math;
}

.formula-label {
  baseline-shift: 15%;
}

.article-intro::first-letter {
  initial-letter: 3 2;
  margin-inline-end: 0.35rem;
}
  </style>
</head>
<body>
  <main class="article"><h1 class="headline">A readable headline</h1><p>Good typography depends on line height, measure, weight, and a dependable fallback stack.</p></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
@font-face1123.53.1
Variable font settings62176211
text-emphasis9999467
ruby-position84843818.2
font-family: math10910914926.2
baseline-shift1791494
initial-letter110110NoNo

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Test like a reader

Try your text with the browser's text size set larger, on a slow connection, with very long headings and with accented letters. Count the font files your page downloads: two or three is plenty.

Web Fonts & Typography exercises

Bold

Make the first paragraph bold.

The HTML your code works on
<h1 class="title">Our Story</h1>
<p class="text">We started baking bread in 1990 in a small kitchen.</p>
<p class="note">Open daily.</p>
<a href="#">Read more</a>

Result

Check your answer to see it here.