white-space Attribute | whiteSpace Property

Internet Development Index

Sets or retrieves a value that indicates whether lines are automatically broken inside the object.

What's New for Microsoft® Internet Explorer 6

This property now applies to the currentStyle element. The pre value of this property is now supported.

Syntax

HTML { white-space : sWrap }
Scriptingobject.style.whiteSpace(v) [ = sWrap ]

Possible Values

sWrapString that specifies or receives one of the following values.
normalDefault. Lines of text break automatically. Content wraps to the next line if it exceeds the width of the object.
nowrapLine breaks are suppressed. Content does not wrap to the next line.
pre  Line breaks and other whitespace are preserved. This possible value is supported in Internet Explorer 6 and later when the !DOCTYPE declaration specifies standards-compliant mode. When the !DOCTYPE declaration does not specify standards-compliant mode, you can retrieve this value, but it does not affect rendering—it functions like the normal value.

The property is read/write for all objects except the following, for which it is read-only: currentStyle. The property has a default value of normal. The Cascading Style Sheets (CSS) attribute is inherited.

Expressions can be used in place of the preceding value(s), as of Microsoft® Internet Explorer 5. For more information, see About Dynamic Properties.

Remarks

Whitespace, such as line breaks, spaces, and tabs, is collapsed by default in HTML documents. You can use the nonbreaking space entity?SPAN CLASS="clsLiteral">( ) to add extra spaces to an object when the whiteSpace property is set to normal or nowrap. You can add extra line breaks using the br element.

This property affects content you access through the Document Object Model (DOM) the same way it affects the way Internet Explorer displays the content.

Examples

The following example shows how automatic line breaks are suppressed when the user places the mouse pointer over the paragraph. This is caused by toggling the value of the white-space attribute in the onmouseover and onmouseout events of the p element. When the white-space attribute is set to nowrap in the onmouseover event, line breaks are suppressed, and horizontal scrolling is required to view content wider than the element. When the attribute is set to normal in the onmouseout event, lines break automatically, depending on the width of the element.

<HTML>
<HEAD>
<STYLE>
.clsOneLiner  {white-space: nowrap}
.clsAutoBreak {white-space: normal}
</STYLE>
</HEAD>
<BODY>
<P ONMOUSEOVER="this.className='clsOneLiner';"
ONMOUSEOUT="this.className='clsAutoBreak';">
Long lines of text remain unbroken when the value of the whitespace
attribute is set to nowrap. Place your mouse over the text to
suppress automatic line breaks.</P>
</BODY>
</HTML>
This feature requires Microsoft® Internet Explorer 5.5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

The following example shows how to set and retrieve the value of the whiteSpace property. When the user sets the value of the whiteSpace property of a div element, the value of the property is retrieved in a span element.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 //EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML>
<HEAD>
<SCRIPT>
function fnSwitch(){
oDiv.style.whiteSpace = event.srcElement.innerText;
document.all.oSpan.innerText = oDiv.currentStyle.whiteSpace;
}
</SCRIPT>
</HEAD>
<BODY>
<BODY>
<H1>direction Property Sample</H1>
<H2>direction:
<SPAN id="oSpan" style="color:red"></SPAN></H2>
<P>[ <A href="#" onclick=fnSwitch()>normal</A> | <A href="#"
onclick=fnSwitch()>nowrap</A> | <A href="#" onclick=fnSwitch()>pre</A> ]</P>
<DIV id="oDiv" style="background:#e4e4e4; padding:10px;">
In   the   source,   this   sentence   has   three   spaces   between   each   word.
This sentence
takes up three
lines in the source.
</DIV>
</BODY>
</HTML>
This feature requires Microsoft® Internet Explorer 6 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This property is defined in Cascading Style Sheets (CSS), Level 1 (CSS1) Non-Microsoft link.

Applies To

ACRONYM, ADDRESS, BASEFONT, BLOCKQUOTE, BDO, BIG, BODY, CENTER, CITE, CODE, currentStyle, CUSTOM, DD, DIV, DIR, DL, DT, EM, FIELDSET, FORM, FONT, hn, HR, ISINDEX, I, INS, KBD, LABEL, LEGEND, LI, LISTING, MENU, OL, P, Q, PLAINTEXT, PRE, S, SMALL, SPAN, STRIKE, STRONG, style, SUB, SUP, TT, U, UL, XMP

See Also

CSS Enhancements in Internet Explorer 6, pre, wordWrap