Style whiteSpaceProperty

Style 对象参考手册Style Object

Definition and Usage

The whiteSpace property sets or returns how tabs, line breaks, and spaces in text are handled.

Syntax

Setting the whiteSpace property:

Object.style.whiteSpace="normal|nowrap|pre|inherit"

Returning the whiteSpace property:

Object.style.whiteSpace

Value Description
normal Default. Automatically ignores spaces and line breaks.
nowrap Ignores spaces, but does not allow line breaks.
pre Line breaks and other spaces are preserved.
inherit The value of the whiteSpace property is inherited from the parent element.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the whiteSpace property.

Note:IE7 and earlier versions do not support the "inherit" value. IE8 only supports "inherit" if a !DOCTYPE is specified. IE9 supports "inherit".


Example

Example

Preserve line breaks and whitespace in text:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial(example.com)</title>
<script>
function displayResult(){
    document.getElementById("div1").style.whiteSpace="pre";
}
</script>
</head>
<body>

<div id="div1">
This is some text, this is some text. This is
some text, this is some text, this is some text.
</div>
<br>
<button type="button" onclick="displayResult()">Preserve line breaks and whitespace</button>

</body>
</html>

Try it yourself »


Style 对象参考手册Style Object Other Extensions