Course contentsShow
← Web Development
Lesson 175 of 2,9823. CSS FundamentalsFree lesson

White Space and Text Wrapping

Control how white space and line breaks are handled using white-space and word-break properties.

White Space and Text Wrapping

You'll learn how to control whether text collapses spaces, wraps to new lines, or breaks inside words using CSS properties.

What This Means

By default, HTML collapses multiple spaces and line breaks into a single space, and text automatically wraps when it reaches the edge of its container. But sometimes you need different behavior—like preserving exact spacing in code samples, or preventing long URLs from overflowing their boxes.

Two CSS properties give you this control:

The white-space Property

This controls how spaces, tabs, and line breaks are handled:

  • normal (default): Collapses whitespace, wraps text automatically
  • nowrap: Collapses whitespace but prevents wrapping—text stays on one line
  • pre: Preserves all whitespace exactly as typed (like the <pre> element)
  • pre-wrap: Preserves whitespace but still wraps text at container edges
  • pre-line: Preserves line breaks but collapses spaces

Think of white-space as instructions for how literally to treat your source code's formatting.

The word-break Property

This controls what happens when a word is too long to fit:

  • normal: Only breaks at normal break points (spaces, hyphens)
  • break-all: Can break anywhere, even mid-word
  • keep-all: Prevents breaks in certain languages (like Chinese/Japanese/Korean)

Use word-break: break-all for URLs or long strings without spaces that might otherwise overflow their containers.

Common Use Cases

Use white-space: pre for displaying code snippets. Use white-space: nowrap for labels that must stay on one line. Use word-break: break-all for user-generated content like URLs that could break your layout.

Key Takeaway: white-space controls spacing and wrapping behavior, while word-break determines whether long unbroken strings can split mid-word.