Page 04: The loop
Retires forEach((el, i) => ...),
getBoundingClientRect() and Math.random()
CSS could always calculate. What it could not do was count, measure or roll dice, so a script did those three things and handed over the results.
The pattern is in every staggered animation ever shipped: loop over the
children and write each one's index into a custom property, so that the
stylesheet has a number to multiply. It is in this zine too. The drum on
page 01 gets its face numbers from twelve
:nth-child rules, which is the same loop unrolled by hand.
items.forEach((el, i) => {
el.style.setProperty("--i", i + 1);
el.style.setProperty("--n", items.length);
});
li {
--i: sibling-index();
--n: sibling-count();
}
One rule, three rings
sibling-index() and sibling-count() are in
Chrome 138, Safari 26.2 and Firefox 154. With them an element knows its
position and the size of its family.
The three rings below have five, eight and thirteen tiles, and one rule between them. Each tile works out its own angle, which is a full turn divided by the count and multiplied by its index. It also works out how far from the axis to stand, which is half its width over the tangent of half that angle, so that neighbouring edges just meet. Add a fourteenth tile to the HTML and that ring becomes a slightly larger polygon with nothing else changed. Hover a ring to hold it still.
In a browser with sibling-index(), each list below is a
ring standing in three dimensions, and all three are built by one shared
rule. Yours gets them as rows of tiles numbered by CSS counters.
Counters have been able to count since 1998. They could never hand the
result to a calculation.
5 children
8 children
13 children
rotateY(calc(360deg / sibling-count() * (sibling-index() -
1)))
then
translateZ(calc(1.65rem / tan(180deg / sibling-count()))). The numbers on the tiles are an ordinary CSS counter.
A length divided by a length
calc() used to refuse this. You could multiply a length by
a number, but dividing one length by another to get a plain ratio was a
type error, so anything that needed to know how many of these fit in
that went to getBoundingClientRect(). Typed arithmetic
lifts the restriction. It is in Chrome 140 and Safari 26, and Firefox
158 made it the third engine this autumn.
This window is CSS pixels wide. This box fits of the digits above on one line.
100vw / 1px. The second is 100cqi / 1ch: the
width of the box divided by the width of a zero in its own font. Each
result is rounded down, stored in a registered integer property and
printed with counter().
Dice
Math.random() was the last reason a purely visual effect
needed a script. random() is in Chrome 156 and Safari 26.2,
and in Firefox 161, which is still in beta as I write this. Each element
gets its own value for each property unless you ask for a shared one.
random() as well. Reload the
page: no two printings match.
random() yet, so the red pass has landed exactly on the
blue one and will do so on every reload. Chrome 156 and Safari 26.2
print this a little crooked, and differently each time.