CSS Variables Explained in 2 Minutes with an Interactive Demo

<!-- **This post is originally from [xtrp.io](https://xtrp.io/), a blog about CSS, JavaScript, and just about anything programming.** -->

Go check out Daily Developer Jokes, my latest project!

Daily Developer Jokes Website Follow Daily Developer Jokes on DEV

Here's the joke from today:

Daily Developer Jokes, Friday Jan. 27, 2020

CSS variables have been around for quite some time now, but are nevertheless extremely useful.

They can be defined in an element like this:

1element { 2 --my-variable: #555; 3} 4

The value of a variable can be just about anything, from pixel values to colors to linear-gradients.

You can use a variable almost anywhere by wrapping it in a var function:

1another-element { 2 color: var(--my-variable); 3} 4

Why Not Use a Preprocessor for Variables?

This is a very common question, as preprocessors seem to have solved the CSS variable problem even before native CSS variables existed!

However, CSS variables can be changed in real-time and aren't compiled, which means that it is possible to change the value of a CSS variable within JavaScript, and all references to that variable will be automatically changed. The example coming up uses this ability pretty well, and uses this ability of CSS variables to its advantage.

A Demo of CSS Variables in Action

One use CSS variables is the ability to use a singular variable for a site's primary color, and then allow the user to interactively change that primary color as they wish.

Here's a fun little CodePen that shows just that!

<p class="codepen" data-height="501" data-theme-id="default" data-default-tab="html,result" data-user="xtrp" data-slug-hash="ZEYZKOw" style="height: 501px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="CSS Variables Demo"> <span>See the Pen <a href="https://codepen.io/xtrp/pen/ZEYZKOw"> CSS Variables Demo</a> by Gabriel Romualdo (<a href="https://codepen.io/xtrp">@xtrp</a>) on <a href="https://codepen.io">CodePen</a>.</span> </p> <script async src="https://static.codepen.io/assets/embed/ei.js"></script> <p style="margin-top: 2.5rem;">Here, I use some basic JavaScript code to change the CSS variable, and thus changing the theme color of the entire page.</p>

Other Use Cases of CSS Variables

The example above is one of the many example use cases of CSS variables. Other possible uses include:

  • Creating a CSS variable for a default font size of an element and dynamically changing that for disabled users
  • Creating a CSS variable for the height and width of an element and dynamically changing that variable in a CSS animation
  • Creating CSS variables for each of the colors on your site an dynamically changing those variables to create a "dark mode" or different theme

Browser Support & Conclusion

CSS variables are supported in all major browsers except IE, QQ, and Baidu: CanIUse Data for CSS Variables.

I hope you enjoyed this explanation and demo of CSS variables and find them to be useful in your sites. I personally use CSS variables for colors and font sizes on the several of my sites, and have found them to help keep my styles much cleaner and easier to read and refactor.

Thanks for scrolling.

<!-- **This post is originally from [xtrp.io](https://xtrp.io/), a blog about CSS, JavaScript, and just about anything programming.** -->

— Gabriel Romualdo, January 27, 2020