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 −
Value | Explanation |
---|---|
initial | It set this property value to its default value. |
inherit | It inherits this property value from its parent element. |
normal | In it the sequence of whitespace will collapse into a single one and text will wrap when necessary. |
nowrap | In it the sequence of whitespace will collapse into a single one and text will not wrap to the next line. |
pre | In it the whitespace is preserved by the browser and text will only wrap when a line break encounter. |
pre-line | In it the sequence of whitespace will collapse into a single one and text will only wrap to the next line when necessary. |
pre-wrap | In 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 −
<!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 −