The Wayback Machine - https://web.archive.org/web/20240118175155/https://www.geeksforgeeks.org/cssstyledeclaration-setproperty-method/
Open In App
Related Articles

CSSStyleDeclaration setProperty() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The setProperty() method is used to modify the existing CSS property or set the new CSS in the declaration block. 

Syntax: It is used to return the length property.

object.setProperty(propertyname, value, priority)

Parameters: It accepts three parameters:

  • propertyname: It is a required parameter that contains a string which represents the name of the property to set.
  • value: It is an optional parameter that contains a string which represents the new value.
  • priority: It is an optional parameter that contains a string which represents if the priority of the property should be set to important or not.

Example: To show the working of the setProperty() method: 

html




<html>
 
<head>
    <style>
        body {
            text-align: center;
        }
         
        h1 {
            color: green;
        }
         
        #p1 {
            color: green;
            font-size: 20;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
 
    <p id="p1">
      setProperty() Method
  </p>
 
    <button onclick="myFunction()">
        Set property
    </button>
 
    <p id="gfg"></p>
 
    <!-- Script to set the property -->
    <script>
        function myFunction() {
            var x =
              document.styleSheets[0].cssRules[2].style;
           
            x.setProperty("background-color", "black");
        }
    </script>
</body>
 
</html>


Output: 

Before clicking on the button:

  

After clicking on the button:

  

Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 9
  • Firefox 1
  • Safari 6
  • Opera 9

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

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!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 14 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials