Advanced Tree Counting: Mathematical Layouts With `sibling-index()` And `sibling-count()`
[RSS: www.smashingmagazine.com] Meet `sibling-index()` and `sibling-count()`. Staggered cascade effect in one line of CSS without `:nth-child()` rules or JS workarounds. Works for 5 items or 5,000.
Advanced Tree Counting: Mathematical Layouts With `sibling-index()` And `sibling-count()`
[RSS: www.smashingmagazine.com] Meet `sibling-index()` and `sibling-count()`. Staggered cascade effect in one line of CSS without `:nth-child()` rules or JS workarounds. Works for 5 items or 5,000.
[RSS: www.smashingmagazine.com] Meet `sibling-index()` and `sibling-count()`. Staggered cascade effect in one line of CSS without `:nth-child()` rules or JS workarounds. Works for 5 items or 5,000.
You know that thing where you have a grid of cards, and you want them to fade in one after another? That staggered cascade effect. Looks great. Should be simple. And yet every time Iโve built it, the implementation has made me feel like Iโm doing something fundamentally stupid.

The current spec only counts all element siblings. But the CSSWG has documented a planned extension in issue #9572: an of <selector> argument, matching what :nth-child() already supports.
Something like sibling-index(of .active) would let you count only siblings matching a specific selector. An element thatโs the eighth child overall but the third .active child would return 3. For dynamic UIs where youโre filtering or toggling visibility, that would keep the index sequential without requiring DOM manipulation.
Thereโs also been CSSWG discussion around children-count() and descendant-count() functions โ the first would tell you how many children an element has (useful for parent-driven layouts), the second would count all descendants recursively. Both are still at the proposal stage, but theyโd round out the tree-counting story: sibling-index() and sibling-count() give you the horizontal view (where am I among my peers?), while children-count() and descendant-count() would give you the vertical view (whatโs below me?).
That feeling I mentioned at the top โ writing ten :nth-child() rules for a staggered animation and wondering if youโre missing something obvious? You werenโt. The obvious thing just didnโt exist yet.
Admin
Editor
Comments (0)
No comments yet. Be the first to share your thoughts!