Favorites

DOM CSSStyleDeclaration.setProperty() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
CSSStyleDeclaration.setProperty()
DOM CSSStyleDeclaration MethodThis method is supported by all modern browsers.
Updated 28th November 2026

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

<div id="card" style="border:3px solid var(--accent, gray)">Card</div>
const card = document.getElementById('card');
card.style.setProperty('--accent', 'tomato');
card.style.setProperty('padding', '16px', 'important');
console.log(card.style.getPropertyValue('--accent'));
console.log(card.getAttribute('style'));

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee