Skip to: content, navigation

by Tommy Olsson and Paul O’Brien

white-space (CSS property)

Browser support full matrix
IE5.5+ FF1+ Saf1.3+ Op9.5+
Partial Partial Full Full
Spec
Inherited Initial Version
Yes normal CSS1, 2

Example

This style rule makes elements that belong to the "poetry" class retain and render all whitespace in the document markup:

.poetry {
  white-space: pre;
}

Try it yourself!View all demos

Description

This property controls the handling of whitespace inside an element. Whitespace is a collective name for one or more occurrences of the characters space, tab, line feed, carriage return, and form feed. Typically, within an HTML element, user agents will collapse a sequence of whitespace characters into a single space character.

Note that this property only handles whitespace characters; a common beginner’s mistake is to try to use it to prevent floated elements from dropping down if there isn’t enough room on a line.

Value

normal
A value of normal dictates that sequences of whitespace will collapse into a single space character. Line breaks will occur wherever necessary to fill line boxes.
nowrap
Specifying nowrap ensures that sequences of whitespace will collapse into a single space character, but line breaks will be suppressed.
pre
Specifying pre ensures that sequences of whitespace won’t collapse. Lines are only broken at new lines in the markup (or at occurrences of "\a" in generated content).
pre-line
This value will cause sequences of whitespace to collapse into a single space character. Line breaks will occur wherever necessary to fill line boxes, and at new lines in the markup (or at occurrences of "\a" in generated content). In other words, it’s like normal except that it’ll honor explicit line breaks.
pre-wrap
Specify pre-wrap to ensure that sequences of whitespace won’t collapse. Line breaks will occur wherever necessary to fill line boxes, and at new lines in the markup (or at occurrences of "\a" in generated content). In other words, it’s like pre except that it’ll wrap the text at the end of line boxes.

Compatibility

IE5.5Partial
6.0Partial
7.0Partial
Firefox1.0Partial
1.5Partial
2.0Partial
Safari1.3Full
2.0Full
3.0Full
Opera9.2Partial
9.5Full

Internet Explorer for Windows versions up to and including 7 don’t support the values pre-line or pre-wrap. The values normal and pre behave like pre-wrap on textarea elements. The value nowrap behaves like pre-line on textarea elements.

Internet Explorer for Windows versions up to and including 7 don’t support the value inherit.

Firefox versions up to and including 2 don’t support the values pre-line and pre-wrap (although -moz-pre-wrap is similar to the latter). The values normal, nowrap, and pre behave like pre-wrap on textarea elements.

Opera 9.2 and prior versions don’t support the value pre-line. The values normal and pre behave like pre-wrap on textarea elements. The value nowrap behaves like pre-line on textarea elements.

User-contributed notes

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters