๐Ÿ‡ฎ๐Ÿ‡ณ Edition IN
Detecting...
Menu
Latest
Advanced Tree Counting: Mathematical Layouts With `sibling-index()` And `sibling-count()`
Politics

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.

Share:

[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.

Whatโ€™s Coming

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.

A

Admin

Editor

Comments (0)

No comments yet. Be the first to share your thoughts!