Sign in

A code-efficient CSS pattern for your theming and coloring needs, inspired by OOP and CSS Variables

Let’s talk about doing more with less code.

Do you struggle to write CSS classes to color and theme your components in a way that is easy to maintain and code-efficient ? That does not end up in hundreds of CSS classes ? Do your CSS libraries fail in simplicity, being lightweight, and code-efficient because of the hassle of managing all these colors ?

TLDR;

We propose a lightweight library called Swatch ( https://swatch.dev ) that offer CSS classes and variables which expose this pattern in an efficient API. Swatch is pure-CSS, which means framework-agnostic. Also its source SCSS is easily customizable.

The problem : theming and coloring imply exponential CSS classes

What do I mean by code-efficient?

Probably, you have as many web devs used Bootstrap / Semantic / Tailwind / Material UI to build your UI. While we don’t criticize the fact these libraries are good to get started quickly, they soon become extremely cumbersome to use and maintain due to (1) the particular syntax of one CSS library (2) the huge amount of classes you need to know and write shall you want to extend a bit the core library features. In particular, for coloring, Bootstrap / Semantic / Tailwind have one class per color per component (.btn-red, .btn-green, .btn-orange, .btn-blue) … what if we could reduce it to just one class with a better pattern ?

Using these libraries, if you have 15 colors and want your buttons (.button), your cards (.cards) , and your labels (.labels) to be compatible with all of them, you would have

  • 15 Classes for the button (btn-red, btn-green) …
  • 15 classes for the cards (card-red, card-green) …
  • 15 classes for the labels

This 45 classes in total, and each of the classes has very little value expect passing a particular color to the element. (All the colors are consumed in the same way by the component).

And imagine how many classes we would have if we had 10 components or a complete 100-component library. This amount to an exponential way of coding CSS classes for coloring and theming… for each component you create, you need to create X classes of colors to it. (Of course, we would use a preprocessor SCSS/LESS mixin, but I am talking about the compiled CSS output and what gets executed in the browser). What if we could reduce it to just one CSS class compatible with all the colors simply by using a better pattern ?

That’s where our pattern and the Swatch library comes in.

Using the Setter/Getter pattern to achieve code-efficiency in theming and coloring

We adapted a pattern that comes from Object-Oriented Programming, the Setter/Getter Pattern to CSS. If you’re unfamiliar with it, this article should give you an idea what the pattern is in Python.

The Setter/Getter pattern applied to CSS amounts to a three-step process. This is how it works.

1. We define CSS color variables at the:root level

:root {
--red: #f8333c;
--light-red: #f48489;
--dark-red: #df000a;
--on-red: #ffffff;
--orange: #f46036;
--light-orange: #f19d85;
--dark-orange: #dd3100;
--on-orange: #ffffff;
--yellow: #ffbf00;
--light-yellow: #f6ce55;
--dark-yellow: #b38600;
--on-yellow: #ffffff;
--olive: grey;
--light-olive: #a6a6a6;
--dark-olive: #635151;
--on-olive: #ffffff;
--green: #44af69;
--light-green: #7fc096;
--dark-green: #268045;
--on-green: #ffffff;
// .... And many other swatches
}

2. We use special CSS classes, called Setters, that take one of the root variables (for instance, — red) and translate it to a local variable (for instance — x).

.x-azure,
.xh-azure:focus,
.xh-azure:hover {
--x: var(--azure);
--light-x: var(--light-azure);
--dark-x: var(--dark-azure);
--on-x: var(--on-azure);
}
.x-blue,
.xh-blue:focus,
.xh-blue:hover {
--x: var(--blue);
--light-x: var(--light-blue);
--dark-x: var(--dark-blue);
--on-x: var(--on-blue);
}
.x-pink,
.xh-pink:focus,
.xh-pink:hover {
--x: var(--pink);
--light-x: var(--light-pink);
--dark-x: var(--dark-pink);
--on-x: var(--on-pink);
}

3. We “get” the local variables and apply it to our HTML

.b-x {
background: var( --x);
}
.b-dark-x {
background: var( --dark-x):
}
.b-light-x {
background: var( --light-x);
}
.b-on-x {
background: var( --on-x);
}
.c-x {
color: var( --x);
}
.c-dark-x {
color: var( --dark-x);
}
.c-light-x {
color: var( --light-x);
}
.c-on-x {
color: var( --on-x);
}

Finally, your html will look like.

<div>
<div class='x-red xh-blue b-x'> This div has red background and blue on hover</div>
<div class='x-red b-x c-on-x'> This div has red background and contrasted text </div>
<div class='x-red b-light-x c-on-x'> This div has light red background and contrasted text </div>
<div class='x-red b-dark-x c-on-x'> This div has dark red background and contrasted text </div>
</div>

You can at the complete example and a live playground here : https://swatch.dev/docs/examples

In the end, what do we have ?

  • Root CSS variables, defined once and for all
  • One setter class per color, defined once and for all
  • One getter class per “use case”, this can also be understood as one class per component (example here).

You can implement this pattern yourself of simply use our llibrary, Swatch ( https://swatch.dev ) which will provide you with these three levels: you will get around 20 different colors with 4 shades each, you will get setter classes and getter classes.

The whole library is 15KB, and it boils down to 2.5KB gzipped. The best thing about it (in our opinion), is that this library is (S)CSS only. Subsequently it is completely framework-agnostic meaning you can use it with bare-bones HTML as well as your favorite front-end framework, think React, Angular, Vue, even Django, or WordPress.

Also, an example in production that uses this library site-wide.

We just finished writing the docs, and we would be really thankful for some adventurers to take a look at it, and tell us if our solution is usable and down-to-earth. If you like it, we would really value your feedback, either here, or as a Github issue.

Also, if you think our library is useful, please give us a star on Github, it would make us really happy!

Thanks for taking the time to read this and if you do, for considering this pattern in your applications !

Final words

In consequence :

  • This allows for a HUGE reduction of CSS code relative to colors and theming (up to 90% when comparing component libraries made using this pattern with Bootstrap)
  • This pattern makes the CSS code much more maintainable.
  • Also, CSS vars make debugging in browser a breeze.

So far we are satisfied by the improvements that came from using this pattern in our projects, for over a year now. We would be glad to hear it helped you too. If you have any issues or suggestions about it please let us know here or at the github repository.

If you’re looking at the Swatch library, we’d like to make this clear : in our library, you won’t find any components or blocks. What we do is something simple: we just provide a simple implementation of the CSS pattern, we explained to take care of coloring and theming. Take a look at the docs or the examples to learn what you can build with it !

Full Stack Dev