Amazing CSS Variables
We eagerly need variables in CSS by long ages. Now, with build systems, we can use variables with Sass and Less. What CSS Variables will bring to us? Something Sass/Less never give us.
Dynamic
The places of variables in Sass/Less, will token by constants after build. Therefore, you can not change them. They are not real variables. On the other hand, CSS Variables is real variables, you can change them by @media or JS.
Better Scope Role
CSS Variables, also named Custom Properties, they don’t like variables in Sass/Less,
but like normal CSS Properties.
The scope of variables in Sass/Less is between a pair of {}
.
CSS Variables, however, like other CSS Properties,
defined in CSSOM and bing to DOM with selector.
and available in the DOM element and its children (and children of children, and children of children of children…).
So the scope is depend on DOM tree. That’s impossible in Sass/Less.
Componentization and Custom Properties
In this example, we set size of buttons with --button-size
.
If it’s undefined, use the default value 1
.
This rule apply on all <button>
s in the page.
And we set --button-size: 1.5
on selector body>main
.
So the variable will apply on <main>
directly belongs to <body>
and it’s children.
As we see, height of the <button>
s in this <main>
will be 1.5rem
,
and the height of the <button>
s outside will be 1rem
.
You may say, what? use a variable before declaring?
Um, let’s think out side the box, the button {}
as a function, and --button-size
as the parameter.
The function called on rendering of <button>
, and the value defined on body>main
pass in.
This little example show the basic componentization.
The <button>
is a component with styles inside, and with some parameters.
We keep not changing styles inside the box, but only change the parameters when use it.
The CSS Variables becomes the properties of the component.
It’s hard to make world ordered by this primitive componentization. Fortunately, we have Web Components.
Work with Web Components
In Custom Elements, styles are stand alone with outside. But the CSS Variables can penetrate the boundary.
Here is a example.
Here, we set size of buttons just like previous.
And, we declare --button-size
on the :host
, but not --font-size
.
The difference is: the variable --font-size
can be defined on any layer outside,
but the --button-size
must be defined on the custom element itself.
We can use two ways on different scene.
The way like --button-size
, is the common way to defined a parameter of element.
And the way like --font-size
, can use to access global configurations.
Interaction with JS
Here are the API of CSS properties (include Custom Properties) for JS.
This is the better way to access CSS properties then read or assign the properties of the style object.
Here is a situation that we get the position of mouse by JS, and read the layout. Then make some calculations with JS. And set some value to styles. That’s dreadful, JS has a hand in the layout, break the separation of concerns. And when JS read the layout, unnecessary reflow will happen.
Using CSS Variables, the problems solved perfectly.
You just need to pass the position from JS to CSS Variables.
And do nothing else in JS. In css, using calc()
to take layout calculations.
The separation of concerns is obeyed. Unnecessary reflows are prevented too.
Data Types
In CSS, the basic data types are number(with unit), string, keyword, color and so on, and list, besides. All of them can be the type of CSS Variables.
You can combine them and calculate.
You must pay attention to the unit when calculating.
To the lists, you need assume ,
and /
as elements of list just like 1px
.
Work with Animation and Transition
Unfortunately, there is no browser support transition of CSS Variables. We can only use animation and transition on normal CSS properties.
Therefore, some exciting idea cannot achieve. Such as change hue and lightness by different speed, change color of gradient background… Let’s just await for future.