Giving Voice to Your Texts: CSS Text & Fonts

Master the CSS properties that control web typography. Learn to manage fonts, sizing, spacing, and alignment to create beautiful and readable content.

Lesson ProgressStep 1 of 9

Styling This Text

CSS gives us fine-grained control over every aspect of typography, from the font itself to the space between lines.

0 EXP

Welcome! Let's learn to style text. We'll start with `font-family`, which changes the typeface.

/* CSS controls the style */
.text-demo {
  font-family: serif;
}

Choosing Fonts (`font-family`)

The `font-family` property lets you specify a typeface. You should always provide a **font stack**—a list of fonts for the browser to try.

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

Here, the browser first tries `Arial`. If the user doesn't have it, it tries `Helvetica`. If that also fails, it will use whatever the user's default `sans-serif` font is. This fallback system is crucial for consistent design.

System Check

Why is `sans-serif` included at the end of a font stack like `font-family: Arial, sans-serif;`?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Font Family Master

Master the `font-family` stack and web-safe fonts.

🏗️
Readability Expert

Correctly use `line-height` and `font-size` for readability.

✍️
Text Stylist

Demonstrate mastery of `text-decoration` and `text-align`.

Mission: Style the Document

Write CSS to style the document. Target the `h1` and give it a `font-size` of `3rem` and `text-align: center;`. Then, target the `p` tags and give them a `line-height` of `1.6`.

A.D.A. Feedback:

> Awaiting input...

Challenge: Order the Properties

While CSS order doesn't *technically* matter, grouping properties logically is good practice. Drag these from most fundamental (font choice) to most specific (decoration).

font-size: 16px;
text-decoration: underline;
font-family: Arial, sans-serif;
line-height: 1.5;

Challenge: Complete the Syntax

Fill in the missing properties and values to complete the CSS rules.

p { : "Times New Roman", ; }.important { : bold; }

Consult A.D.A.

Community Holo-Net

More Than Just Words: The Art of Web Typography

It's often said that web design is 90% typography. While that might be an exaggeration, it's not far from the truth. The way you present text on a webpage dictates not only its **readability** but also its **tone** and **emotional impact**. CSS provides a powerful suite of tools to transform plain text into a compelling user experience.

The Foundation: `font-family`

The `font-family` property is your starting point. It defines the typeface for your text. Fonts are broadly categorized into:

  • Serif Fonts: Have small "feet" or strokes on the letters (e.g., Times New Roman, Georgia). They are often considered more traditional and are common in print.
  • Sans-serif Fonts: "Sans" means "without." These fonts lack the small feet (e.g., Arial, Helvetica, Verdana). They are generally seen as more modern and clean, and are the most common choice for web body text.

You should never rely on a single font. Instead, you must provide a **font stack**: a comma-separated list of fonts. The browser will try the first font, and if the user doesn't have it installed, it will move to the next, and so on.

p {
  /* 1. Try a specific, nice sans-serif font */
  /* 2. Try a common, web-safe font */
  /* 3. Provide a generic family as a final fallback */
  font-family: "Roboto", Arial, sans-serif;
}

For custom fonts not installed on a user's device, you can use the CSS `@font-face` rule or, more commonly, services like Google Fonts to import them into your stylesheet.

The Sizing Debate: `px` vs. `em` vs. `rem`

How you size your font with `font-size` is critical for accessibility.

  • `px` (Pixels): An absolute unit. `font-size: 16px;` is always 16 pixels. This is simple but rigid and doesn't respect a user's browser-level font size settings.
  • `em` (Elements): A relative unit. `1em` is equal to the `font-size` of the **parent element**. This is flexible but can lead to "compounding," where nested elements become unpredictably large or small.
  • `rem` (Root Em): A relative unit. `1rem` is equal to the `font-size` of the **root element** (the `<html>` tag).

Today, **`rem` is the preferred unit for font sizing**. By default, `1rem` equals `16px`. If a user with visual impairments sets their browser's default font size to be larger, `1rem` will become larger proportionally. This makes your site accessible and scalable, while `px` values would ignore the user's preference.

Rhythm & Readability: `line-height` and Spacing

Long blocks of text can be daunting. `line-height` (the space between lines), `letter-spacing` (space between letters), and `word-spacing` (space between words) are your tools to improve readability.

The most important of these is `line-height`. For body text, a `line-height` of **`1.5` to `1.7`** is recommended.

Pro Tip: Always set `line-height` as a **unitless value** (e.g., `line-height: 1.6;`). This makes it a multiplier of the element's `font-size`. If you use `px`, a large `font-size` on a heading might have a `line-height` that is too small, causing text to overlap.

❌ Bad Practice

p {
  font-size: 16px;
  line-height: 24px;
}

Rigid. If a child element has `font-size: 30px`, it will still have a `line-height` of `24px`, causing overlap.

✔️ Good Practice

p {
  font-size: 1rem; /* 16px */
  line-height: 1.5; /* 1.5 * 16 = 24px */
}

Flexible. A child with `font-size: 2rem` will automatically get a `line-height` of `3rem` (1.5 * 2rem).

Style & Emphasis

Finally, you can control the specific styling of your text:

  • `font-weight`: Controls boldness. Use `normal`, `bold`, or numeric values like `400` (normal) and `700` (bold).
  • `font-style`: Used to set text to `italic`.
  • `text-align`: Aligns text. Use `left`, `right`, `center`. Avoid `justify`, as it can create ugly "rivers" of white space on the web.
  • `text-decoration`: Adds lines to text, like `underline`, `overline`, or `line-through`. A common use is `text-decoration: none;` to remove the default underline from links.
  • `text-transform`: Changes the case of text to `uppercase`, `lowercase`, or `capitalize`.

Mastering these properties allows you to guide the user's eye, establish a clear hierarchy, and create a design that is both beautiful and, most importantly, effortless to read.

CSS Text & Typography Glossary

`font-family`
Specifies a prioritized list of one or more font family names (a "font stack") and/or generic family names for an element.
`font-size`
Controls the size of the font. Can be set with absolute units (`px`) or, preferably, relative units (`rem`, `em`).
`font-weight`
Sets the thickness or boldness of the font. Common values are `normal` (400) and `bold` (700).
`font-style`
Sets the style of the font, most commonly `normal` or `italic`.
`line-height`
Specifies the height of a line box. It's used to set the distance between lines of text. A unitless value (e.g., `1.6`) is recommended.
`text-align`
Describes how inline content like text is aligned within its parent block element. Values include `left`, `right`, `center`, and `justify`.
`text-decoration`
Adds decorative lines to text. Common values are `none`, `underline`, `overline`, and `line-through`.
`text-transform`
Controls the capitalization of text. Values include `none`, `uppercase`, `lowercase`, and `capitalize`.
`letter-spacing`
Adjusts the spacing between characters of text.
`word-spacing`
Adjusts the spacing between words.
`color`
Sets the foreground color value of an element's text.
`rem` (Unit)
"Root Em." A relative unit equal to the `font-size` of the root (`<html>`) element. Preferred for accessible and scalable typography.
Font Stack
The list of fonts provided in the `font-family` property. It provides fallback options if the user's browser cannot find or render the first-choice font.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust, beautiful, and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS3 specifications (MDN) and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!