1. Home
  2. Articles
  3. best practices

Using CSS Custom Properties with Fallbacks for Efficiency

Best Practices||

4 min read

By now, most of us are accustomed to using CSS Custom Properties on projects. Since its introduction in 2017, CSS Custom Properties, also known as CSS variables, have optimised the code we work with and allowed engineers a finer degree of control when managing values within rules.

CSS Custom Properties can be tricky to deal with and aren’t always as intuitive as one might hope. However, when used right, they can significantly reduce boilerplate code and keep things efficient, making our work more productive.

While the approach below is not perfect, it works well when writing CSS for building projects.

Why Use CSS Custom Properties

As far as my experience goes, I can only think of three possible reasons to implement a CSS Variable:

  • We want to use a given value in more than one place,
  • We know that a value will change in the future and want to reduce our efforts for refactoring,
  • We need to store dynamic values from JS execution.

With this in mind, we can be more assertive about our use cases. Point 3 is less of a concern for this article, but points 1 and 2 are essential. Let's illustrate this using the familiar HTML button element.

The Infamous Button

Buttons are notoriously tricky to style consistently for such a simple HTML element. Perhaps it’s because they have several different states and applications, regardless - it's usually the first component we Frontend Engineers take on when it comes to a fresh build. Let's have a look at how we could write the style for a button using CSS Custom Properties:

1:root {
2 --color-primary: purple;
3 --color-text: black;
4 --color-text-inverted: white;
5 --color-border: gray;
8.button {
9 --button-bg: var(--color-primary);
10 --button-color: var(--color-text-inverted);
11 --button-border: var(--color-border);
13 align-items: center;
14 background-color: var(--button-bg);
15 border: 1px solid var(--button-border);
16 color: var(--button-color);
17 display: inline-flex;
18 line-height: 1;

As far as this example goes, from a technical standpoint, it’s great. It’s clean and straightforward and would get a 👍 from me in a code review. But could we optimise it further? Well, yes.

The var() function in CSS is pretty nifty…we’re used to using it in a somewhat one-dimensional way by just rendering out a value; however, it can be more dynamic. The way to do this is by using a fallback value.

If we considered the above example, we could rewrite it like so:

1:root {
2 --color-primary: purple;
3 --color-text: black;
4 --color-text-inverted: white;
5 --color-border: gray;
8.button {
9 align-items: center;
10 background-color: var(--button-bg, var(--color-primary));
11 border: 2px solid var(--button-border, var(--color-border));
12 color: var(--button-color, var(--color-text-inverted));
13 cursor: pointer;
14 display: inline-flex;
15 line-height: 1;
16 padding-block: 0.875rem;
17 padding-inline: 1rem;

This is far cleaner and will lead to the same outcomes as the previous example - with less boilerplate code. Overriding it in a variation makes each locally scoped CSS Variable (effectively) optional. We can easily now optionally define the variables defined in .button as separate variations:

1.button:where(.button-secondary) {
2 --button-bg: orange;
3 --button-border: teal;
6.button:where(.button-tertiary) {
7 --button-bg: teal;
8 --button-border: orange;

Although this technique is not revolutionary, we care how many lines of compressed text are heading over the wire. The less physical memory a CSS file takes up, the better, which ultimately translates to how many lines of CSS exist. In the first example, we’re looking at almost double the amount of code to achieve the same thing in our optimised example.

This compound effect has an influential impact on performance and further improves readability and developer experience. If you'd like to play around a bit further, feel free to fork the following Code Pen: https://codepen.io/iamdainemawer/pen/NWmQZMz


Since April 2017, according to Google Baseline, this feature is well-supported across all major browsers. If you're interested to learn more about Baseline, read my post about Google Baseline and Its Impact

You can check for browser support by adding the following statement to your CSS stylesheets:

1@supports(--css: variable) {}


Written by Daine Mawer. Enjoy reading the article? Im always posting new content. If you liked what you read, please subscribe to my RSS feed or follow me on Github, Twitter or LinkedIn. Im also always on the look out for new oppurtunities, engagements, contract work or just coffee! So please dont hesitate to reach out.