/

HTML / CSS / JavaScript Tutorial

CSS Property: white-space

[this page | pdf | back links]

The CSS (CSS1) white-space property indicates how white-space inside an element should be handled.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

normal

(default value). Sequences of whitespace collapsed into a single whitespace and text will wrap when necessary

nowrap

Sequences of whitespace collapsed into a single whitespace but text will not wrap until a <br> tag occurs

pre

Whitespace is preserved by browser and text will only wrap on line breaks (i.e. akin to <pre> tag in HTML)

pre-line

Sequences of whitespace collapsed into a single whitespace and text will wrap when necessary and on line breaks

pre-wrap

Whitespace is preserved by browser and text will wrap when necessary and on line breaks

 

Default Value:

normal

JavaScript syntax:

e.g. object.style.whiteSpace="hidden"

Inherited:

Yes

Animatable:

No

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
div {border: 2px solid red; width: 100px; height: 30px; background-color: yellow}
.x1 {white-space: pre;}
</style>
</head>
<body>
1. Element with default property<br>
<div><span>a     b</span></div><br>
2. Property set using HTML<br>
<div><span class="x1">a     b</span></div><br>
3. Property set using JavaScript<br>
<div><span id="x2">a     b</span></div>

<script>
document.getElementById("x2").style.whiteSpace = "pre";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyWhiteSpace() {
  var x = document.createElement("DIV"); x.style.whiteSpace = "pre"; return (window.getComputedStyle(x, null).whiteSpace == "pre");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile