Okay, we'll go with this quick and straight. When you first start diving into CSS, you do the usual thing like changing color, changing fonts, etc. Then you dive deep into media queries, cross-browser properties, and finally to variables.
But what if we use these variables in our CSS files alongside some magic of our old friend JavaScript, then imagine what we can achieve! Something cool? Yes, you're correct. Let's do this and make something nice.
Something to sprinkle!
Quick note on CSS variables 📝
Of course, some basics first.
CSS Custom Properties or CSS Variables allows us to store a value stored in one place, then referenced in multiple other places.
Sometimes specific values need to be reused throughout a document. A typical example is when you get a specific color palette from designers, and you need to add specific hex values of colors, font-sizes, or even some responsive breakpoints. You assign these values to your custom-made CSS properties called variables here.
This is useful not only because they can be used at multiple instances and makes editing the values easy but also, it makes the properties easier to read when referring to it later. For example: --headline-color is better to read than #000.
Declaring a custom CSS property is created by assigning the double hyphen (--) in front of the variable name, and then the property value is written like any other CSS property.
Check out this example:
element {
--main-bg-color: lightgray;
}
Now, to use this custom property anywhere in your CSS file you can do this:
element {
background-color: var(--main-bg-color);
}
So, you don't need to write lightgray value for the background-color in all places where there is a need to use the var() function and pass in the CSS custom variable inside.
Introducing JavaScript ✨
Time to start interacting with the web developer's favorite language.
Let's see how we can write a new property value in JavaScript.
➡️ setProperty
var element = document.documentElement;
element.style.setProperty('--name', value);
style.setProperty(): it sets a new value for a property on a CSS style declaration object.
The setProperty() takes in the property name, it's value, and optionally the priority.
The code you saw above sets a new value for a globally defined property. This type of JavaScript code is useful when you are managing state and then modifying CSS styles based on given values.
➡️ getPropertyValue
element.style.getPropertyValue('--my-color');
Yeah, exactly what you're thinking right now. Just as with any other language, we have setters and getters here too. With the setProperty we were setting a new value, and here with getPropertyValue, we return a DOMString containing the value of the above-specified CSS property.
That is it! There are endless possibilities when it comes to combining the power of CSS variables with JavaScript. You can make awesome DOM-based games, use it to change component styles dynamically, and more!
More resources 🤩
Go ahead and learn more about custom CSS properties from the resources given below: