close icon
daily.dev platform

Discover more from daily.dev

Personalized news feed, dev communities and search, much better than what’s out there. Maybe ;)

Start reading - Free forever
Start reading - Free forever
Continue reading >

CSS variables but with a dash of JavaScript ✨

CSS variables but with a dash of JavaScript ✨
Author
Vaibhav Khulbe
Related tags on daily.dev
toc
Table of contents
arrow-down

🎯

Okay, we'll go with this quick and straight. When you first start diving into CSS, you do the usual thing like changing color, changing fonts, etc. Then you dive deep into media queries, cross-browser properties, and finally to variables.

But what if we use these variables in our CSS files alongside some magic of our old friend JavaScript, then imagine what we can achieve! Something cool? Yes, you're correct. Let's do this and make something nice.

Sprinkle GIF
Something to sprinkle!

Quick note on CSS variables 📝

Of course, some basics first.

CSS Custom Properties or CSS Variables allows us to store a value stored in one place, then referenced in multiple other places.

Sometimes specific values need to be reused throughout a document. A typical example is when you get a specific color palette from designers, and you need to add specific hex values of colors, font-sizes, or even some responsive breakpoints. You assign these values to your custom-made CSS properties called variables here.

This is useful not only because they can be used at multiple instances and makes editing the values easy but also, it makes the properties easier to read when referring to it later. For example: --headline-color is better to read than #000.

Check out this wonderful CodePen example:

See the Pen CSS Variables by James Steinbach (@jdsteinbach) on CodePen.

Usage and syntax

Declaring a custom CSS property is created by assigning the double hyphen (--) in front of the variable name, and then the property value is written like any other CSS property.

Check out this example:


element {
  --main-bg-color: lightgray;
}

Now, to use this custom property anywhere in your CSS file you can do this:


element {
  background-color: var(--main-bg-color);
}

So, you don't need to write lightgray value for the background-color in all places where there is a need to use the var() function and pass in the CSS custom variable inside.

Introducing JavaScript ✨

Time to start interacting with the web developer's favorite language.

Let's see how we can write a new property value in JavaScript.

➡️ setProperty


var element = document.documentElement;
element.style.setProperty('--name', value);

So, what these new functions mean?

The setProperty() takes in the property name, it's value, and optionally the priority.

The code you saw above sets a new value for a globally defined property. This type of JavaScript code is useful when you are managing state and then modifying CSS styles based on given values.

➡️ getPropertyValue


element.style.getPropertyValue('--my-color');

Yeah, exactly what you're thinking right now. Just as with any other language, we have setters and getters here too. With the setProperty we were setting a new value, and here with getPropertyValue, we return a DOMString containing the value of the above-specified CSS property.

Here's a practical example:


element.style.setProperty('--accent-color', '#663399');
element.style.getPropertyValue('--accent-color');

This will return the value of --accent-color as #663399 when the browser renders the webpage.

➡️ removeProperty


element.style.removeProperty('--my-color');

The removeProperty method will remove the provided property from a CSS style declaration object.

So, if you want to dynamically remove the attached custom CSS property, then you can have code similar to this:


element.style.setProperty('--accent-color', '#663399');
element.style.getPropertyValue('--accent-color');

element.style.removeProperty('--accent-color');

Using event listeners 👂

If there's some JavaScript code, then there are events happening! What if you want to change a div's position on cursor click?

First, start off with declaring the CSS variables:


:root {
  --cursor-pos-x: 10px;
  --cursor-pos-y: 10px;
}

By declaring them at :root, we're putting them into the root element of the DOM tree. Typically, it's the <html> element.

Next, we will be using these variables in our <div> as follows:


div {
  .
  .
  .
  left: var(--cursor-pos-x);
  top: var(--cursor-pos-x;
  .
  .
  .
}

As you know by now, it translates into:


div {
  .
  .
  .
  left: 10px;
  top: 10px;
  .
  .
  .
}

We have set the initial position of the <div>, now let's interact with an event listener.


let root = document.documentElement;

root.addEventListener('click', event => {
  root.style.setProperty('--cursor-pos-x', event.clientX + "px");
  root.style.setProperty('--cursor-pos-y', event.clientY + "px");
});

The result will be similar to this:

Mouse click demo

That is it! There are endless possibilities when it comes to combining the power of CSS variables with JavaScript. You can make awesome DOM-based games, use it to change component styles dynamically, and more!

More resources 🤩

Go ahead and learn more about custom CSS properties from the resources given below:

Why not level up your reading with

Stay up-to-date with the latest developer news every time you open a new tab.

Read more