XML technologies / CSS / Advanced websites with CSS / CSS property reference / white-space

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Consecutive spaces and tabs in HTML are collapsed to single spaces. Line breaks occur only through automatic wrapping or tags <br/> in the HTML code.

To create consecutive spaces or manually controlled line breaks, HTML requires tricks using elements such as "&nbsp;", the attribute nowrap in table tags, and the HTML tag <pre>, none of which were intended for these purposes.

CSS offers a more elegant, standards-compliant solution: the property white-space allows consecutive spaces and control of line breaks in selected elements.

Inherited: yes

Values

This property accepts the following constants:

  • normal: Content follows normal HTML behavior: spaces collapse and lines wrap automatically.

  • nowrap: Spaces collapse, automatic wrapping is disabled, and line breaks are triggered only by tags <br/> or replaced by generated content (see content).

  • pre: Spaces are not collapsed and automatic wrapping is disabled, behaving similarly to the HTML element pre.

Default value: normal

Compatibility

CSS version: 1

Works in Mozilla, including Netscape 6 and later, Opera and Internet Explorer 5 for Macintosh.

Internet Explorer for Windows supports this property only from version 5.5, and the value pre only in version 6’s Standards-Compliant Mode.

Netscape 4 supports this property except for nowrap.

Example

The following style rule allows consecutive spaces and disables automatic wrapping in elements div of the class screen:

div.screen {
 white-space: pre;
}

  

 

 

 

back to top