Going beyond constants with custom properties

By Brecht De Ruyte

9 min read

If you love CSS, you’ve probably heard of custom properties (a.k.a. CSS variables) before. Still, a lot of people seem to use them as constants for their CSS. In this article, I will try to give you some more insights on how you can use these custom properties to create some smart systems or even use them as booleans in CSS and create easy progressive enhancements.

Going beyond constants with custom properties
Authors

Creating a smart button system

One of the best places to start is of course… BUTTONS! When we create buttons, we’re usually relying on mixins when using SASS or even some other tricks with CSS-in-JS. But what if I told you that custom properties can really create some clean code that’s easily maintainable as well?

Using mixins combined with only SASS variables will create quite a large output for our buttons. Say - for example - we want to style these 4 buttons:

4 buttons, a primary, secondary and outline variants

Each of these buttons have a hover state as well, you can see them on this codepen with the SASS output as CSS

By using some modern CSS we can easily create these same buttons by using custom Properties:

/* default buttons layer */
@layer buttons {
  button {
    --_color: var(--color, black);
    background: var(--_color);
    border: 2px solid var(--_color);
    color: white;
  }

  button.secondary {
    --color: deeppink;
    --hoverColor: purple;
  }
}

/* Non layered styles come last */
button.outline {
  background: transparent;
  color: var(--_color);
}

button:is(:hover, :focus) {
  --color: var(--hoverColor, DarkCyan);
}

So? What just happened here?

We started off by setting a “private” custom property for the color, which will inherit the --color custom property or fallback to black for our primary button. The secondary button will set the --color variable, so that the fallback isn't used. You also see that the secondary button sets another custom property which is the --hoverColor. This will be read by the last selector in the example which will again overwrite --color with the --hoverColor or fallback to our default button hover color which is DarkCyan.

Private custom property?

There really isn’t such a thing, it’s just a method of working and using an underscore is a way to show that we only use this custom property for this button. It’s a convention that was used when JS didn’t have const or let.

And why is this layered?

We could potentially run into some specificity problems by declaring a new button below our hover state. By declaring our buttons inside of a layer, we make sure that doesn’t happen as unlayered styles in CSS are read last.

If you want an in-depth explanation of this button system, I suggest you read about this smart button system on my blog.

This idea of private custom properties and the blueprint for this button system was also introduced to me by Lea Verou at CSS Day 2022. If you haven't seen it, it's an amazing talk where she also does a lot of fun stuff with "invalid at computed value time", which I'm not going to cover here (it's really cool, but a bit "hacky").

Creating a smart grid system

Besides buttons, we all love a good grid, right? So maybe we want to take a bit of responsive control for our gaps. Imagine the following example:

You have a little grid that is connected with some dots. On a certain breakpoint you might want to increase the gap of your grid an the connection should just grow:

Two column grid, with columns connected with a dotted line

When we’re making our grid, we could set our gap with a custom property and use that same custom property to position our dotted line between the columns. The benefit? You only need to write one media query (or container query, for the cool kids) to make everything work.

Here is the basic HTML for this:

<div class="wrapper">
  <div>There are fixed points throughout time where things must stay exactly the way they are.</div>
  <div>This is not one of them</div>
</div>

And the CSS:

.wrapper {
  --gap: 30px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--gap);
}

@media (min-width: 800px) {
  .wrapper {
    --gap: 100px;
  }
}
.wrapper div {
  position: relative;
}

.wrapper div:not(:last-child)::after {
  position: absolute;
  inset-block-start: 50%;
  inset-inline-end: calc(var(--gap) / -1);
  inline-size: var(--gap);
  block-size: 10px;
  border-block-end: 5px dotted black;
  transform: translateY(-50%);
  content: '';
}

By adding this scoped variable --gap we can now position our pseudo element by giving it a negative inset-inline-end by using the calc() function in CSS and adjust the size as well.

Think outside the box with this inheriting

Now that we have some understanding that these custom properties can easily be overwritten, we can use this for many things when it comes to positioning elements as well. If you use them as constants for your colors, they can be overwritten to create a dark theme for your project:

:root {
  --color-lightest: rgb(250, 250, 250);
  --color-magenta: rgb(143, 45, 86);
  --color-darkest: rgb(10, 10, 10);
  --color-background: var(--color-lightest);
  --color-text: var(--color-darkest);
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-magenta: rgb(72, 190, 222);
    --color-background: var(--color-darkest);
    --color-text: var(--color-lightest);
  }
}

In this example we just change the color of a background and the text based on a darkest and lightest color, which in themselves are custom properties as well.

Custom properties can contain anything you want

Yes, you read that right, anything you want! If you have a project that is working with a lot of transparent colors you could just add the number value of the rgb() function and add use your colors like this:

button {
  --background-btn: 239, 111, 108;
  background: rgb(var(--background-btn));
}
button.transparent {
  background: rgba(var(--background-btn), 0.5);
}

Although we probably won’t have the need to do this anymore for long with upcoming CSS features included in color-module-5 (for example color-mix()). Still, this could help our workflow for now.

How about using custom properties for progressive enhancement

These new HD colors in CSS are really beautiful, I absolutely adore them. And there is quite a bit of browser support for them, but unfortunately we’re still stuck for the moment in providing a fallback. Checkout gradient.style if you want to play around with them.

But there is more than just using the HD colors themselves. Even using a normal gradient with sRGB color can give a completely different effect depending on the color mode you are drawing it in. The effect being that your gradient will mostly look a bit smoother and more natural. We can easily use progressive enhancement with custom properties for these kind of gradients.

Remember, custom properties can contain anything you want. So why not use a space?

:root {
  --in-oklab: ;
}

@supports (background: linear-gradient(in oklab, red, tan)) {
  :root {
    --in-oklab: in oklab;
  }
}

.gradient {
  background: linear-gradient(var(--in-oklab) rgb(0, 0, 255), rgb(255, 255, 0));
}

We can set the :root custom property to have a space, which will just show some whitespace inside of our linear-gradient. If the browser supports oklab, we can change that custom property to oklab, (notice the comma, it’s important here) That way you have a silky smooth gradient in the browsers that support it and still have a pretty good one in the others.

Checkout this codepen over different browsers to see it in action (for now, Firefox has no support)

Can you tell which is the oklab gradient?

Gradient from blue to yellow

The holy trinity: Control custom properties with JavaScript

I love the web platform and when you can create something that makes HTML, CSS and JavaScript work together in harmony, it can be a beautiful thing. Let’s start with a basic example:

Moving spotlight with custom properties

Spotlight over text that reads custom properties

For this demo we’re going to make a little spotlight that follows your cursor, all you need is a <div>

<div id="mouse-follower"></div>

Next up we’re going to style this <div> and give it some basic styling, as well as setting it up to follow our cursor later on:

#mouse-follower {
  position: fixed;
  width: 15vw;
  height: 15vw;
  background: radial-gradient(closest-side, white 10%, transparent);
  background-repeat: no-repeat;
  transform: translate(var(--xAxis, 0), var(--yAxis, 0));
  transition: transform 0.2s ease-out;
}

The main take-away from this is the transform property, we’re setting it for both the axis with a custom property and let them fallback to 0.

Next up, we’ll use a mousemove event in Javascript and do a little calculation to set these custom properties to make the spotlight appear in the center of our cursor:

const spotlight = document.getElementById('mouse-follower')
document.addEventListener('mousemove', (event) => {
  const xAxis = event.clientX - spotlight.clientWidth / 2,
    yAxis = event.clientY - spotlight.clientHeight / 2

  spotlight.style.setProperty('--xAxis', `${xAxis}px`)
  spotlight.style.setProperty('--yAxis', `${yAxis}px`)
})

The formula to calculate the axis is pretty easy: we just use the axis from our event and subtract half the width of our spotlight itself.

Using :has() and container style queries to create booleans

Especially with the new and upcoming features such as container queries and :has(), using custom properties really seems to click. I’m currently working on a lot of demo’s that combine these exciting features and wanted to share one of them.

Select with below a list that contains fruit and vegetable names

Let’s say we have the following select and list in our HTML:

<select class="select-view">
  <option value="">Both</option>
  <option value="veggie">Veggies</option>
  <option value="fruit">Fruits</option>
</select>

<ul class="list" role="list">
  <li class="fruit">Banana</li>
  <li class="veggie">Broccoli</li>
  <li class="fruit">Apple</li>
  <li class="fruit">Orange</li>
  <li class="veggie">Beets</li>
  <li class="fruit">Grapes</li>
  <li class="veggie">Carrots</li>
</ul>

When we change our select, we only want to display the items with the same class of the selected option in our select. This is by all means not the best practice to do this, but it’s nice to see something like this is possible with CSS:

:root {
  --veggies: true;
  --fruits: true;
}

:root:has(.select-view option:checked[value='veggie']) {
  --fruits: false;
}

@container style(--fruits: false) {
  .fruit {
    display: none;
  }
}

:root:has(.select-view option:checked[value='fruit']) {
  --veggies: false;
}

@container style(--veggies: false) {
  .veggie {
    display: none;
  }
}

What happens here is that we check on the :root if the current option in the select has been checked by using :has(). Following up with style queries, we can do a little check for the current value of a custom property, we’re using this trick to hide the items set to false.

We’re actually just using CSS custom properties as booleans here, pretty sweet.

For the record, I know that you could probably do this by just using :has(), But by showing it to you this way, you also get a feel of the power that container queries have to offer. This way, you could easily enhance this demo by adding more boolean dependent items such as showing a carrot or apple based on the container query, the choice is yours

But remember, this might not be the best practice, as doing a lot of heavy calculations with :has() on a :root level could affect your overall performance. So, it's ok to use it, as long as you don't overuse it.

I created a theme selector as well that uses the same technique, but might be a bit more complicated if you want to check it out. Who knows, I might write a full explainer about that one later.

What about @property?

This is probably on top of my list of "features I really wish were cross browser" (poor Firefox..). In short, it gives custom properties some superpowers. I’m not going to get into it but still wanted to show off what you can do with them by sharing a demo I created for the first iO CodePen challenge, by using @property to animate the hue of an hsl() color and rotating a conic gradient.

But don’t forget to look at some of the other pens popping up

Conclusion

Should there be a conclusion? Really? Custom properties are just awesome, they can help you to create a better, clever design system, help you in creating some of those progressive enhancements and really shorten the size of your CSS file in general, which is always a good thing. With all the new features coming to CSS from container queries to scroll-driven-animations, they will really be helpful in keeping our code clean, expandable and maintainable. So if you haven’t taken a deepdive, I can only suggest you try to do so and you will notice a whole new world of CSS opening before you.


Upcoming events

  • Mastering Event-Driven Design

    PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Are you and your team struggling with event-driven microservices? Join us for a meetup with Mehmet Akif Tütüncü, a senior software engineer, who has given multiple great talks so far and Allard Buijze founder of CTO and founder of AxonIQ, who built the fundaments of the Axon Framework. RSVP for an evening of learning, delicious food, and the fusion of creativity and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 Getting Your Axe On Event Sourcing with Axon Framework 20:00 – 🍹 Small break 20:15 – 📢 Event-Driven Microservices - Beyond the Fairy Tale 21:00 – 🙋‍♀️ drinks 22:00 – 🍻 See you next time? Details: Getting Your Axe On - Event Sourcing with Axon Framework In this presentation, we will explore the basics of event-driven architecture using Axon Framework. We'll start by explaining key concepts such as Event Sourcing and Command Query Responsibility Segregation (CQRS), and how they can improve the scalability and maintainability of modern applications. You will learn what Axon Framework is, how it simplifies implementing these patterns, and see hands-on examples of setting up a project with Axon Framework and Spring Boot. Whether you are new to these concepts or looking to understand them more, this session will provide practical insights and tools to help you build resilient and efficient applications. Event-Driven Microservices - Beyond the Fairy Tale Our applications need to be faster, better, bigger, smarter, and more enjoyable to meet our demanding end-users needs. In recent years, the way we build, run, and operate our software has changed significantly. We use scalable platforms to deploy and manage our applications. Instead of big monolithic deployment applications, we now deploy small, functionally consistent components as microservices. Problem. Solved. Right? Unfortunately, for most of us, microservices, and especially their event-driven variants, do not deliver on the beautiful, fairy-tale-like promises that surround them.In this session, Allard will share a different take on microservices. We will see that not much has changed in how we build software, which is why so many “microservices projects” fail nowadays. What lessons can we learn from concepts like DDD, CQRS, and Event Sourcing to help manage the complexity of our systems? He will also show how message-driven communication allows us to focus on finding the boundaries of functionally cohesive components, which we can evolve into microservices should the need arise.

    | Coven of Wisdom - Utrecht

    Go to page for Mastering Event-Driven Design
  • The Leadership Meetup

    PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED What distinguishes a software developer from a software team lead? As a team leader, you are responsible for people, their performance, and motivation. Your output is the output of your team. Whether you are a front-end or back-end developer, or any other discipline that wants to grow into the role of a tech lead, RSVP for an evening of learning, delicious food, and the fusion of leadership and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 First round of Talks 19:45 – 🍹 Small break 20:00 – 📢 Second round of Talks 20:45 – 🙋‍♀️ drinks 21:00 – 🍻 See you next time? First Round of Talks: Pixel Perfect and Perfectly Insane: About That Time My Brain Just Switched Off Remy Parzinski, Design System Lead at Logius Learn from Remy how you can care for yourself because we all need to. Second Round of Talks: Becoming a LeadDev at your client; How to Fail at Large (or How to Do Slightly Better) Arno Koehler Engineering Manager @ iO What are the things that will help you become a lead engineer? Building Team Culture (Tales of trust and positivity) Michel Blankenstein Engineering Manager @ iO & Head of Technology @ Zorggenoot How do you create a culture at your company or team? RSVP now to secure your spot, and let's explore the fascinating world of design systems together!

    | Coven of Wisdom - Amsterdam

    Go to page for The Leadership Meetup
  • Coven of Wisdom - Herentals - Spring `24 edition

    Join us for an exciting web technology meetup where you’ll get a chance to gain valuable insights and knowledge about the latest trends in the field. Don’t miss out on this opportunity to expand your knowledge, network with fellow developers, and discover new and exciting possibilities. And the best part? Food and drinks are on us! Johan Vervloet - Event sourced wiezen; an introduction to Event Sourcing and CQRS Join me on a journey into the world of CQRS and Event Sourcing! Together we will unravel the misteries behind these powerful concepts, by exploring a real-life application: a score app for the 'Wiezen' card game.Using examples straight from the card table, we will delve into the depths of event sourcing and CQRS, comparing them to more traditional approaches that rely on an ORM.We will uncover the signs in your own database that indicate where event sourcing can bring added value. I will also provide you with some tips and pointers, should you decide to embark on your own event sourcing adventure. Filip Van Reeth - WordPress API; "Are you talking to me?" What if the WordPress API could be one of your best friends? What kind of light-hearted or profound requests would it share with you? In this talk, I would like to introduce you to it and ensure that you become best friends so that together you can have many more pleasant conversations (calls). Wanna be friends? Please note that the event or talks will be conducted in Dutch. Want to give a talk? Send us your proposal at meetup.herentals@iodigital.com 18:00 - 19:00: Food/Drinks/Networking 19:00 - 21:00: Talks 21:00 - 22:00: Networking Thursday 30th of May, 18h00 - 22h00 CET iO Campus Herentals, Zavelheide 15, Herentals

    | Coven of Wisdom Herentals

    Go to page for Coven of Wisdom - Herentals - Spring `24 edition

Share