Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 139 ยท Text and Fonts

Fonts

Choose typefaces with font-family and font stacks, size text with font-size, set weight and style, and combine everything with the CSS font shorthand.

The typeface you choose sets the tone of a page before anyone reads a word. A rounded sans-serif feels friendly, a classic serif feels bookish, and a monospaced font says "code". CSS gives you a small set of font-* properties to pick the family, size, weight and style of text.

In this lesson you will learn how browsers choose a font from a font stack, why every stack should end with a generic family, how to size text sensibly, and how to use the font shorthand without accidentally resetting things. Loading your own font files is the topic of the next lesson, Web Fonts.

font-family and font stacks

The font-family property takes a comma-separated list of font names. The browser walks the list from left to right and uses the first font that is available on the device. That list is called a font stack.

CSS
body {
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

A few rules for writing font names:

  • Quote names that contain spaces or digits, such as "Segoe UI". Quoting simple names like Arial is allowed but not needed.
  • Never quote generic family keywords like sans-serif. "sans-serif" in quotes means "a font literally named sans-serif", which does not exist.
  • The fallback happens per character, too. If the first font lacks a glyph (say, a Japanese character or an emoji), the browser looks further down the stack for that character only.

Generic families

Generic families are keywords that always resolve to some installed font. Every stack should end with one, so the text never falls back to an unexpected default.

Keyword What you get
serif Letters with small strokes (serifs) at the ends, like Georgia or Times
sans-serif Clean letters without serifs, like Arial or Helvetica
monospace Every character the same width, ideal for code
cursive Handwriting-style script
fantasy Decorative display fonts (unpredictable; rarely useful)
system-ui The operating system's interface font
ui-serif, ui-sans-serif, ui-monospace, ui-rounded The system's own variants of each style, where available
Generic families

The exact fonts depend on your operating system, which is the point: generic families adapt to whatever the reader has.

System font stacks

A system font stack uses fonts that ship with the reader's device instead of downloading any. It loads instantly, looks native and supports many languages. For a lot of sites it is the best choice.

CSS
:root {
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-serif: ui-serif, Georgia, Cambria, "Times New Roman", serif;
  --font-mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
}

body { font-family: var(--font-sans); }
code, pre { font-family: var(--font-mono); }

Storing stacks in custom properties means you can switch the whole site's typography by editing one line.

Form controls don't inherit fonts by default. Buttons, inputs and text areas use the browser's own font unless you say otherwise. Add button, input, select, textarea { font: inherit; } to your base styles. Inheritance explains why.

font-size

font-size sets the size of the text. You can use any length, but two units cover nearly every case:

  • rem โ€“ relative to the root (<html>) font size, which is 16px by default. 1.5rem is 24px for most readers.
  • em โ€“ relative to the parent's font size. Useful for things that should scale with their surroundings, like an icon inside a button.

There are also keywords (small, medium, large, x-large and so on) and relative keywords (smaller, larger), but lengths are easier to reason about. Units compares every option.

Don't set the root font size in pixels. Writing html { font-size: 16px; } overrides the text size the reader chose in their browser settings. Leave the root alone (or use a percentage like 100%) and size everything else in rem.

em compounds, rem doesn't

Each nested em box multiplies its parent's size, so text snowballs. The rem boxes all point back to the root, so they stay equal. Later, Fluid Typography shows how to make sizes grow smoothly with the screen using clamp().

font-weight

font-weight controls how bold text is. It accepts numbers from 1 to 1000, plus the keywords normal (400) and bold (700). The traditional steps are:

Value Common name
100 Thin
300 Light
400 Regular (normal)
500 Medium
600 Semibold
700 Bold (bold)
900 Black

You only see a real difference if the font has that weight. A system font might ship with just regular and bold; asking for 300 then gives you the closest available weight. If no bold exists at all, the browser fakes one by smearing the strokes (called synthetic bold), which tends to look muddy. Variable fonts, covered in Web Fonts, can render any weight in their range.

The relative keywords bolder and lighter pick a weight relative to the parent's, which is handy inside already-bold headings.

font-style

font-style switches between normal, italic and oblique. Italic uses the font's dedicated italic design, with genuinely different letter shapes; oblique just slants the regular letters. If a font has no italic, the browser slants it for you.

Weight and style

Remember that <strong> and <em> carry meaning (importance and stress), while font-weight and font-style are purely visual. Formatting Text covers the HTML side.

Other useful font properties

A few more properties are worth knowing:

  • font-variant-numeric: tabular-nums makes all digits the same width, so numbers line up in tables and timers. oldstyle-nums gives elegant numerals that blend into running text.
  • font-variant-caps: small-caps shows lowercase letters as small capitals, if the font supports them.
  • font-feature-settings switches OpenType features on or off by their four-letter tags, such as "liga" 0. Prefer the specific font-variant-* properties when one exists.
  • font-synthesis: none stops the browser from faking bold and italic, which is useful when a font simply does not have them.
Tabular numbers

Not every font has proportional digits by default, so the effect varies by system, but in fonts that do, the tabular column lines up perfectly.

The font shorthand

The font shorthand sets several properties at once. Its order matters:

CSS
/* font: [style] [variant] [weight] [stretch] size[/line-height] family */
h1 { font: italic 700 2.5rem/1.1 Georgia, serif; }
p  { font: 1rem/1.6 system-ui, sans-serif; }

The size and family are required, and they must come last, in that order. The optional style, weight and so on go before them, and a line height can follow the size after a slash.

The shorthand resets what you leave out. font: 1rem system-ui sets font-weight back to normal, font-style back to normal and line-height back to normal, even if an earlier rule set them. Use the shorthand for base styles, and the individual properties when you only want to change one thing.

There is one more special use: font: inherit copies all font settings from the parent, which is exactly what form controls need.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens with font-family: "Fancy Sans", Arial, sans-serif; if Fancy Sans is not installed?

    The browser walks the stack from left to right and uses the first available font. The generic family at the end guarantees there is always a match.
  2. 2. Why should you not write "sans-serif" in quotes?

    Generic families are keywords. Quoting them turns them into font names, which won't match anything.
  3. 3. A list nests four levels deep, and each level has font-size: 1.2em. What happens to the text?

    em compounds: each level multiplies its parent's computed size. Use rem to stay relative to the root instead.
  4. 4. Which declaration is a valid use of the font shorthand?

    The size and family are required and must come last, size first. Style and weight go before them, and a line height can follow the size after a slash.
  5. 5. You need numbers in a price table to line up in columns. Which property helps?

    Tabular numerals all share one width, so digits stack neatly in columns.

Summary

  • font-family takes a font stack; the browser uses the first available font, even per character.
  • End every stack with a generic family such as sans-serif, serif, monospace or system-ui, unquoted.
  • System font stacks load instantly and look native.
  • Size text in rem; em compounds through nesting. Never pin the root size in pixels.
  • font-weight runs from 1 to 1000; you only see the weights the font actually has.
  • font-style: italic uses the font's italic design; oblique slants the regular one.
  • The font shorthand needs size then family at the end, and it resets everything you leave out.

Next: Web Fonts, where you load your own typefaces with @font-face and keep them fast.