Skip to content
Go back

CSS Variables

Published:  at  08:17 AM
1 min read

I’ve used variables in SASS and LESS before, but I always prefer the native approach.

Today I learned the basics of CSS Custom Properties, also known as Variables.

General

CSS by Peter Griffin

Usage

.example {
      --example-color: #ffff66;
}
#title {
      color: var(--example-color);
}
const example = document.getElementsByClassName('example');
const exampleStyles = getComputedStyle(example);
exampleStyles.getPropertyValue('--example-color');
-> #ffff66

example.style.setProperty('--example-color', '#3399ff')

Browser compatibility

CSS Variables browser compatibility table

taken from MDN web docs




Previous Post
ngrok Rocks!
Next Post
every() day some() time