Javascript CSSStyleDeclaration setProperty Method

Introduction

Set a new CSS property:

Click the button to set a new CSS property.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#ex1 {/*from ww  w  .ja  va  2 s  .c  o m*/
  color: red;
}
</style>
</head>
<body>
<button onclick="myFunction()">Test</button>
<div id="ex1">Some text.</div>
<script>
function myFunction() {
  var declaration = document.styleSheets[0].cssRules[0].style;
  var setprop = declaration.setProperty("background-color", "yellow");
}
</script>

</body>
</html>

The setProperty() method sets a new or modifies an existing CSS property in a CSS declaration block.

setProperty(propertyname, value, priority);

Parameter Values

Parameter
Description
propertyname
Required. A String representing the name of the property to set
value
Optional. A String representing the new value
priority




Optional. A String representing if the property's priority should be set to important or not.
Legal values:
"important"
undefined
""



PreviousNext

Related