Skip to content
Related Articles

Related Articles

Improve Article

CSS StyleDeclaration cssText Property

  • Last Updated : 09 Aug, 2019

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:






<!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>

Output:

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

Example-2: To set the cssText property:




<!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>

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
Recommended Articles
Page :