A recent project I worked on had a common design element across many pages: A cool-looking gradient background composed of multiple color “spotlights.” The colors of the spotlights varied from page to page, as well as their placement. The gradient only covered the header on some pages, while on others it extended further down… sometimes covering the entire page!
Historically, this would have been a difficult challenge. Before browsers had good support for CSS radial gradients, you could only do this with background images. Each variation would require a new image, and the full-page versions would have suffered from performance problems or unsightly compression artifacts.
Even after CSS radial gradients arrived, the variations would have caused a problem. Likely, I would have created a class, and added a bunch of modifiers like to override the CSS in certain situations. The performance problem from the images would be gone, but you couldn’t easily create new variations without making new CSS modifier classes.
My preferred technique for something like this is to turn to CSS variables — aka, custom properties — to allow me to create a single CSS class with intelligent defaults that can be easily overridden. Check this out:
You can see that the color, size, and position of each color spotlight are defined as custom properties. We initially set those in the element, so anywhere this class is used, it will have the default values available. But you can easily override some or all of those custom properties on any page to change the background.
Let me show you how easy it is:
To make a “watermelon” variation we’ve updated the colors of all the spotlights as well as the background to give a more fruity style. Then we changed the size of the top left and top right spotlights and set them to the same color to make them look more like a single shadow stretching across the top of the viewport.
This rainbow example goes further by adjusting the position of all four spotlights from the corners to display in a diagonal across the viewport, matching the angle of the text. Even though there are only four spotlights, the blending between them manages to produce a complex rainbow.
And for fun, here’s an interactive version. It will even show you the custom properties you need to override to create your version.
When I sat down to write this article, I realized that the point for me wasn’t “look at this cool gradient I built.” (Although it is pretty slick, if I do say so myself.) It’s to encourage you (and remind myself) to look for opportunities to make the things you build customizable.
Even if you don’t expect it to be reused, it might open possibilities you weren’t expecting, or allow it to be used in new contexts. The easiest way to do this is when you find yourself hard-coding a value, consider making it a variable instead. I’ve never regretted making something customizable, but I’ve often had to refactor something that was only designed to be used in one way.