KEMBAR78
CSS Custom Properties (aka CSS Variable) | PDF
CSS Custom Properties
Add Variables to Your CSS
“CSS Custom Properties” — @geoffrey_crofte 2
Geoffrey Crofte
geoffrey.crofte.fr
creativejuiz.fr
UX/UI Web Designer at Maltem (Foyer)
@geoffrey_crofte
“CSS Custom Properties” — @geoffrey_crofte
Overview
What are Custom Properties?
Quick start with CSS Variables.
Why CSS C.P. instead of Sass variables?
Use cases & Demos
Takeaways
What are Custom Properties?
What are Custom Properties?
CSS Variables
IIIIIIII
$color: #bada55;
@color: #bada55;
This not about…
This is more about…
http://bit.ly/csscurrentcolor
This is more about…
http://bit.ly/csscurrentcolor
currentColor
But this is not about…
“CSS Custom Properties” — @geoffrey_crofte
A custom property is any
property whose name starts
with two dashes […] like --foo
“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.
“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.
Custom properties define variables
Variables are referenced with the var() notation
They are Case-Sensitive
Their value is “extremely permissive”
What the Spec says…
Do the fu%€
you want with them.
In other words…
How to use CSS Variables?
--variableName: value;
CSS Declaration
:root {
}
--variableName: value;
CSS Declaration
.element {
}
property: var(--variableName);
.element {
}
CSS Use
Quick Example of use
Why CSS instead of Sass Variables?
The main issue with Sass (or LESS) variables:
They have to be computed to get their value.
Computation
CSS Variables are alive
JavaScript can access them
Media Queries ❤ CSS Variables
Media Queries ❤ CSS Variables
CSS Variables are inherited
CSS Variables are inherited
CSS Variables are inherited
CSS Variables are inherited
Demos & Use Cases
http://bit.ly/cssvargradient
Mouse position
CSS Transformation
CSS Transformation
CSS Transformation
CSS Transformation
CSS Transformation
CSS Transformation
CSS Transformation
Why not pushing transformations directly in JavaScript?
Now you’re wondering…
Why not pushing transformations directly in JavaScript?
Now you’re wondering…
Why not pushing transformations directly in JavaScript?
Now you’re wondering…
Maintainability / Portability

style=“transform…” is dirty. Period.

Why not pushing transformations directly in JavaScript?
Now you’re wondering…
Maintainability / Portability

style=“transform…” is dirty. Period.

Inheritance

style=“transform…” do not make --x and
--y inherited.
Why not pushing transformations 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.

Why not pushing transformations 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.
http://bit.ly/cssvargradient2
CSS Transformation
Themable interfaces
Themable interfaces
Themable interfaces
Themable interfaces
Themable interfaces
Accessibility Example
Accessibility Example
Responsive
Responsive
Further Takeaways
--variableName: value !important;
:root {
}
CSS Variables are CSS properties
--variableName: lolilol;
:root {
}
(kind of) Silent error
--spacing: 20;
:root {
}
You can’t “build up” values.
margin: var(--spacing)px;
:root {
}
--spacing: 20;
:root {
}
You can’t “build up” values.
margin: var(--spacing)px;
:root {
}
Doesn’t work
--spacing:;
:root {
}
Strange behaviour
--spacing: ;
:root {
}
Invalid
Valid
Yeah, that’s a space caracter.
Fallback value
var(--variableName, default);
Fallback value
Fallback value
Doesn’t support var()
Fallback value
Doesn’t support var()
Fallback is there
Fallback value
Doesn’t support var()
Fallback is there
Fallback value
Doesn’t support var()
Fallback is there
--bgColor Value
Fallback value
Doesn’t support var()
Fallback is there
--bgColor Value
Fallback value
Doesn’t support var()
Fallback is there
--bgColor Value
Transparent
Variable as Fallback
var(--var1, var(--var2, default));
Variable as Fallback
var(--var1, var(--var2, var(--var3,
var(--var4, var(--var5,
default)))));
You can’t cycle with variables
You can’t cycle with variables
@supports (color: var(--)) {
…
}
Support Testing
Compatibility
Polyfills
https://github.com/aaronbarker/css-variables-polyfill
“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)
Thanks!
“CSS Custom Properties” — @geoffrey_crofte 55
Geoffrey Crofte
geoffrey.crofte.fr
creativejuiz.fr
UX/UI Web Designer at Maltem (Foyer)
@geoffrey_crofte

CSS Custom Properties (aka CSS Variable)