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.
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.
The setProperty() takes in the property name, it's value, and optionally the priority.
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.
More resources 🤩
Go ahead and learn more about custom CSS properties from the resources given below: