This is admittedly a pretty raw idea, but I’m still curious what the community thinks of it.
Currently, the CSS background rule allows for passing in multiple background layers (color, image, gradient) as part of a single declaration.
Problem statement
Imagine a design system. Which is not hard to do as design is increasingly systemic in our industry. Often such design systems make use of flexible CSS utility classes.
Imagine we have 20 classes for colors, making up our brand. We also have 5 texture classes (repeating backgrounds). We apply these single purpose classes to markup to compose UI.
In this particular scenario, we want to be able to apply a background color to an element. Or, to give it a background image (example: noise pattern). And here comes the heart of the matter: we also want to be able to apply both. Example: a noise pattern on a gray background.
<div class="gray noise">
</div>
Edit: I realize this is not the best example as it could be solved using background-color and background-image separately. The issue may still occur though in several other situations such as combining multiple background images, gradients, etc.
This is not a far-fetched scenario, yet currently inefficient to implement. If either utility class uses the background-image property, they would overwrite each other. The only way to solve this currently is to have classes for every possible combination of layers. In this case: 5 x 20 = 100 classes.
We can easily dramatize the problem by introducing utility gradient classes. Now we would require several hundreds of classes.
Idea for a solution
The idea of additive backgrounds is that you can declare a background on an element which will be added to the existing declaration (that may exist elsewhere), if any. I haven’t really thought much about the syntax, so this is just to illustrate the idea:
.gray {
background: #ccc;
}
.noise {
+background: url('noise.gif');
}
When both of the above classes are applied to an element, the end result will be that the background consists of the color and the image background combined.
Notes
I imagine the order of declaration to be a potential source of confusion. In the example shown, one would probably want the image before the color. It may be tricky to reason about by developers.
Finally, should the idea have some merit, it could be considered for other CSS properties that expect multiple values. I have thought of filters and box shadows but don’t really see a practical use case there.