# white-space-collapse

> The `white-space-collapse` CSS property controls how white space inside an element is collapsed.

## How do I keep line breaks but collapse spaces in CSS?

The property that defines whether sequences of spaces and line breaks are collapsed or preserved. Takes values like 'collapse', 'preserve' and 'preserve-breaks', and together with `text-wrap-mode` forms the `white-space` shorthand.

- Topic: Text Handling
- Type: property
- Browser support: Chrome 114+, Chrome Android 114+, Edge 114+, Firefox 124+, Firefox Android 124+, Safari 17.4+, Safari iOS 17.4+

## Example

Wrong:

```css
.message {
  white-space: pre;
}
```

Right:

```css
.message {
  white-space-collapse: preserve-breaks;
}
```

'preserve-breaks' keeps the user's line breaks and collapses extra spaces while long lines still wrap.

## Common mistakes

- Using `white-space: pre`, which keeps spaces but stops long lines from wrapping.

## Key points

- `white-space-collapse` is used when only spacing must change, while the `white-space` shorthand also sets wrapping.
- 'preserve-breaks' keeps line breaks from user text without keeping extra spaces.

## Interview questions

### What is the difference between white-space-collapse and white-space?

`white-space-collapse` only controls spaces and line breaks. `white-space` is the shorthand that also sets `text-wrap-mode`.

### Is preserve-breaks the same as white-space: pre-line?

Yes in effect. 'pre-line' is the shorthand form that combines 'preserve-breaks' with normal wrapping.

### What does break-spaces do?

'break-spaces' preserves spaces like 'preserve', and also lets lines wrap after any space, so spaces take room at line ends.

## Related cards

- [white-space](https://wonderium.dev/css/white-space.md)
- [text-wrap](https://wonderium.dev/css/text-wrap.md)
- [`<pre>`](https://wonderium.dev/html/pre.md)
- [word-break](https://wonderium.dev/css/word-break.md)
- [tab-size](https://wonderium.dev/css/tab-size.md)

---

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