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.
Fundamentals
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.
Foundation
📝 Syntax
The syntax for the white-space property is simple and can be applied to any element.
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
Defaults
🎯 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.
Cheat Sheet
⚡ Quick Reference
Question
Answer
Default value
normal
Keep text on one line
white-space: nowrap;
Preserve code formatting
white-space: pre-wrap;
Keep line breaks only
white-space: pre-line;
Preserve trailing spaces
white-space: break-spaces;
Inherited
Yes
Reference
💎 Property Values
The white-space property accepts keyword values that control space collapsing and line breaking.
Value
Example
Description
normal
white-space: normal;
Collapses whitespace and breaks lines as needed (default).
nowrap
white-space: nowrap;
Collapses whitespace but prevents line breaks.
pre
white-space: pre;
Preserves whitespace and line breaks; does not wrap long lines.
pre-wrap
white-space: pre-wrap;
Preserves whitespace and line breaks, but wraps when necessary.
pre-line
white-space: pre-line;
Collapses whitespace but preserves line breaks.
break-spaces
white-space: break-spaces;
Like pre-wrap, but preserves spaces at line ends and breaks at any character when needed.
normalnowrapprepre-wrappre-linebreak-spaces
Context
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.
Preview
👀 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
Hands-On
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><divclass="normal">Extra spaces collapse.</div><divclass="nowrap">Text stays on one line.</div><divclass="pre-wrap">Preserves formatting.</div>
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll 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 property99% supported
Bottom line:white-space is safe to use for text formatting across the modern web.
Wrap Up
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.