Open In App

CSS StyleDeclaration cssText Property

Last Updated : 09 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 specifies the style to be added to the element.

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

Example 1: To get the cssText property

html




<!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() {
            let x =
                document.getElementById(
                    "p1").style.cssText;
 
            document.getElementById(
                "gfg").innerHTML = x;
        }
    </script>
</body>
</html>


Output:

 

Example 2: To set the cssText property

html




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

 

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

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 5
  • Firefox 1
  • Opera 12.1
  • Safari 1


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads