You’re reading diehealthy.org

Playing with sibling-index() for CSS Colors.

Warning: ugly examples ahead!

I’ve been trying to figure out the best way to color siblings (e.g., RSS items) using the new-ish sibling-index() and sibling-count().

Before, I used :nth-of-type() with algebraic choices (e.g., :nth-of-type(2n+1) selects the odds only), and specifying the colors manually.

While you cannot do arbitrary color selection (though with if() you could), you can do a few things to get colors you want using sibling-index().

The common example is:

.letter {
    --letcol: hsl(calc(360deg / sibling-count() * sibling-index()) 100% 50%);
    color: light-dark(hsl(from var(--letcol) h s 25%), hsl(from var(--letcol) h s 80%));
}
Example of dividing the hues into the number of items you have and then picking the nth in that series. I used a variable so I could apply light-dark() which doesn’t work within the color definition.
The quick brown fox jumped over the lazy dog.
Demonstration of rainbow coloring.

Works fine. The other obvious technique is to rotate through the hues by some fixed amount: calc(sibling-index() * 30deg). The color code of CSS automatically normalizes to between [0deg, 360deg) (MDN: <hue> CSS type), so that would start at 30 degrees, followed by 60, followed by… you get the picture:

The quick brown fox jumped over the lazy dog.
Demonstration of rotated coloring.

Without trying to understand, much less explain all the modular arithmetic, depending on what you specify in place of 30deg, you get different color patterns. We can quickly realize, for example, that if our multiplicand is coprime with 360, we will eventually rotate through all the integer values in some order. (We do not have to use integers, though.) If we use a small piece of 360, it will tend more toward our original rainbow, stepping slowly around the color wheel. If we use a large chunk and a number that evenly divides 360, say 120, it will only jump between three colors.

The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
Demonstration of multiplying by 179deg. The output is lengthened to show the colors progress (rather than repeat) as it continues.
The quick brown fox jumped over the lazy dog.
Demonstration of three color rotation.

But what if we don’t want some of the colors to appear? Without maths beyond me, the options are limited. We can return to the original rainbow design, but restrict it a bit: calc(300deg / sibling-count() * sibling-index()), for example, will make a rainbow of hues only up to 300 degrees. If we add an offset, we can shift what portion of the color wheel is available: calc(300deg / sibling-count() * sibling-index() + 90deg). That one would start at 90 degrees and go to 30 degrees, wrapping around. Here’s the picture:

The quick brown fox jumped over the lazy dog.
Demonstration of the restricted wheel coloring.

You can see that it starts with a light yellow-green and runs through orange, but yellow itself is missing. We have one more trick up our sleeve for now: mod(). We can combine the two techniques a little more, letting us rotate through a restricted gamut:

.letter {
    --csteps: 5;
    --cstep: calc(300deg / var(--csteps));
    --cchoice: mod(sibling-index(), var(--csteps));
    --letcol: hsl(calc(var(--cstep) * var(--cchoice)) 100% 50%);
    color: light-dark(hsl(from var(--letcol) h s 25%), hsl(from var(--letcol) h s 80%));
}
Using some extra variables, we calculate a number to offset each step, and we bucket the items into a number of step buckets.
The quick brown fox jumped over the lazy dog.
Demonstration of the restricted rotated coloring.

I don’t see a way to also do progression with this form, but perhaps it’s possible. You could add the offset to this version, if you wish.

I look forward to using if() for arbitrary color selections with sibling-index(), but it’s interesting to see how far you can push this without it.


blog discovery

Ye Olde Blogroll

An alphabetical listing of blogs. (Page not in Old-English.)

Please Deposit Two Cents:

This site uses Akismet to reduce spam. Learn how your comment data is processed.