I thought to start from explaining the purpose of having variables in CSS
but actually popularity of CSS pre/postprocessors already did that.
- color variables for branding
- consistent components properties (layout, positioning etc.)
- avoid code duplications
Yes, of course, you still can search and find/replace all you need manually in your codebase
but it’s like not having variables in JS- pain.
Having dynamic and scoped CSS variables provides even more abilities for your experiments and applications-
you can read, set and update them on the fly!
Also you can prevent code duplications around your codebase as recently
Webkit guys did in their Web Inspector.
And finally you have an interface to easily pass data from CSS to JS (for example media breakpoint values).
Here is the short-list of features CSS properties have:
- they are dynamic and can be changed at runtime
- they are easily to read/write from JS
- they inherit, cascade and have scopes
So let’s dive deeper what CSS properties are and how to use them.