CSSStyleDeclaration setProperty() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

Supported Browsers:

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


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.