Menu

Goodbye :nth-child! How Modern CSS sibling-index() Works Under the Hood
πŸ“°

Goodbye :nth-child! How Modern CSS sibling-index() Works Under the Hood

DEV Community: cssΒ·Muneeb AliΒ·8 days ago
#lcUQ4WL8
#devtocss#devto#dev#sibling#index
Reading 0:00
15s threshold

Ever found yourself writing endless lines of :nth-child(1) , :nth-child(2) , :nth-child(3) or injecting custom CSS variables via JavaScript just to stagger a list of entrance animations?

Modern CSS (Values & Units Module Level 5) introduces two native functions:

sibling-index() β€” Returns the 1-based index of the element among its siblings.

sibling-count() β€” Returns the total count of siblings under the parent.

Here is the complete video breakdown and live animation:

1. Staggered Delays in Pure CSS

.card { animation : cardFadeIn 0.6s cubic-bezier ( 0.16 , 1 , 0.3 , 1 ) both ; animation-delay : calc ( sibling-index () * 120ms ); }

Enter fullscreen mode

Exit fullscreen mode

2. Dynamic 360Β° Color Wheels with OKLCH

.chip { --hue : calc (( sibling-index () / sibling-count ()) * 360deg ); background : oklch ( 0.68 0.22 var ( --hue )); }

Enter fullscreen mode

Exit fullscreen mode

3.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.