Sometimes, we want to change CSS :root color variables in JavaScript.
In this article, we’ll look at how to change CSS :root color variables in JavaScript.
How to change CSS :root color variables in JavaScript?
To change CSS :root color variables in JavaScript, we can use the document.documentElement.style.setProperty
method.
For instance, we write
document.documentElement.style.setProperty("--your-variable", "#YOURCOLOR");
to call the document.documentElement.style.setProperty
method to define the --your-variable
CSS variable.
We set its value to #YOURCOLOR
.
Conclusion
To change CSS :root color variables in JavaScript, we can use the document.documentElement.style.setProperty
method.