CSS custom properties, also known as CSS variables, allow authors to store reusable values that can be used throughout a document. Custom properties are defined with a double hyphen prefix like --main-color and accessed with the var() function like color: var(--main-color). They can be used to create reusable themes, dynamic UIs that respond to user input, and more accessible interfaces. CSS variables provide inheritance, fallbacks, and other advantages over preprocessors. They are supported in all modern browsers but polyfills exist for older environments.
“CSS Custom Properties”— @geoffrey_crofte
A custom property is any
property whose name starts
with two dashes […] like --foo
11.
“CSS Custom Properties”— @geoffrey_crofte
Custom properties are solely for
use by authors and users; CSS
will never give them a meaning
beyond what is presented here.
12.
“CSS Custom Properties”— @geoffrey_crofte
Custom properties are solely for
use by authors and users; CSS
will never give them a meaning
beyond what is presented here.
14.
Custom properties definevariables
Variables are referenced with the var() notation
They are Case-Sensitive
Their value is “extremely permissive”
What the Spec says…
Why not pushingtransformations directly in JavaScript?
Now you’re wondering…
42.
Why not pushingtransformations directly in JavaScript?
Now you’re wondering…
43.
Why not pushingtransformations directly in JavaScript?
Now you’re wondering…
Maintainability / Portability
style=“transform…” is dirty. Period.
44.
Why not pushingtransformations directly in JavaScript?
Now you’re wondering…
Maintainability / Portability
style=“transform…” is dirty. Period.
Inheritance
style=“transform…” do not make --x and
--y inherited.
45.
Why not pushingtransformations directly in JavaScript?
Now you’re wondering…
Maintainability / Portability
style=“transform…” is dirty. Period.
Inheritance
style=“transform…” do not make --x and
--y inherited.
It’s a philosophy
CSS is for styling. JS is not.
46.
Why not pushingtransformations directly in JavaScript?
Now you’re wondering…
Maintainability / Portability
style=“transform…” is dirty. Period.
Inheritance
style=“transform…” do not make --x and
--y inherited.
It’s a philosophy
CSS is for styling. JS is not.
Futur proof
Let the CSS engine handle that part.
“CSS Custom Properties”— @geoffrey_crofte
Some resources
CSS Variable Secrets (Lea Verou Smashing Conf. 2017)
CSS Custom Properties (CCSWG W3C documentation)
CSS Mouse Tracking Gradient (Gradient on Text demonstration)
CSS Variable Tutorials (A series of 4 short videos about CSS Variables)