Open In App
Related Articles

CSS StyleDeclaration cssText Property

Improve Article
Improve
Save Article
Save
Like Article
Like

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 09 Jun, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials