The easiest way to get and set CSS Variables in JavaScript
CSS Variables have become super popular for many reasons. One of them these reasons is — you can now manipulate CSS easily with JavaScript.
In this article, we’ll go through how to get and set CSS variables using the methods Vanilla JavaScript provides us with, then we’ll go through how to make the process easier with simple helper functions.
Let’s start from the beginning.
Using CSS Variables
CSS Variables can be written on two kinds of selectors:
- The
:rootselector - Any other CSS selector
:root { --variable: #111;}
.element { --variable: #222;}This will be an important distinction later because how you access the CSS variables depends on whether you use it on the :root selector or other selectors.
Getting CSS Variables with JavaScript
You can use getComputedStyle and getPropertyValue to get the CSS Variable in JavaScript. Here’s the syntax:
getComputedStyle(element).getPropertyValue('--variable')element must be replaced by the HTML Element that contains the CSS Variable.
- If you wrote the variable in
:root, you can usedocument.documentElementordocument.body - If you wrote the variable in another selector, you need to use
document.querySelectorto find the element first.
// Getting a CSS variable from :rootconst value = getComputedStyle(document.body).getPropertyValue('--variable') // #111// Getting a CSS variable from an HTML element with the `.element` classconst element = document.querySelector('.element')const value = getComputedStyle(element).getPropertyValue('--variable')console.log(value) // #222Setting CSS Variables with JavaScript
You can set CSS Variables with the following syntax:
element.style.setProperty(property, value)elementmust be the HTML element you’re trying to set the CSS variable to.propertyis the CSS variable you want to set. You must include the double dashes (--) that signify CSS variables here.valueis the value you want to set.
The same thing applies as before:
- If you want to set CSS variables on the
:rootelement, you can usedocument.bodyordocument.documentElementaselement. - If you want to set CSS variables on any other element, you must get that element first.
// Setting CSS Variables on :rootdocument.body.style.setProperty('--variable', 'blue')// Setting CSS Variables on an HTML Element with the `.element` classconst element = document.querySelector('.element')element.style.setProperty('--variable', 'blue')Helper functions that make it easier to get and set CSS variables
The above processes are simple, but it can be tedious remember the correct way to get and set CSS variables.
To make the process easier for me, I created two helper functions to help me out:
You can install these two helpers by installing Splendid UI, then import them from the utils subpath.
npm install splendidlabz --saveimport { getCSSVar, setCSSVar } from 'splendidlabz/svelte'Let’s go through how to use each of these helper functions.
get css var
getCSSVar helps you get a CSS variable from an element.
const value = getCSSVar(element, property)elementis the element you want to get the CSS property from.propertyis the name of the CSS variable.
getCSSVar makes things a little easier if you have to get a CSS Variable from an element — you can just pass in the selector and it will do a querySelector for you.
// Getting CSS variable from an HTML element with the `element` class.const value = getCSSVar('.element', '--variable')console.log(value) // #222You can still pass in an HTML element and getCSSVar will respect that element you passed in.
// This works tooconst element = document.querySelector('.element')const value = getCSSVar(element, '--variable')console.log(value) // #222If the CSS variable is on the :root selector, you can just use document.documentElement or document.body as the element.
// Getting CSS variable from the :root selectorconst value = getCSSVar(document.body, '--variable')console.log(value) // #111set css var
setCSSVar lets you set a CSS variable on an element. The syntax is as follows:
setCSSVar(element, property, value)elementis the element you want to set the CSS property on.propertyis the name of the CSS variable.valueis the value you want to set.
The same rules for element in getCSSVar apply for setCSSVar.
You can pass in a selector into setCSSVar and it will find the element for you.
// Setting CSS variable on an element with the `.element` classsetCSSVar('.element', '--variable')setCSSVar will respect the HTML element you pass into element too.
// This works tooconst element = document.querySelector('.element')setCSSVar(element, '--variable', 'blue')If the CSS variable is on the :root selector, you can just use document.documentElement or document.body as the element.
// Setting CSS variable on the :root selectorsetCSSVar(document.body, '--variable')That’s it!
Wrapping Up
CSS variables are powerful but working with CSS variables in JavaScript can become tedious — especially if you need to remember how different it is to get and set CSS Variables.
To make the process easier, I created getCSSVar and setCSSVar helper functions in Splendid UI.
Take them out for a spin and let me know what you think!
If you enjoyed this article, you might also enjoy the courses I’ve written to help people become better full-stack developers. You can get 2-3 chapters of each of my courses for free here. (No registration necessary). Hope you enjoy these free lessons!