5.5 · Logic in CSS

Know your position: sibling-index()

Every element knows its position, so staggering needs no :nth-child() rules.

sibling-index() returns an element’s position among its siblings, starting at 1. sibling-count() returns how many there are. Both are numbers you can use in calc().

Staggered animations and evenly spread colors no longer need :nth-child() rules or an inline --i on every item.

Before
li:nth-child(1) { transition-delay: 0ms; }
li:nth-child(2) { transition-delay: 60ms; }
li:nth-child(3) { transition-delay: 120ms; }
/* …or style="--i: 3" on each item */
Now
li {
  transition-delay:
    calc((sibling-index() - 1) * 60ms);
  background: oklch(80% 0.12
    calc(sibling-index() * 360deg / sibling-count()));
}