HTML | DOM Style whiteSpace Property

The Style whiteSpace property in HTML DOM is used to set the whitespace for text content. It returns the whitespace property that is given to the text.

Syntax:

  • It returns the whiteSpace property.
    object.style.whiteSpace
  • It is used to set the whiteSpace property.
    object.style.whiteSpace = "normal|nowrap|pre|initial|inherit"

Property Values:



  • normal: It is used to collapse the whitespace into single whitespace and wrap the text. It is a default value.
  • nowrap: It is used to collapse the whitespace into single whitespace and there is no text wrapping.
  • pre: It is used to define the pre-formatted text.
  • pre-line: It is used to collapse whitespaces to single whitespace and wrap text on line brakes.
  • pre-wrap: It is used to preserve whitespaces and wrap text on line brakes.
  • intial: It sets the whiteSpace property to its default value.
  • inherit: This property is inherited from its parent element.

Return Value: It returns a string representing the whiteSpace property for an element.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>
            DOM Style whiteSpace Property 
        </title>
    </head
      
    <body
        <center
            <h1 style = "color:green;"
                GeeksForGeeks 
            </h1
              
            <h2>
                DOM Style whiteSpace Property 
            </h2
                  
            <p id = "gfg">
                A Computer science portal for geeks
                A Computer science portal for geeks
            </p
              
            <button type = "button" onclick = "geeks()"
                Change
            </button
              
            <script
                function geeks() { 
                    document.getElementById("gfg").style.whiteSpace
                                = "pre-line"; 
                
            </script
        </center>
    </body
</html>                                    

chevron_right


Output:
Before Click on the button:

After Click on the button:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>
            DOM Style whiteSpace Property 
        </title>
    </head
      
    <body
        <center
            <h1 style = "color:green;"
                GeeksForGeeks 
            </h1
              
            <h2>
                DOM Style whiteSpace Property 
            </h2
                  
            <p id = "gfg">A Computer science portal for geeks
                A Computer science portal for geeks
            </p
              
            <button type = "button" onclick = "geeks()"
                Change
            </button
              
            <script
                function geeks() { 
                    document.getElementById("gfg").style.whiteSpace
                                = "pre-wrap"; 
                
            </script
        </center>
    </body
</html>                                 

chevron_right


Output:
Before Click on the button:

After Click on the button:

Supported Browsers: The browser supported by DOM Style whiteSpace property are listed below:

  • Apple Safari
  • Google Chrome
  • Firefox
  • Internet Explorer
  • Opera


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.