HTML DOM Style whiteSpace Property


The HTML DOM style whiteSpace property returns and modify how to handle tabs, line breaks, and whitespace in a text of an element in an HTML document.

Syntax

Following is the syntax −

  • Returning whiteSpace

object.style.whiteSpace
  • Modifying whiteSpace

object.style.whiteSpace = “value”

Values

Here, value can be −

ValueExplanation
initialIt set this property value to its default value.
inheritIt inherits this property value from its parent element.
normalIn it the sequence of whitespace will collapse into a single one and text will wrap when necessary.
nowrapIn it the sequence of whitespace will collapse into a single one and text will not wrap to the next line.
preIn it the whitespace is preserved by the browser and text will only wrap when a line break encounter.
pre-lineIn it the sequence of whitespace will collapse into a single one and text will only wrap to the next line when necessary.
pre-wrapIn it the whitespace is preserved by the browser and text will only wrap to the next line when necessary.

Example

Let us see an example of HTML DOM style whiteSpace property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      color: #000;
      background: lightblue;
      height: 100vh;
   }
   p {
      border: 2px solid #fff;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem 0;
}
</style>
</head>
<body>
<h1>DOM Style whiteSpace Property Example</h1>
<p>
   I'm paragraph element with some dummy text. I'm paragraph element with some dummy text.
   I'm paragraph element with some dummy text. I'm paragraph element with some dummy text.
   I'm paragraph element with some dummy text.
</p>
<button onclick="add()" class="btn">Change whiteSpace</button>
<script>
   function add() {
      document.querySelector('p').style.whiteSpace = "pre-line";
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click on “Change whiteSpace” button to change the behaviour of how to handle whitespaces of paragraph element −

Updated on: 01-Jul-2020

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements