Aleksandra Hristov
10 July 2018, Skopje @ Front-end meetup
//SCSS
$text-color: #565656;
.component {
color: $text-color;
}
/* LESS */
@text-color: #565656;
.component {
color: @text-color;
}
/* CSS */
:root {
/* :root is on the level of the html tag, with more specificity */
--text-color: #565656;
}
.component {
color: var(--text-color);
}
/* CSS */
:root {
--text-color: #565656;
}
.component {
color: var(--text-color, red);
/* red is fallback if --text-color is not defined */
}
/* CSS */
:root {
--text-color: #565656;
}
.component {
color: red;
color: var(--text-color, var(--text-color-2, red));
/* red is fallback if --text-color and --text-color-2 are not defined */
}
:root{
--main-color: #4d4e53;
--main-bg: rgb(255, 255, 255);
--logo-border-color: rebeccapurple;
--header-height: 68px;
--content-padding: 10px 20px;
--base-line-height: 1.428571429;
--transition-duration: .35s;
--external-link: "external link";
--margin-top: calc(2vh + 20px);
/* Valid CSS custom properties can be reused later in, say, JavaScript. */
--foo: if(x > 5) this.width = 10;
}
:root{
--indent-size: 10px;
--indent-xl: calc(2*var(--indent-size));
--indent-l: calc(var(--indent-size) + 2px);
--indent-s: calc(var(--indent-size) - 2px);
--indent-xs: calc(var(--indent-size)/2);
}
:root{
--spacer: 10;
}
.box{
padding: var(--spacer)px 0; /* DOESN'T work */
padding: calc(var(--spacer)*1px) 0; /* WORKS */
}
//Compiled
.component {
color: red;
}
//SCSS
$text-color: red;
.component {
color: $text-color;
}
$text-color: blue;
//Compiled
.component {
color: blue;
}
.component {
color: red;
}
//SCSS
$text-color: red;
.component {
color: $text-color;
}
$text-color: blue;
.component {
color: $text-color;
}
.component {
color: var(--text-color); //blue
}
:root {
--text-color: blue;
}
:root {
--text-color: red;
}
/* CSS */
:root {
--text-color: red;
}
.component {
color: var(--text-color);
}
:root {
--text-color: blue;
}
.component {
color: var(--text-color); //blue
--text-color: blue;
}
:root {
--text-color: red;
}
/* CSS */
:root {
--text-color: red;
}
.component {
color: var(--text-color);
--text-color: blue;
}
.component span {
--text-color: blue;
}
.component {
color: var(--text-color);
}
:root {
--text-color: red;
}
/* SCSS */
:root {
--text-color: red;
}
.component {
color: var(--text-color);
span {
--text-color: blue;
color: var(--text-color);
}
}
.component span {
--text-color: blue;
color: var(--text-color);
}
.component {
color: var(--text-color);
}
:root {
--text-color: red;
}
/* CSS */
* {
--text-color: initial;
/* remove inheritance */
}
.component {
color: var(--text-color);
span {
--text-color: inherit;
/* explicitly inherit from parent */
}
}
//Compiled for screens > 900px
.component {
color: var(--text-color); //red
}
/* CSS */
:root {
--text-color: red;
}
.component {
color: var(--text-color);
}
@media (max-width: 900px) {
:root {
--text-color: blue;
}
}
//Compiled for screens > 900px
.component {
color: var(--text-color); //red
}
//Compiled for screens <= 900px
.component {
color: var(--text-color); //blue
}
//SCSS mixin to include
//https://github.com/malyw/css-vars/blob/master/css-vars.scss
$color-basic: #ff0000; //red
@include css-vars((
--text-color: $color-basic;
));
.component {
color: var(--text-color);
}
or check out https://codepen.io/vank0/pen/kkGvAb
var element = document.documentElement;
// get variable from inline style
element.style.getPropertyValue("--text-color");
// get variable from wherever
getComputedStyle(element).getPropertyValue("--text-color");
// set variable on inline style
element.style.setProperty("--var-value", jsVar + 10);
Example usage:
63
70
18
12
Custom
properties
31 (2014)
49 (2016)
15 (04.2017)
9.1
(2016)
@supports
22
(2013)
28
(2013)
12 (2015)
9
(2015)
Grid
52
(03.2017)
57
(03.2017)
16 (10.2017)
10.1
(03.2017)
IE 10+ (2012)
Old spec w/ -ms-
current versions 12.2018
.selector {
/* Styles that are supported in old browsers */
}
@supports (property:value) {
.selector {
/* Styles for browsers that support the specified property */
}
}
.selector { }
@supports ( (--a: 0)) {
/* Styles that are supported in old browsers */
.selector { }
}
@supports ( not (--a: 0)) {
/* Styles for browsers that support the specified property */
.selector { }
}