api-member

Cssstyledeclaration.setproperty — CSS reference

Writes a CSS property by its hyphenated name. Priority is a separate optional argument: use important or the empty string, not !important inside the value. The declaration still participates in the cascade. Here inline important overrides an important author rule.

Formal Syntax & Structure

css syntaxcss
declaration.setProperty("color", "blue", "important")

Working Example

Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>CSSStyleDeclaration.setProperty</title><style>body{margin:8px;font:16px/20px sans-serif;color:black;background:white}#a{color:green!important}</style><body><p id="a">Sample text</p><p>Run once, then reload the page to repeat from the original state.</p><button id="run" type="button">Run example</button><pre id="result" aria-live="polite"></pre><script>const target=document.getElementById('a');const sheet=document.styleSheets[0];async function demo(){target.style.setProperty("color","blue","important");return [target.style.getPropertyPriority("color"),getComputedStyle(target).color];}document.getElementById('run').onclick=async()=>{try{document.getElementById('result').textContent=JSON.stringify(await demo());}catch(e){document.getElementById('result').textContent=e.name+': '+e.message;}};</script></body></html>
CSS Reference