CSS white-space Property

You are Here:

CSS white-space Property

CSS white-space property sets how white space inside an element is handled.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> div{ border: 1px solid black; padding:10px; width: 300px; } #point{ white-space: normal; } #point1{ white-space: nowrap; } #point2{ white-space: pre; } #point3{ white-space: pre-line; } #point3{ white-space: pre-wrap; } </style> </head> <body> <h1>CSS white-space Property</h1> <h2>white-space: normal;</h2> <div id="point"> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </div> <h2>white-space: nowrap;</h2> <div id="point1"> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </div> <h2>white-space: pre;</h2> <div id="point2"> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </div> <h2>white-space: pre-line;</h2> <div id="point3"> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </div> <h2>white-space: pre-wrap;</h2> <div id="point4"> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </div> </body> </html>

Syntax

Using CSS

element{ white-space: nowrap; }

Using Javascript

object.style.whiteSpace="nowrap";

Animatable

No, white-space property is not animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS white-space property is normal.

Property Value

The following table provides a list of values for CSS white-space property.

ValueExplanation
normalSequences of white space are collapsed. Newline characters in the source are handled the same as other white space. Lines are broken as necessary to fill line boxes.
nowrapSequences of whitespace will collapse into a single whitespace. Text will never wrap to the next line.
preSequences of white space are preserved. Lines are only broken at newline characters in the source and at <br> elements.
pre-lineSequences of whitespace will collapse into a single whitespace. Text will wrap when necessary, and on line breaks.
pre-wrapWhitespace is preserved by the browser. Text will wrap when necessary, and on line breaks.

Using JavaScript

In the following example, we will demonstrate how to change the CSS white-space property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> div{ padding: 10px; border: 1px solid #000; font-size: 17px; width: 200px; margin-bottom: 10px; } </style> </head> <body> <h1>CSS white-space Property</h1> <div> This is some text. This is some text. This is some text. </div> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("div")[0]; function myFunction(){ x.style.whiteSpace = "nowrap"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author