DEV Community

Cover image for CSS variables but with a dash of JavaScript. ✨
Vaibhav Khulbe for daily.dev

Posted on • Originally published at daily.dev

CSS variables but with a dash of JavaScript. ✨

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 with 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:

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;
}
Enter fullscreen mode Exit fullscreen mode

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

element {
  background-color: var(--main-bg-color);
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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

➡️ removeProperty

element.style.removeProperty('--my-color');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
  .
  .
  .
}
Enter fullscreen mode Exit fullscreen mode

As you know by now, it translates into:

 div {
  .
  .
  .
  left: 10px;
  top: 10px;
  .
  .
  .
}
Enter fullscreen mode Exit fullscreen mode

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");
});
Enter fullscreen mode Exit fullscreen mode

We are simply using the clientX and clientY properties of the MouseEvent interface, whose function is to move the corresponding div in both X and Y directions, respectively.

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:

Thanks for reading. I appreciate it! Have a good day. (✿◕‿◕✿)

Team printf!#programmingmemes #DEVCommunity pic.twitter.com/Ou4sNRPRE7

— 🎵PhonoForest🌳GameDev🎮 (@PhonoForest) October 28, 2020

daily.dev delivers the best programming news every new tab. We will rank hundreds of qualified sources for you so that you can hack the future.
Daily Poster

Top comments (6)

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Ahem..

npmjs.com/package/reactive-css-pro...

🔌 Sorry couldn't resist.

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Oh wow, thanks for sharing! (might create a tutorial on this...)

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

That would be awesome, I haven't really been maintaining it lately, I should really get back to it.

Thread Thread
 
vaibhavkhulbe profile image
Vaibhav Khulbe • Edited

Oh, it's yours!! Great work. I would like to write about it then. Yes, please keep it maintained :)

  • didn't quite get to know about the meaning of that plug emoji (shameless plug haha)...
Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

I may do, it has a nasty bug of a memory leak which I have yet to address 🥁 pun intended. I can't remember if I got around to implimenting the Houdini custom properties (variables on steroids) and then there's the style guide.
I have some posts on re.css il try and dig the out. I'm also always looking for contributors 😅