DOM CSSStyleDeclaration.setProperty() Method
The setProperty() method sets a CSS property on a style object using its CSS name, which is how you set custom properties like --accent from JavaScript.
Description
The setProperty() method sets a CSS property using its CSS name, with an optional "important" priority. It's the only way to set custom properties (CSS variables) from JavaScript, and setting them on document.documentElement changes them for the whole page. getPropertyValue() reads a value and removeProperty() removes one.
An invalid value is ignored without any error, leaving the previous value in place, so check the result with getPropertyValue() if you aren't sure the value is valid. Passing null or an empty string removes the property.
Custom properties set in JavaScript are a clean way to connect script and styles: set --progress or --x from JavaScript and let CSS use it in calculations, transforms or colours, rather than writing every style from script.
Syntax
style.setProperty(propertyName, value)
style.setProperty(propertyName, value, priority)Parameters
- propertyNameThe CSS property name in its CSS form, such as
"background-color"or"--accent". - valueThe new value, or an empty string to remove it.
- priorityOptional.
"important"to add!important.
Return Value
None (undefined).
Example
Browser Support
The following information will show you the current browser support for the CSSStyleDeclaration.setProperty() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





