CSS StyleDeclaration cssText Property

The cssText property is used to set or return the value of an inline style declaration of an element.

Syntax:

  • It is used to return the cssText property.
    element.style.cssText
  • It is also used to set the cssText property.
    element.style.cssText = style

Property value:



  • style: It is a string literal that specify the style to be added on the element.

Return Value: It returns the inline style of the element in the form of string.

Example-1: To get the cssText property:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        CSS StyleDeclaration cssText Property
    </title>
    <style>
        body {
            text-align: center;
        }
          
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>
      GeeksforGeeks
  </h1>
  
    <!-- Adding inline style -->
    <p id="p1" 
       style="color:green;
              font-size:20 ">
        cssText Property
  </p>
  
    <p>
      Click the button
      to get the style
  </p>
  
    <button onclick="myFunction()">
        Get style
    </button>
  
    <p id="gfg">
  </p>
    <!-- Script to get 
     cssText property -->
    <script>
        function myFunction() {
            var x = 
                document.getElementById(
                  "p1").style.cssText;
            
            document.getElementById(
              "gfg").innerHTML = x;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example-2: To set the cssText property:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        CSS StyleDeclaration cssText Property
    </title>
    <style>
        body {
            text-align: center;
        }
          
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <!-- Adding inline style -->
    <p id="p1"
       style="color:green; 
              font-size:20 ">
        cssText Property
  </p>
  
    <p>
      Click the button to change the style
  </p>
  
    <button onclick="myFunction()">
        Set style
    </button>
  
    <p id="gfg"></p>
    <!-- Script to set cssText property -->
    <script>
        function myFunction() {
            document.getElementById(
              "p1").style.cssText = 
              "color:blue; font-size:15";
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Supported Browsers: The browsers supported by CSS StyleDeclaration cssText Property are listed below:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


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