The Power and Flexibility of
CSS Variables
@guilh
CSS Variables @guilh https://sass-lang.com/guide - - PowerPoint PPT Presentation
The Power and Flexibility of CSS Variables @guilh https://sass-lang.com/guide http://lesscss.org/features/#variables-feature https://caniuse.com/#feat=css-variables CSS variables hold references to values you can reuse --property-name:
The Power and Flexibility of
@guilh
var(--property-name)
:root {
}
:root {
} .headline { color: var(--color-secondary); } .btn { background-color: var(--color-primary); }
:root {
} header { background-image: linear-gradient( var(--color-bg-light), var(--color-bg) ); }
:root {
var(--color-bg); } header { background-image: linear-gradient( var(--gradient) ); }
:root { /* Fonts */
/* Layout */
}
Preprocessor Variables
CSS Variables
:root {
} .headline { margin-bottom: var(--margin-size); } .col + .col { margin-left: var(--margin-size); }
@media (min-width: 576px) { :root {
} } @media (min-width: 768px) { :root {
} }
:root { /* Colors */
/* Fonts */
/* Layout */
}
.btn { ... background-color: var(--button-bg); } .btn.callout {
} .btn.info {
}
<a class="btn callout" href="#">Callout Button</a> <a class="btn info" href="#">Info Button</a>
.btn { ... background-color: var(--button-bg); } .btn.callout {
} .btn.info {
}
.card .btn {...} .modal > .btn {...} .banner .btn {...}
.btn { font-size: var(--btn-font-size); background-color: var(--btn-bg); ... }
/* .card .btn */ .card {
} /* .modal > .btn */ .modal {
} <div class="card"> <a class="btn" href="#">More</a> </div> <div class="modal"> <a class="btn" href="#">Start</a> </div>
×
Modal Scope Card Scope
:root {
} .img-featured { margin-bottom: calc(var(--margin-size) * 10px); /* 20px */ } .headline { margin-bottom: calc(var(--margin-size) * 1em); /* 2em */ }
getPropertyValue() setProperty()
.ball { background-color: var(--ball-bg); transform: translate( calc( var(--pos-x) * 1px), calc( var(--pos-y) * 1px) ); }
// Select element const ball = document.querySelector('.ball'); // Update CSS custom property values body.addEventListener('click', e => { ball.style.setProperty( '--pos-x', e.clientX ); ball.style.setProperty( '--pos-y', e.clientY ); ball.style.setProperty( '--ball-bg', randomHex() ); });
// Select element const ball = document.querySelector('.ball'); // Update CSS custom property values body.addEventListener('click', e => { ball.style.setProperty( '--pos-x', e.clientX ); ball.style.setProperty( '--pos-y', e.clientY ); ball.style.setProperty( '--ball-bg', randomHex() ); });
@guilh
teamtreehouse.com