Slide 1

Slide 1 text

CSS Custom Properties Add Variables to Your CSS

Slide 2

Slide 2 text

“CSS Custom Properties” — @geoffreycrofte 2 Geoffrey Crofte geoffrey.crofte.fr/en creativejuiz.fr/en Lead Designer at Foyer Group @geoffreycrofte

Slide 3

Slide 3 text

“CSS Custom Properties” — @geoffreycrofte Overview What are Custom Properties? Quick start with CSS Variables. Why CSS C.P. instead of Sass variables? Use cases & Demos Takeaways

Slide 4

Slide 4 text

What are Custom Properties?

Slide 5

Slide 5 text

What are Custom Properties? CSS Variables IIIIIIII

Slide 6

Slide 6 text

$color: #bada55; @color: #bada55; This not about…

Slide 7

Slide 7 text

This is more about… http://bit.ly/csscurrentcolor

Slide 8

Slide 8 text

This is more about… http://bit.ly/csscurrentcolor

Slide 9

Slide 9 text

currentColor But this is not about…

Slide 10

Slide 10 text

“CSS Custom Properties” — @geoffreycrofte A custom property is any property whose name starts with two dashes […] like --foo

Slide 11

Slide 11 text

“CSS Custom Properties” — @geoffreycrofte Custom properties are solely for use by authors and users; CSS will never give them a meaning beyond what is presented here.

Slide 12

Slide 12 text

“CSS Custom Properties” — @geoffreycrofte Custom properties are solely for use by authors and users; CSS will never give them a meaning beyond what is presented here.

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Custom properties define variables Variables are referenced with the var() notation They are Case-Sensitive Their value is “extremely permissive” What the Spec says…

Slide 15

Slide 15 text

Do the fu%€ you want with them. In other words…

Slide 16

Slide 16 text

How to use CSS Variables?

Slide 17

Slide 17 text

--variableName: value; CSS Declaration :root { }

Slide 18

Slide 18 text

--variableName: value; CSS Declaration .element { }

Slide 19

Slide 19 text

property: var(--variableName); .element { } CSS Use

Slide 20

Slide 20 text

Quick Example of use

Slide 21

Slide 21 text

Why CSS instead of Sass Variables?

Slide 22

Slide 22 text

The main issue with Sass (or LESS) variables: They have to be computed to get their value. Computation

Slide 23

Slide 23 text

CSS Variables are alive

Slide 24

Slide 24 text

CSS Variables are alive

Slide 25

Slide 25 text

JavaScript can access them

Slide 26

Slide 26 text

Media Queries ❤ CSS Variables

Slide 27

Slide 27 text

Media Queries ❤ CSS Variables

Slide 28

Slide 28 text

CSS Variables are inherited

Slide 29

Slide 29 text

CSS Variables are inherited

Slide 30

Slide 30 text

CSS Variables are inherited

Slide 31

Slide 31 text

CSS Variables are inherited

Slide 32

Slide 32 text

CSS Variables are inherited

Slide 33

Slide 33 text

CSS Variables are inherited

Slide 34

Slide 34 text

Demos & Use Cases

Slide 35

Slide 35 text

http://bit.ly/cssvargradient

Slide 36

Slide 36 text

http://bit.ly/cssvargradient

Slide 37

Slide 37 text

Mouse position

Slide 38

Slide 38 text

CSS Transformation

Slide 39

Slide 39 text

CSS Transformation

Slide 40

Slide 40 text

CSS Transformation

Slide 41

Slide 41 text

CSS Transformation

Slide 42

Slide 42 text

CSS Transformation

Slide 43

Slide 43 text

CSS Transformation

Slide 44

Slide 44 text

CSS Transformation

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Why not pushing transformations directly in JavaScript? Now you’re wondering… Maintainability / Portability 
 style=“transform…” is dirty. Period. 


Slide 48

Slide 48 text

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.

Slide 49

Slide 49 text

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. 


Slide 50

Slide 50 text

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.

Slide 51

Slide 51 text

http://bit.ly/cssvargradient2

Slide 52

Slide 52 text

http://bit.ly/cssvargradient2

Slide 53

Slide 53 text

CSS Transformation

Slide 54

Slide 54 text

Themable interfaces

Slide 55

Slide 55 text

Themable interfaces

Slide 56

Slide 56 text

Themable interfaces

Slide 57

Slide 57 text

Themable interfaces

Slide 58

Slide 58 text

Themable interfaces

Slide 59

Slide 59 text

Themable interfaces

Slide 60

Slide 60 text

Accessibility Example

Slide 61

Slide 61 text

Accessibility Example

Slide 62

Slide 62 text

Responsive

Slide 63

Slide 63 text

Responsive

Slide 64

Slide 64 text

Customization https://blog.fontawesome.com/introducing-duotone/

Slide 65

Slide 65 text

Further Takeaways

Slide 66

Slide 66 text

--variableName: value !important; :root { } CSS Variables are CSS properties

Slide 67

Slide 67 text

--variableName: lolilol; :root { } (kind of) Silent error

Slide 68

Slide 68 text

--spacing: 20; :root { } You can’t “build up” values. margin: var(--spacing)px; :root { }

Slide 69

Slide 69 text

--spacing: 20; :root { } You can’t “build up” values. margin: var(--spacing)px; :root { } Doesn’t work

Slide 70

Slide 70 text

--spacing:; :root { } Strange behaviour --spacing: ; :root { } Invalid Valid Yeah, that’s a space caracter.

Slide 71

Slide 71 text

Fallback value var(--variableName, default);

Slide 72

Slide 72 text

Fallback value

Slide 73

Slide 73 text

Fallback value Doesn’t support var()

Slide 74

Slide 74 text

Fallback value Doesn’t support var() Fallback is there

Slide 75

Slide 75 text

Fallback value Doesn’t support var() Fallback is there

Slide 76

Slide 76 text

Fallback value Doesn’t support var() Fallback is there --bgColor Value

Slide 77

Slide 77 text

Fallback value Doesn’t support var() Fallback is there --bgColor Value

Slide 78

Slide 78 text

Fallback value Doesn’t support var() Fallback is there --bgColor Value Transparent

Slide 79

Slide 79 text

Variable as Fallback var(--var1, var(--var2, default));

Slide 80

Slide 80 text

Variable as Fallback var(--var1, var(--var2, var(--var3, var(--var4, var(--var5, default)))));

Slide 81

Slide 81 text

You can’t cycle with variables

Slide 82

Slide 82 text

You can’t cycle with variables

Slide 83

Slide 83 text

@supports (color: var(--)) { … } Support Testing

Slide 84

Slide 84 text

Compatibility

Slide 85

Slide 85 text

Polyfills https://github.com/aaronbarker/css-variables-polyfill

Slide 86

Slide 86 text

“CSS Custom Properties” — @geoffreycrofte 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)

Slide 87

Slide 87 text

Thanks!

Slide 88

Slide 88 text

“CSS Custom Properties” — @geoffreycrofte 56 Geoffrey Crofte geoffrey.crofte.fr/en creativejuiz.fr/en Lead Designer at Foyer Group @geoffreycrofte