CSS
Updating CSS Variables with Javascript
📣 Sponsor
If you are building an application or SaaS platform, or in fact any website, it can be useful to store all your colours as CSS variables, and then update these once to change the theme of the app.
Fortunately this is relatively straightforward to accomplish. Imagine you have your CSS variables set up like this:
:root {
--main-background: blue;
--main-sidebar: red;
/* Yes these colors do not match */
}
:root
CSS styles are stored on document.documentElement
. So we can access all of its styles via that. So in JS, all we need to do is this:
document.documentElement.style.setProperty("--main-background", "yellow");
Last Updated 1609074689603
More Tips and Tricks for CSS
- CSS Transitions
- An Interactive Guide to CSS Flex Box
- How to Create Animated Cards with WebGL and Three.js
- Creating a Javascript Drawing and Annotation Application
- A Complete Guide to How the CSS not Selector Works
- How the CSS Box Model Works
- Multi colored text in CSS
- CSS Text
- Creating Custom, Interactive CSS Checkboxes
- A Guide to CSS Selectors