# white-space

> The `white-space` CSS property controls how whitespace inside an element is handled, such as whether lines should wrap or not.

## How do I stop text from wrapping in CSS?

The property that controls how white space inside an element is handled. It can be set to 'normal', 'nowrap', 'pre', 'pre-wrap' or 'pre-line', affecting whether text wraps, is preserved or collapses whitespace.

- Topic: Text Handling
- Type: property
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 1+, Safari iOS 1+

## Example

Wrong:

```css
.comment-body {
  white-space: pre;
}
```

Right:

```css
.comment-body {
  white-space: pre-wrap;
}
```

'pre-wrap' keeps the user's spaces and line breaks but still wraps long lines inside the box.

## Common mistakes

- Using `white-space: pre` for comments and getting a horizontal scrollbar on long lines.
- Setting 'nowrap' on a flex item and breaking the layout on small screens.

## Key points

- User text with line breaks uses 'pre-wrap' or 'pre-line', not 'pre', so long lines still wrap.
- 'nowrap' text inside a narrow box is paired with `overflow` handling or `text-overflow`.
- Code blocks that must not wrap have `overflow-x: auto` so they scroll instead of overflowing the page.

## Interview questions

### What is the difference between pre-wrap and pre-line?

'pre-wrap' keeps all spaces and line breaks. 'pre-line' keeps line breaks but collapses runs of spaces into one.

### Is white-space a shorthand now?

Yes. In current specs it is a shorthand for `white-space-collapse` and `text-wrap-mode`, and the old keywords still work.

### Why does white-space: nowrap not add an ellipsis?

'nowrap' only stops wrapping. An ellipsis also needs `overflow: hidden` and `text-overflow: ellipsis`.

## Related cards

- [white-space-collapse](https://wonderium.dev/css/white-space-collapse.md)
- [text-wrap](https://wonderium.dev/css/text-wrap.md)
- [text-overflow](https://wonderium.dev/css/text-overflow.md)
- [overflow-wrap](https://wonderium.dev/css/overflow-wrap.md)
- [`<pre>`](https://wonderium.dev/html/pre.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/white-space
- Last updated: 2026-11-25
