+ All Categories
Home > Software > CSS from the future

CSS from the future

Date post: 19-Mar-2017
Category:
Upload: giacomo-zinetti
View: 71 times
Download: 0 times
Share this document with a friend
41
One step in the future: CSS Variables version
Transcript
Page 1: CSS from the future

One step in the future:CSS Variables

version ✌�

Page 2: CSS from the future
Page 3: CSS from the future

Giacomo ZinettiGiko

Page 4: CSS from the future

Action plan

●●●●●●●

Page 5: CSS from the future

Theory �

Page 6: CSS from the future

Define

--your-property: value;

Page 7: CSS from the future

How to define a property

:root {

--button-color: blue;

--align: left;

}

header {

--header-height: 300px;

}

Page 8: CSS from the future

Use

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

Page 9: CSS from the future

How to use a variable

button {

background-color: var(--button-color);

}

a {

color: var(--link-color, black);

}

Page 10: CSS from the future

Quiz time: Inherited or

not?

Page 11: CSS from the future

Quiz time: Inherited or

not?

Page 12: CSS from the future

VS Preprocessors �

Page 13: CSS from the future

<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 }

Page 14: CSS from the future

<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 }

Page 15: CSS from the future

<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: N

ope

Page 16: CSS from the future

: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;

}

Page 17: CSS from the future

: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;

}

Page 18: CSS from the future

: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: N

ope

Page 19: CSS from the future

: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;

}

Page 20: CSS from the future

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;

}

Page 21: CSS from the future

But… they can be friends

Page 22: CSS from the future

Example ��

Page 23: CSS from the future

<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) }

Page 24: CSS from the future

<body>

<a>Link 1</a>

<footer>

<a>Link 2</a>

<section>

<a>Link 3</a>

</section>

</footer>

</body>

Override variables, not propertiessection { --link: green }

a { background: var(--link, red) }

Page 25: CSS from the future

<body>

<a>Link 1</a>

<footer>

<a>Link 2</a>

<section>

<a>Link 3</a>

</section>

</footer>

</body>

Make Cascade Love, not Specificity Warsection { --link: green }

footer { --link: blue }

a { background: var(--link, red) }

Page 26: CSS from the future

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>

Page 27: CSS from the future

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 specific

ity war!

Page 28: CSS from the future

Javascript

Page 29: CSS from the future

ReadgetComputedStyle(element).getPropertyValue('--color');

element.style.setProperty('--color', 'red');

Write

Page 30: CSS from the future

Support �

Page 31: CSS from the future

Browsers

49+ 31+ 15? 36+ 9.1+

Jan 2017: 68.34%

Page 32: CSS from the future

Fallback property

@supports (--foo: green) {

body {

color: var(--my-color);

}

}

Feature query

color: red;

color: var(--my-color, red);

Page 33: CSS from the future

Good news �

Page 34: CSS from the future

<body>

<a>Link 1</a>

<footer>

<a>Link 2</a>

<section>

<a>Link 3</a>

</section>

</footer>

</body>

We already use CSS variablesbody { font-size: 18px }

footer { font-size: 16px }

section { font-size: 14px }

a {

border-width: 0.2em;

padding-right: 2em;

}

Page 35: CSS from the future

<body>

<a>Link 1</a>

<footer>

<a>Link 2</a>

<section>

<a>Link 3</a>

</section>

</footer>

</body>

The superhero: currentColorbody { color: red }

footer { color: blue }

section { color: green }

a {

border-color: currentColor;

}

Page 36: CSS from the future

Use case:

Theming �

Page 37: CSS from the future

.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

Page 38: CSS from the future

.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

Page 39: CSS from the future

.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

Page 40: CSS from the future

Questions? �

Page 41: CSS from the future

Grazie! �@giacomozinetti


Recommended