CSS

🎯 What is white-space?

The white-space property defines how spaces, line breaks, and tabs inside an HTML element are handled by the browser.

In other words, it controls whether whitespace is preserved, collapsed, or wrapped to the next line.

🔹 Basic Usage

CSS

p {
  white-space: pre;
}
                

➡️ In this example, text is displayed exactly as written, including spaces and line breaks.

🔸 Values and Descriptions

1. normal

Default value. Spaces are collapsed and text wraps automatically.

2. nowrap

Spaces are collapsed, but text does not wrap and stays on a single line.

3. pre

Preserves all spaces, tabs, and line breaks exactly as written.

Example

p {
  white-space: pre;
}
                

➡️ Output keeps original formatting.

4. pre-line

Line breaks are preserved, but extra spaces are collapsed.

5. pre-wrap

Preserves both spaces and line breaks, but allows automatic wrapping.

💡 Quick Summary

Value Spaces Line Breaks Wrap
normal Collapsed Yes Yes
nowrap Collapsed No No
pre Preserved Yes No
pre-line Collapsed Yes Yes
pre-wrap Preserved Yes Yes
  • 🧠 Summary

  • The white-space property controls how spaces and line breaks behave inside an element.
  • It is especially useful for:
  • Code blocks
  • Poems or formatted text
  • UI layouts requiring strict formatting