SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
TypographyColor and Backgrounds Setting type and color for readable text
Typography and Color

Typography

Choose and style fonts to create readable, responsive text with a clear hierarchy.

Typography is the arrangement of type to make written language legible, readable, and visually appealing. On a web page, choices such as font, size, weight, and spacing affect both how easily people read the content and the mood it conveys.

A page also needs a hierarchy: an arrangement that guides the reader toward the most important information. A larger, bolder heading can stand apart from a paragraph, while consistent alignment and spacing help the text feel connected. CSS gives you controls for these choices, and HTML gives the text its structure and meaning.

Typefaces, fonts, and families

A typeface is the design of the lettering. A font is a set of characters, including letters, numbers, and symbols, in a particular design and style. The terms are often used interchangeably. A font family groups related fonts, which can vary in weight, slant, or width while sharing a design.

Some common kinds of type have recognizable features:

  • Serif typefaces have small lines at the ends of characters. Georgia and Times New Roman are examples.
  • Sans-serif typefaces lack those small lines. Arial and Helvetica are examples.
  • Monospace fonts give each character the same amount of space. Courier New is an example.
  • Display fonts are decorative fonts often used for titles or headlines.

The serif H has finishing strokes, the sans-serif H has plain ends, and the monospace letters each occupy equal width.

These choices contribute to a page's visual voice. For example, a luxury brand might choose a serif typeface to suggest elegance, use large bold headings for emphasis, and leave ample space around text. The choice should also keep the content readable for its intended audience.

Choose a font family and fallbacks

The CSS font-family property accepts a prioritized, comma-separated list of families, often called a font stack:

p {
  font-family: Arial, Helvetica, sans-serif;
}

The browser tries Arial first, then Helvetica when needed. Even when Arial is installed, a missing character can come from a later font in the list. The final value, sans-serif, is a generic family: it lets the browser use an available font with that general style. Include a generic family so there is an alternative when the named choices are unavailable. Put names containing spaces in quotes, such as "Times New Roman".

The browser tries Arial first, Helvetica next when needed, then the generic sans-serif family.

Load a custom font

A font can also come from a file. The @font-face rule gives the font a name and identifies its resources:

@font-face {
  font-family: "MyFont";
  src: url("myfont.woff2") format("woff2"),
       url("myfont.woff") format("woff");
  font-weight: normal;
  font-style: normal;
}

body {
  font-family: "MyFont", Arial, sans-serif;
}

The first rule names the font and lists files in two formats. The second uses that name and supplies fallbacks. If the custom font cannot load, the browser can move to the next choice, keeping the text legible. Font loading can affect performance, so check load times and test the fallback under different network conditions. Ensure you have the right to use and distribute a font file.

Set size, weight, and style

The family selects a design; size, weight, and style help give text its role within the page.

p {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  font-weight: normal;
}

h1 {
  font-family: Georgia, serif;
  font-size: 2em;
  font-weight: 700;
}

em {
  font-style: italic;
}

Here the heading uses a serif family, a larger size, and a bold weight to distinguish it from the paragraph. The emphasized text uses italics. Check the result together: a hierarchy should make important text easy to find while leaving the paragraph comfortable to read.

A large bold heading stands apart from smaller body text, where italics distinguish one word.

font-size accepts several kinds of values:

ValueRelative toExample
pxA fixed pixel size16px sets the font size to 16 pixels.
emThe parent element's font size2em makes the text twice its parent's font size.
remThe root element's font size1rem uses the root element's font size.
%The parent element's font size200% makes the text twice its parent's font size.

Keywords such as small, medium, and large are also available.

font-weight controls the thickness of text:

ValueMeaning
normal or 400Normal weight.
bold or 700Bold weight.
Numeric valuesRequest a particular weight, such as 100, 400, or 700.
bolderA heavier weight relative to the inherited weight.
lighterA lighter weight relative to the inherited weight.

A font may not support every requested weight, so inspect the result.

font-style controls whether text is upright or slanted:

ValueMeaning
normalDisplays text in its normal style.
italicSelects an italic style, which may be designed as a distinct face.
obliqueSelects an oblique style, which is typically slanted.

Use italic and oblique styles with readability in mind.

Variable fonts

A variable font can offer multiple variations from one file. Its axes describe adjustable features such as weight or width. For a variable font that supports a weight axis, the following requests different weights for body text and a heading:

body {
  font-variation-settings: "wght" 400;
}

h1 {
  font-variation-settings: "wght" 700;
}

The values select positions on the font's weight axis. To see the change, apply a variable font that actually supplies this axis and supports both weights. A static face ignores the axis setting. For an ordinary weight change, prefer font-weight. This provides flexibility without switching between separate files for those variations; the actual options depend on the font.

The same letter is thinner at weight 400 and thicker at 700 along the variable font’s weight axis.

Arrange and decorate text

Spacing affects how easily words and lines can be read. letter-spacing adjusts spacing across letters, while word-spacing changes the space between words:

p {
  letter-spacing: 0.05em;
  word-spacing: 0.1em;
  text-indent: 20px;
}

The first two declarations adjust spacing throughout the paragraph. The last indents its first line by 20 pixels. Letter spacing is distinct from kerning, which adjusts spacing between specific pairs of letters, such as the “T” and “y” in “Typography.” Too little word spacing can make words hard to distinguish; too much can make them feel disconnected. Preview the result on different screens and consider it alongside line spacing.

One arrow measures space between two letters. The other measures the gap between two words.

Use text-align to align inline content within a block. For example, text-align: center centers the text inside its container; it does not center the container itself. Other values include left, right, and justify.

text-decoration adds lines such as underline, overline, or line-through; none removes a decoration set on that element. A child inside an underlined link cannot cancel the link’s underline this way. You can combine line values, as in text-decoration: underline overline. text-transform changes the displayed case with values such as uppercase, lowercase, and capitalize. The underlying text remains unchanged.

Adapt text to screen size

A media query applies styles when a condition is met. The following example starts with one body text size and changes it as the viewport gets wider:

body {
  font-size: 16px;
  line-height: 1.4;
}

@media (min-width: 768px) {
  body {
    font-size: 18px;
    line-height: 1.5;
  }
}

@media (min-width: 1024px) {
  body {
    font-size: 20px;
    line-height: 1.6;
  }
}

At 768 pixels wide or more, the first query increases the font size and line height. At 1024 pixels or more, the later query sets them again. These widths are breakpoints: points where the styling changes. Adjusting line height along with size helps maintain spacing between lines.

For a size that changes smoothly between limits, use clamp():

h1 {
  font-size: clamp(1.5rem, 2vw + 1rem, 3rem);
}

The three arguments set a minimum, a preferred value, and a maximum. The preferred value combines a viewport-width-based amount with 1rem; the result stays between 1.5rem and 3rem. Unlike the breakpoint example, the heading can scale between those bounds without a separate rule for each screen width.

Give text meaning and enough contrast

Semantic HTML describes the role of content. Headings from <h1> through <h6> establish a heading hierarchy, and <p> identifies a paragraph. Use <strong> for importance and <em> for emphasis. These meanings help assistive technologies interpret and navigate the content; CSS can then control its appearance.

Text must also be distinguishable from its background. A contrast ratio expresses the difference in luminance between two colors. The Web Content Accessibility Guidelines (WCAG) give minimum ratios for text:

The word in dark text stands out on white, while the pale gray version is difficult to distinguish from its white background.

LevelNormal textLarge text
AA4.5:13:1
AAA7:14.5:1

Large text is at least 18pt, or at least 14pt when bold. For example, you can check dark gray #333333 text on a white #FFFFFF background against the AA minimum for normal text. Use a tool such as the WebAIM Contrast Checker to calculate the ratio for your text and background colors.

Try it yourself

Try it yourself

Build a text hierarchy

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

Key Takeaways

  • Typography: Font choice, hierarchy, alignment, and spacing shape both readability and visual voice.
  • Font stacks: List preferred families in order and end with a generic family so text has a fallback.
  • Font properties: Size, weight, and style distinguish text roles; available variations depend on the font.
  • Text layout: Adjust spacing, alignment, decoration, and case to support the content's presentation.
  • Responsive text: Media queries change styles at breakpoints, while clamp() scales a value within limits.
  • Meaning and contrast: Semantic HTML communicates text roles, and sufficient contrast helps people perceive text.

How is this lesson?

Previous lesson Choosing units and tracing valuesNext lessonColor and Backgrounds
Edit on GitHubReport on GitHub
On this page
%

On this page

Typefaces, fonts, and familiesChoose a font family and fallbacksLoad a custom fontSet size, weight, and styleVariable fontsArrange and decorate textAdapt text to screen sizeGive text meaning and enough contrastTry it yourselfKey Takeaways