Categories
JavaScript Answers

How to change CSS :root color variables in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *