one step in the future: css variables

33
ONE STEP IN THE FUTURE: CSS VARIABLES 25 MARCH 2016

Upload: giacomo-zinetti

Post on 08-Jan-2017

674 views

Category:

Software


1 download

TRANSCRIPT

Page 1: One step in the future: CSS variables

ONE STEP IN THE FUTURE:CSS VARIABLES

25 MARCH 2016

Page 2: One step in the future: CSS variables

GIACOMO "GIKO" ZINETTI

NERD FRONTEND WEB DEVELOPER

@GIACOMOZINETTI

Page 3: One step in the future: CSS variables

LET'S GO

Page 4: One step in the future: CSS variables

CSS CUSTOM PROPERTIESFOR CASCADING VARIABLES

MODULE LEVEL 1

Page 5: One step in the future: CSS variables

WAT

ONE STEP BEYOND...

Page 6: One step in the future: CSS variables

PROPERTIES

Thanks to for the imageStyleStats

Page 7: One step in the future: CSS variables

CASCADINGThe assigns a weight to each style rule. Whenseveral rules apply, the one with the greatest weight takes

precedence.

CSS cascade

Page 8: One step in the future: CSS variables

CSS VERSIONINGWHAT IS CSS3?

Everything after [7 June 2011]CSS 2.1

CSS4 EXISTS?No, it's time to talk about modules

Page 9: One step in the future: CSS variables

CSS CUSTOM PROPERTIESFOR CASCADING VARIABLES

MODULE LEVEL 1

Page 10: One step in the future: CSS variables

CUSTOM PROPERTIESPossibility to de�ne new properties with the -- pre�x

--custom-property: value;

They are case sensitive!

Page 11: One step in the future: CSS variables

CASCADING VARIABLESA new primitive value type

property: var(--custom-property);

And you can set a default

var(--custom-property, default-value)

Page 12: One step in the future: CSS variables

SOME EXAMPLES

Page 13: One step in the future: CSS variables

.box { background-color: white; } .button { color: black; }

.theme .box { background-color: blue; } .theme .button { color: yellow; }

THEMING TODAY

Page 14: One step in the future: CSS variables

// Deafult css .box { background-color: white; } .button { color: black; }

// Theme file .box { background-color: blue !important; } .button { color: yellow !important; }

THEMING TODAY V2

Page 15: One step in the future: CSS variables

.theme { --color-bg: blue; --color-button: yellow; }

.box { background-color: var(--color-bg, white); }

.button { color: var(--color-button, black); }

THEMING TOMORROW

Page 16: One step in the future: CSS variables

body.class { font-size: 14px; }

@media (min-width: 960px) {

body { font-size: 16px; } }

RESPONSIVE TODAY

Page 17: One step in the future: CSS variables

body { font-size: var(--base-font, 14px); }

@media (min-width: 960px) {

body { --base-font: 16px; } }

RESPONSIVE TOMORROW

Page 18: One step in the future: CSS variables

.button { color: red; }

footer .button { color: white; }

footer .highlight .button { color: yellow; }

SPECIFICITY WARS TODAY

Page 19: One step in the future: CSS variables

footer { --button-color: white; }

.highlight { --button-color: yellow; }

.button { color: var(--button-color, red); }

SPECIFICITY WARS TOMORROW

Page 20: One step in the future: CSS variables

 

Page 21: One step in the future: CSS variables

TODAY

Page 22: One step in the future: CSS variables

 

Page 23: One step in the future: CSS variables

COMPATIBILITYW3C Candidate Recommendation, 03 December 2015

Firefox >= 31Chrome >= 49Safari >= 9.1Edge: Under Consideration

Global support 26.01%, data from Can I Use...

Page 24: One step in the future: CSS variables

.Box { // Default value for all browsers color: blue;

// Override default with variable for new browsers color: var(--color-accent, blue) !important; }

PROGRESSIVE ENHANCEMENTUSE DOUBLE DECLARATION

A small example on Codepen

Page 25: One step in the future: CSS variables

.hide\@cp { // Ignored by old browsers display: var(--fake-unset-custom-prop, none); }

.show\@cp { // Hidden for all browsers display: none; // And set visible just for new browsers display: var(--fake-unset-custom-prop, initial); }

CUSTOM PROPERTIES MEDIA QUERY

Page 26: One step in the future: CSS variables

BONUS TRACK

Page 27: One step in the future: CSS variables

:root { --custom-prop: value; }

ROOT SCOPE

Page 28: One step in the future: CSS variables

.box { --color: red; background-color: var(--color); transition: all ease 500ms; }

.box:hover { --color: blue; }

TRANSITION AND ANIMATION

Page 29: One step in the future: CSS variables

$button-color: red; .header { $button-color: blue; } .button { color: $button-color; }

:root { --button-color: red; } .header { --button-color: blue; } .button { color: var(--button-color); }

CSS VS PREPROCESSORSPreprocessor variables don’t inherit

Even CSSNext

Page 30: One step in the future: CSS variables

LINKS: Try the color picker

My on Codepen: tool for css statistics

This slides:

W3C Speci�cationCan I Use... CSS VariablesGiko.it

custom properties playgroundStyleStats

goo.gl/cItM4J

Page 31: One step in the future: CSS variables

QUESTIONS?

Page 32: One step in the future: CSS variables

THANK YOUSO LONG AND THANKS FOR ALL THE FEEDBACK!

@giacomozinetti

#betterweb

Page 33: One step in the future: CSS variables