CSS white-space Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Typography

What You’ll Learn

The white-space property controls how spaces, tabs, and line breaks are handled in text — from normal wrapping to fully preserved formatting.

01

Spaces

Collapse or keep.

02

Line breaks

Wrap or preserve.

03

normal

Default value.

04

nowrap

Single-line text.

05

pre-wrap

Code blocks.

06

pre-line

Poems & addresses.

Introduction

The white-space property in CSS is used to control how white space inside an element is handled. This includes spaces, tabs, and newline characters.

By using the white-space property, you can manage how text is displayed, whether it’s preserving spaces, wrapping lines, or collapsing whitespace.

Definition and Usage

Apply white-space when default browser text behavior is not what you need. Common scenarios include navigation labels that must stay on one line, code snippets that need indentation, and user-generated content with intentional line breaks.

💡
Beginner Tip

HTML normally collapses multiple spaces into one. If your text looks “squished” or line breaks disappear, check whether you need pre-line, pre-wrap, or pre instead of the default normal.

📝 Syntax

The syntax for the white-space property is simple and can be applied to any element.

syntax.css
element {
  white-space: normal | nowrap | pre | pre-wrap | pre-line | break-spaces;
}

Here, the value can be one of several predefined keywords that dictate how white space is treated.

Basic Example

white-space.css
.code-snippet {
  white-space: pre-wrap;
  font-family: monospace;
}

Syntax Rules

  • One keyword value at a time.
  • Shorthand for two behaviors: space collapsing and line wrapping.
  • nowrap is common for labels; pair with overflow and text-overflow when needed.
  • pre-wrap is usually better than pre for responsive layouts.
  • The property is inherited.

Related Properties

  • word-break — controls breaking inside long words
  • overflow-wrap (& word-wrap) — allows breaking long unbroken strings
  • text-overflow — adds ellipsis when text overflows a nowrap box
  • line-height — spacing between wrapped lines

🎯 Default Value

The default value of the white-space property is normal. This means that sequences of white space will collapse into a single space, and lines will wrap as necessary to fit within the block container.

⚡ Quick Reference

QuestionAnswer
Default valuenormal
Keep text on one linewhite-space: nowrap;
Preserve code formattingwhite-space: pre-wrap;
Keep line breaks onlywhite-space: pre-line;
Preserve trailing spaceswhite-space: break-spaces;
InheritedYes

💎 Property Values

The white-space property accepts keyword values that control space collapsing and line breaking.

ValueExampleDescription
normalwhite-space: normal;Collapses whitespace and breaks lines as needed (default).
nowrapwhite-space: nowrap;Collapses whitespace but prevents line breaks.
prewhite-space: pre;Preserves whitespace and line breaks; does not wrap long lines.
pre-wrapwhite-space: pre-wrap;Preserves whitespace and line breaks, but wraps when necessary.
pre-linewhite-space: pre-line;Collapses whitespace but preserves line breaks.
break-spaceswhite-space: break-spaces;Like pre-wrap, but preserves spaces at line ends and breaks at any character when needed.
normal nowrap pre pre-wrap pre-line break-spaces

When to Use white-space

white-space is helpful whenever default text flow is not enough:

  • Navigation and buttons — Use nowrap so labels stay on one line.
  • Code and logs — Use pre-wrap to keep indentation on small screens.
  • Addresses and poetry — Use pre-line to respect line breaks without extra spaces.
  • User comments — Preserve intentional formatting from pasted text.
  • Tables and data — Prevent awkward wrapping in numeric columns with nowrap.

👀 Live Preview

Same sample text with normal, nowrap, and pre:

Hello world wraps normally
normal
Hello world stays on one line with nowrap
nowrap
Hello world preserves spaces
pre

Examples Gallery

In this example, we’ll demonstrate how different values of the white-space property affect text display.

📜 Core Patterns

See how each white-space keyword changes spacing and wrapping behavior.

Example 1 — Compare all main values

In this example, we’ll demonstrate how different values of the white-space property affect text display.

index.html
<style>
  .normal { white-space: normal; }
  .nowrap { white-space: nowrap; }
  .pre { white-space: pre; }
  .pre-wrap { white-space: pre-wrap; }
  .pre-line { white-space: pre-line; }
</style>

<div class="normal">Extra     spaces collapse.</div>
<div class="nowrap">Text stays on one line.</div>
<div class="pre-wrap">Preserves     formatting.</div>
Try It Yourself

How It Works

Each keyword combines two decisions: whether extra spaces collapse and whether the browser may wrap lines automatically.

Example 2 — nowrap for single-line labels

Prevent button and tag text from wrapping awkwardly in tight toolbars.

nowrap.css
.pill {
  white-space: nowrap;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
}
Try It Yourself

How It Works

nowrap keeps the entire label on one line. Combine with overflow: hidden and text-overflow: ellipsis if space is limited.

📄 Content Patterns

Format code snippets and multi-line text for readability.

Example 3 — pre-wrap for code blocks

Keep indentation and line breaks in code while still wrapping on narrow screens.

pre-wrap.css
.code-block {
  white-space: pre-wrap;
  font-family: monospace;
}
Try It Yourself

How It Works

pre-wrap behaves like the HTML <pre> tag for spacing, but long lines still wrap instead of overflowing horizontally.

Example 4 — pre-line for poetry

Respect line breaks from the source text while collapsing unnecessary extra spaces.

pre-line.css
.poem {
  white-space: pre-line;
  line-height: 1.7;
}
Try It Yourself

How It Works

pre-line is ideal when authors press Enter for new lines but you still want normal space collapsing within each line.

♿ Accessibility

  • Avoid horizontal overflownowrap on long text can cause scrolling issues on mobile. Test with zoom and small screens.
  • Do not hide meaning with wrapping — Ensure critical words are not cut off without a way to read the full text.
  • Pair nowrap with ellipsis carefully — Truncated text should still be available in full via title attribute, expandable UI, or a detail view.
  • Preserve readable line length — Even with pre-wrap, keep comfortable line-height and contrast for code blocks.

white-space + text-overflow

A common UI pattern for truncated labels combines three properties: white-space: nowrap, overflow: hidden, and text-overflow: ellipsis.

ellipsis.css
.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 12rem;
}

🧠 How white-space Works

1

Text contains spaces and breaks

HTML source may include multiple spaces, tabs, or newline characters.

Input
2

white-space sets the rules

Keywords decide whether spaces collapse and lines may wrap.

Rules
3

Browser lays out the line boxes

Text flows into lines according to the chosen white-space behavior.

Layout
=

Readable formatted text

Content displays with the spacing and wrapping you intended.

Browser Compatibility

The white-space property is well-supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. This ensures that your text formatting will be consistent across different platforms.

Modern browsers · Widely supported

Text wrapping and preservation

All major values — including pre-wrap, pre-line, and break-spaces — work reliably in current browsers.

99% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support

Testing tip

Test nowrap labels and pre-wrap code blocks on mobile widths to confirm wrapping and overflow behave as expected.

white-space property 99% supported

Bottom line: white-space is safe to use for text formatting across the modern web.

Conclusion

The white-space property is an essential tool for web developers when it comes to managing text display.

Whether you need to preserve formatting or ensure that text wraps correctly, the white-space property provides the flexibility to handle whitespace in a way that suits your design needs. Experiment with the different values to see how they can enhance the readability and layout of your content.

💡 Best Practices

✅ Do

  • Use pre-wrap for responsive code blocks
  • Use nowrap for short labels and table headers
  • Combine nowrap with ellipsis for truncated titles
  • Use pre-line for user text with line breaks
  • Test formatting on mobile and with browser zoom

❌ Don’t

  • Use pre for long content on small screens without overflow handling
  • Apply nowrap to entire paragraphs of body text
  • Assume HTML will preserve spaces without the right value
  • Truncate text without giving users access to the full content

Key Takeaways

Knowledge Unlocked

Five things to remember about white-space

Use these points when formatting text.

5
Core concepts
🕐 02

normal

Default value.

Default
🔀 03

nowrap

Single line.

Pattern
🔒 04

pre-wrap

Code blocks.

Content
🌐 05

Inherited

From parent.

Cascade

❓ Frequently Asked Questions

white-space controls how spaces, tabs, and line breaks inside an element are handled — whether they collapse, wrap, or are preserved.
The default is normal, which collapses extra spaces and wraps text to fit the container.
Both collapse spaces, but nowrap prevents line breaks so text stays on one line until overflow handling kicks in.
Both preserve spaces and line breaks. pre does not wrap long lines; pre-wrap wraps when the container is too narrow.
Yes, white-space is inherited. Setting it on a parent affects child text unless overridden.

Practice in the Live Editor

Open the HTML editor and experiment with normal, nowrap, pre-wrap, and pre-line.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful