white-space
The white-space property controls how text is handled on the element it is applied to. Let's say you have HTML exactly like this:
<div>
A bunch of words you see.
</div>
You've styled the div to have a set width of 100px. At a reasonable font size, that's too much text to fit in 100px. Without doing anything, the default white-space value is normal, and the text will wrap. See the example below or follow along at home with the demo.
div {
/* This is the default, you don't need to
explicitly declare it unless overriding
another declaration */
white-space: normal;
}

If you want to prevent the text from wrapping, you can apply white-space: nowrap;

Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on it's own line (in the code). When the text renders in the browser, those line breaks appear as if they are stripped out. Also stripped out are the extra spaces one the line before the first letter. If we want to force the browser to display those line breaks and extra white space characters we can use white-space: pre;

It's called pre because the behavior is that as if you had wrapped the text in <pre></pre> tags (which by default handle white space and line breaks that way). White space is honored exactly as it is in the HTML and the text does not wrap until a line break is present in the code. This is particularly useful when literally displaying code, which benefits aesthetically from some formatting (and some time is absolutely crucial, as in white space dependent languages!)
Perhaps you like how pre honors white space and breaks, but you need the text to wrap instead of potentially break out of it's parent container. That's what white-space: pre-wrap; is for:

Finally, white-space: pre-line; will break lines where they break in code, but extra white space is still stripped.

Interestingly, the final line break is not honored. As per the CSS 2.1 spec: "Lines are broken at preserved newline characters, and as necessary to fill line boxes." so perhaps that makes sense.
Here is a table to understand the behaviors of all the different values:
| New lines | Spaces and tabs | Text wrapping | |
|---|---|---|---|
| normal | Collapse | Collapse | Wrap |
| pre | Preserve | Preserve | No wrap |
| nowrap | Collapse | Collapse | No wrap |
| pre-wrap | Preserve | Preserve | Wrap |
| pre-line | Preserve | Collapse | Wrap |
In CSS3, the white-space property is literally going to follow that chart and map the properties to text-space-collapse and text-wrap accordingly.
More Information
Browser Support
Little more complex than the regular support table, since each value has different levels of support:
| Browser | Version | Support of |
|---|---|---|
| Internet Explorer | 5.5 | normal | nowrap |
| 6.0 | normal | pre | nowrap |
|
| 8+ | normal | pre | nowrap | pre-wrap | pre-line |
|
| Firefox (Gecko) | 1.0 (1.0) | normal | pre | nowrap | -moz-pre-wrap |
| 3.0 (1.9) | normal | pre | nowrap | pre-wrap | -moz-pre-wrap |
|
| 3.5 (1.9.1) | normal | pre | nowrap | pre-wrap | pre-line |
|
| Opera | 4.0 | normal | pre | nowrap |
| 8.0 | normal | pre | nowrap | pre-wrap |
|
| 9.5 | normal | pre | nowrap | pre-wrap | pre-line |
|
| Safari (WebKit) | 1.0 (85) | normal | pre | nowrap |
| 3.0 (522) | normal | pre | nowrap | pre-wrap | pre-line |
Chris, you seem to be having a clipping issue with the IE image sprite. Try increasing the width, as it’s the widest of them all.
+1
Great stuff, bro!
Correct the typo when you are free :)
What if you have an element of a specific width and it contains one word that is wider than the box? Is there a way – other than adding some js – to wrap the word to fit in the box with a hyphen?
If not, this should definitely be considered in the next round of css… imo
So I found this – http://www.w3.org/TR/css3-text/#hyphens – Would you mind explaining it? And maybe some browser compatibility tests if you have time?
Thanks!
Is there any way of setting text to wrap before a word, rather than hyphenated in the middle (other than manually inserting a break <br>)?
@ Kevin – Chris has another resource which touches on this (but doesn’t address my question, i think): prevent-long-urls-from-breaking-out-of-container
Thanks Tim! That’s useful.
@Tim Osborn, some useful answers for text wrap control on SO, especially two of mine (;-), http://stackoverflow.com/a/6298738/736006 and http://stackoverflow.com/a/6508168/736006.
@Chris (and all!), does anyone have a better answer for how to discard whitespace completely than this: http://stackoverflow.com/a/2629446/736006
(Thanks, great site!)
(Sorry, forgot to subscribe, can’t w/o a comment apparently.)
Hi, I’m trying to do something which is pretty basic; I want the xslt code in Visual Studio to look the same as the on the resulting pdf so I don’t waste time doing ‘trial and error’ having to guess when spacing out the fields in VS to get them to align eg:
Account Number: 6319469991655135
Balance: £51.28
Payment of Loans: £0.00
eg. this is what I would like to be able to do – so a space in xslt is a space on the pdf
Account Number:
Balance: £
Payment of Loans: £
Can you help?