SlideShare a Scribd company logo
1 of 41
Download to read offline
One step in the future:
CSS Variables
version ✌
Giacomo Zinetti
Giko
Action plan
●
●
●
●
●
●
●
Theory
Define
--your-property: value;
How to define a property
:root {
--button-color: blue;
--align: left;
}
header {
--header-height: 300px;
}
Use
var(--your-property, default-value)
How to use a variable
button {
background-color: var(--button-color);
}
a {
color: var(--link-color, black);
}
Quiz time:
Inherited or
not?
Quiz time:
Inherited or
not?
VS Preprocessors
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
</footer>
</body>
Inheritance
:root { --link: red }
footer { --link: blue }
a { color: var(--link) }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
</footer>
</body>
$link: red
footer { $link: blue }
a { color: $link }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
</footer>
</body>
Inheritance compiled
:root { --link: red }
footer { --link: blue }
a { color: var(--link) }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
</footer>
</body>
$link: red
footer { $link: blue }
a { color: $link red }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
</footer>
</body>
Inheritance
:root { --link: red }
footer { --link: blue }
a { color: var(--link) }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
</footer>
</body>
$link: red
footer { $link: blue }
a { color: $link }
Preprocessors: Nope
:root {
--gutter: 10px;
}
@media (min-width: 768px) {
:root {
--gutter: 15px;
}
}
article {
padding: var(--gutter);
}
Media queries
$gutter: 10px;
@media (min-width: 768px) {
$gutter: 15px;
}
article {
padding: $gutter;
}
:root {
--gutter: 10px;
}
@media (min-width: 768px) {
:root {
--gutter: 15px;
}
}
article {
padding: var(--gutter);
}
Media queries compiled
$gutter: 10px;
@media (min-width: 768px) {
$gutter: 15px;
}
article {
padding: $gutter 10px;
}
:root {
--gutter: 10px;
}
@media (min-width: 768px) {
:root {
--gutter: 15px;
}
}
article {
padding: var(--gutter);
}
Media queries
$gutter: 10px;
@media (min-width: 768px) {
$gutter: 15px;
}
article {
padding: $gutter;
}
Preprocessors: Nope
:root {
--gutter: 10px;
}
@media (min-width: 768px) {
:root {
--gutter: 15px;
}
}
article {
padding: var(--gutter);
}
Preprocessors solution
$gutter: 10px;
$gutter-large: 15px;
@media (min-width: 768px) {
article {
padding: $gutter-large;
}
}
article {
padding: $gutter;
}
But…
:root {
--gutter: 10px;
}
@media (min-width: 768px) {
:root {
--gutter: 15px;
}
}
article.class {
padding: var(--gutter);
}
$gutter: 10px;
$gutter-large: 15px;
@media (min-width: 768px) {
article {
padding: $gutter-large;
}
}
article.class {
padding: $gutter;
}
But… they can be friends
Example
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
<section>
<a>Link 3</a>
</section>
</footer>
</body>
Base rule
a { background: var(--link, red) }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
<section>
<a>Link 3</a>
</section>
</footer>
</body>
Override variables, not properties
section { --link: green }
a { background: var(--link, red) }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
<section>
<a>Link 3</a>
</section>
</footer>
</body>
Make Cascade Love, not Specificity War
section { --link: green }
footer { --link: blue }
a { background: var(--link, red) }
Old school...
section { --link: green }
footer { --link: blue }
a { background: var(--link, red) }
footer section a { background: green }
footer a { background: blue }
a { background: red }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
<section>
<a>Link 3</a>
</section>
</footer>
</body>
Old school...
section { --link: green }
footer { --link: blue }
a { background: var(--link, red) }
--------------
footer section a { background: green }
footer a { background: blue }
a { background: red }
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
<section>
<a>Link 3</a>
</section>
</footer>
</body>
The end of specificity war!
Javascript
Read
getComputedStyle(element).getPropertyValue('--color');
element.style.setProperty('--color', 'red');
Write
Support
Browsers
49+ 31+ 15? 36+ 9.1+
Jan 2017: 68.34%
Fallback property
@supports (--foo: green) {
body {
color: var(--my-color);
}
}
Feature query
color: red;
color: var(--my-color, red);
Good news
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
<section>
<a>Link 3</a>
</section>
</footer>
</body>
We already use CSS variables
body { font-size: 18px }
footer { font-size: 16px }
section { font-size: 14px }
a {
border-width: 0.2em;
padding-right: 2em;
}
<body>
<a>Link 1</a>
<footer>
<a>Link 2</a>
<section>
<a>Link 3</a>
</section>
</footer>
</body>
The superhero: currentColor
body { color: red }
footer { color: blue }
section { color: green }
a {
border-color: currentColor;
}
Use case:
Theming
.theme(@bg: gray, @col: red, @var: blue) {
.box { background: @bg }
.button { color: @col }
.box .button { color: @var }
}
.theme();
.dark {
.theme(black, orange, lightgreen);
}
Theming with LESS
.box { background: gray }
.button { color: red }
.box .button { color: blue }
.dark .box { background: black }
.dark .button { color: orange }
.dark .box .button { color: lightgreen }
Theming with CSS
.box {
--col: var(--alt, blue);
background: var(--bg, lightgray);
}
.button {
color: var(--col, red);
}
.dark {
--bg: black;
--col: orange;
--alt: lightgreen;
}
Theming with CSS Variables
Questions?
Grazie!
@giacomozinetti

More Related Content

Viewers also liked

Presentación corporativa ABUS MÉXICO SECURITY SOLUTIONS
Presentación corporativa ABUS MÉXICO SECURITY SOLUTIONSPresentación corporativa ABUS MÉXICO SECURITY SOLUTIONS
Presentación corporativa ABUS MÉXICO SECURITY SOLUTIONSABUS MÉXICO SECURITY SOLUTIONS
 
Grids of Tomorrow: CSS Grid Layout
Grids of Tomorrow: CSS Grid LayoutGrids of Tomorrow: CSS Grid Layout
Grids of Tomorrow: CSS Grid LayoutSimone Lelli
 
Apresentação de Campanha Universidade Nilton Lins - Medicina
Apresentação de Campanha Universidade Nilton Lins - MedicinaApresentação de Campanha Universidade Nilton Lins - Medicina
Apresentação de Campanha Universidade Nilton Lins - MedicinaAndré Castro Barroso
 
ASSESSMENT AND EVALUATION TEST ENGLISH 7
ASSESSMENT AND EVALUATION TEST ENGLISH 7ASSESSMENT AND EVALUATION TEST ENGLISH 7
ASSESSMENT AND EVALUATION TEST ENGLISH 7Pristine Estorque
 
Leveraging Tech for Legal
Leveraging Tech for LegalLeveraging Tech for Legal
Leveraging Tech for LegalEvolve Law
 
Evaluation let it go campaign
Evaluation let it go campaignEvaluation let it go campaign
Evaluation let it go campaignStacey Johnson
 
Tata Tigor - Press Note
Tata Tigor - Press NoteTata Tigor - Press Note
Tata Tigor - Press NoteRushLane
 
Css figli di un dio minore
Css figli di un dio minoreCss figli di un dio minore
Css figli di un dio minoreDavide Di Pumpo
 
T1 Prima 2017 Season 4 Driver list
T1 Prima 2017 Season 4 Driver listT1 Prima 2017 Season 4 Driver list
T1 Prima 2017 Season 4 Driver listRushLane
 
Bootstrap 4 is Coming!
Bootstrap 4 is Coming!Bootstrap 4 is Coming!
Bootstrap 4 is Coming!Carmine Alfano
 

Viewers also liked (12)

Presentación corporativa ABUS MÉXICO SECURITY SOLUTIONS
Presentación corporativa ABUS MÉXICO SECURITY SOLUTIONSPresentación corporativa ABUS MÉXICO SECURITY SOLUTIONS
Presentación corporativa ABUS MÉXICO SECURITY SOLUTIONS
 
Grids of Tomorrow: CSS Grid Layout
Grids of Tomorrow: CSS Grid LayoutGrids of Tomorrow: CSS Grid Layout
Grids of Tomorrow: CSS Grid Layout
 
Apresentação de Campanha Universidade Nilton Lins - Medicina
Apresentação de Campanha Universidade Nilton Lins - MedicinaApresentação de Campanha Universidade Nilton Lins - Medicina
Apresentação de Campanha Universidade Nilton Lins - Medicina
 
Shivani Bio Data
Shivani Bio DataShivani Bio Data
Shivani Bio Data
 
ASSESSMENT AND EVALUATION TEST ENGLISH 7
ASSESSMENT AND EVALUATION TEST ENGLISH 7ASSESSMENT AND EVALUATION TEST ENGLISH 7
ASSESSMENT AND EVALUATION TEST ENGLISH 7
 
Leveraging Tech for Legal
Leveraging Tech for LegalLeveraging Tech for Legal
Leveraging Tech for Legal
 
Examen de entrada. solución
Examen de entrada. soluciónExamen de entrada. solución
Examen de entrada. solución
 
Evaluation let it go campaign
Evaluation let it go campaignEvaluation let it go campaign
Evaluation let it go campaign
 
Tata Tigor - Press Note
Tata Tigor - Press NoteTata Tigor - Press Note
Tata Tigor - Press Note
 
Css figli di un dio minore
Css figli di un dio minoreCss figli di un dio minore
Css figli di un dio minore
 
T1 Prima 2017 Season 4 Driver list
T1 Prima 2017 Season 4 Driver listT1 Prima 2017 Season 4 Driver list
T1 Prima 2017 Season 4 Driver list
 
Bootstrap 4 is Coming!
Bootstrap 4 is Coming!Bootstrap 4 is Coming!
Bootstrap 4 is Coming!
 

CSS from the future