SlideShare a Scribd company logo
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.
CSS Custom Properties (aka CSS Variable)
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

More Related Content

Similar to CSS Custom Properties (aka CSS Variable) (20)

KEY
A brief look at CSS3 techniques by Aaron Rodgers, Web Designer @ vzaar.com
applicake
 
PDF
Intro to CSS3
Denise Jacobs
 
KEY
Slow kinda sucks
Tim Wright
 
KEY
Sass Essentials at Mobile Camp LA
Jake Johnson
 
PDF
960 grid psd
Raju Nag
 
PDF
Simply Responsive CSS3
Denise Jacobs
 
PDF
Css3 transitions and animations + graceful degradation with jQuery
Andrea Verlicchi
 
PDF
CSS3: Ripe and Ready
Denise Jacobs
 
PDF
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Erin M. Kidwell
 
PDF
Refresh OKC
Nathan Smith
 
PDF
CSS3: Ripe and Ready to Respond
Denise Jacobs
 
KEY
Advanced sass/compass
Nick Cooley
 
PPTX
The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}
Eric Carlisle
 
PPTX
Diazo: Bridging Designers and Programmers
TsungWei Hu
 
PDF
OOScss Architecture For Rails Apps
Netguru
 
PPT
Toplax
Xavier Badosa
 
PPT
Intermediate Dreamweaver (2007)
Matteo Wyllyamz
 
PDF
Rapid Templating with Serve
Nathan Smith
 
A brief look at CSS3 techniques by Aaron Rodgers, Web Designer @ vzaar.com
applicake
 
Intro to CSS3
Denise Jacobs
 
Slow kinda sucks
Tim Wright
 
Sass Essentials at Mobile Camp LA
Jake Johnson
 
960 grid psd
Raju Nag
 
Simply Responsive CSS3
Denise Jacobs
 
Css3 transitions and animations + graceful degradation with jQuery
Andrea Verlicchi
 
CSS3: Ripe and Ready
Denise Jacobs
 
Girl Develop It Cincinnati: Intro to HTML/CSS Class 4
Erin M. Kidwell
 
Refresh OKC
Nathan Smith
 
CSS3: Ripe and Ready to Respond
Denise Jacobs
 
Advanced sass/compass
Nick Cooley
 
The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}
Eric Carlisle
 
Diazo: Bridging Designers and Programmers
TsungWei Hu
 
OOScss Architecture For Rails Apps
Netguru
 
Intermediate Dreamweaver (2007)
Matteo Wyllyamz
 
Rapid Templating with Serve
Nathan Smith
 

Recently uploaded (20)

PPTX
QBDqbdqbdqbdqbdqbdqbdqbdqbdqbqdbqd .pptx
komoja3525
 
PDF
HISTORY OF Ethiopia and Horn of African
dereab29
 
PPTX
FPCL Land Acquisition.pptxgggggjjjjjjjjj
radiologisthinarana
 
PPTX
Robotic Arm Control System for help of robots you can easily operate things t...
altron1331
 
PPTX
Modelling for etab and design on etabs for concrete
MohamedAttia601252
 
PPTX
search engine optimization123244556.pptx
revathi148366
 
PPTX
原版加拿大亚岗昆学院毕业证(AC毕业证书)如何办理
Taqyea
 
PPTX
Untitled presentation (1).pptx testdsaff
David Arbirk
 
PDF
How to Style a Pink Puff-Sleeve Dress for the Ultimate Gen-Z Party Look | Boh...
boheewohee
 
PPTX
Turn prompts into brochures - AI Brochure Generator
Venngage AI Infographic Generator
 
PPTX
Infection Control Practicesin do ors .pptx
drinayadfm
 
PDF
Guide to Understanding Hailey Welch's Wealth
arslantaj725
 
PDF
COLOUR IN INTERIOR DESIGN- KAVYA CHAWLA .pdf
KavyaChawla4
 
PDF
Cannatopolis Brand Book - brand identity
impybla
 
PDF
The Third Place revolution: Designing for community in a fragmented world
jgadsbypeet8321
 
PPTX
Iot module of the module 4 is the very beautiful
prodbythre
 
PPTX
BOILER STEAM bana M ntpc kahalgaon boiler .pptx
didikesaadi1108
 
PPTX
The Costly Mistakes Homeowners Make in Dining Room Renovations
anurag anand
 
PPTX
etab modelling and design of concrete elemnts
MohamedAttia601252
 
PDF
GATE-26_AE_Online_CGATE-26_AE_Online_CGATE-26_AE_Online_C
aerom2341
 
QBDqbdqbdqbdqbdqbdqbdqbdqbdqbqdbqd .pptx
komoja3525
 
HISTORY OF Ethiopia and Horn of African
dereab29
 
FPCL Land Acquisition.pptxgggggjjjjjjjjj
radiologisthinarana
 
Robotic Arm Control System for help of robots you can easily operate things t...
altron1331
 
Modelling for etab and design on etabs for concrete
MohamedAttia601252
 
search engine optimization123244556.pptx
revathi148366
 
原版加拿大亚岗昆学院毕业证(AC毕业证书)如何办理
Taqyea
 
Untitled presentation (1).pptx testdsaff
David Arbirk
 
How to Style a Pink Puff-Sleeve Dress for the Ultimate Gen-Z Party Look | Boh...
boheewohee
 
Turn prompts into brochures - AI Brochure Generator
Venngage AI Infographic Generator
 
Infection Control Practicesin do ors .pptx
drinayadfm
 
Guide to Understanding Hailey Welch's Wealth
arslantaj725
 
COLOUR IN INTERIOR DESIGN- KAVYA CHAWLA .pdf
KavyaChawla4
 
Cannatopolis Brand Book - brand identity
impybla
 
The Third Place revolution: Designing for community in a fragmented world
jgadsbypeet8321
 
Iot module of the module 4 is the very beautiful
prodbythre
 
BOILER STEAM bana M ntpc kahalgaon boiler .pptx
didikesaadi1108
 
The Costly Mistakes Homeowners Make in Dining Room Renovations
anurag anand
 
etab modelling and design of concrete elemnts
MohamedAttia601252
 
GATE-26_AE_Online_CGATE-26_AE_Online_CGATE-26_AE_Online_C
aerom2341
 
Ad

CSS Custom Properties (aka CSS Variable)