CSS white-space Property Explained

What the CSS white-space property does, with an example.

Published November 5, 2026

white-space controls how whitespace and line breaks inside an element are handled: normal, nowrap, pre, pre-wrap or pre-line.

How it works

normal collapses spaces and wraps lines; nowrap keeps everything on one line; pre preserves spaces and newlines like <pre>; pre-wrap preserves them but still wraps.

Combine with text-overflow: ellipsis for truncation.

Values and effects

white-space: normal; → collapse and wrap
white-space: nowrap; → single line
white-space: pre; → preserve exactly
white-space: pre-wrap; → preserve and wrap
white-space: pre-line; → keep newlines only

Practical examples

Truncate one line

.title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

Show line breaks from a textarea

.comment { white-space: pre-wrap; }

Code blocks

pre { white-space: pre; overflow-x: auto; }

Common mistakes

  • Using nowrap on mobile and causing overflow.
  • Forgetting overflow for ellipsis to work.
  • Using <br> where pre-line would do.

Key takeaways

  • white-space in one line: white-space controls how whitespace and line breaks inside an element are handled: normal, nowrap, pre, pre-wrap or pre-line.
  • Inspect it live in DevTools → Styles.
  • Test across browsers and screen sizes.

FAQ

pre vs pre-wrap?

pre never wraps; pre-wrap keeps whitespace but wraps at the container edge.

Is white-space supported in all browsers?

Yes in all modern browsers; check caniuse.com for edge cases.

More CSS articles