CSS-Tricks Reimplements :nth-child() With Modern CSS
CSS-Tricks has reimagined the :nth-child() feature, allowing for dynamic adjustments and exploring new CSS features. This reimplementation enables developers to adjust the :nth-child() arguments on the fly.
Original reporting · css-tricks.com
Original: “Re-Implementing :nth-child() With Modern CSS”
Read “Re-Implementing :nth-child() With Modern CSS” at CSS-Tricks →
How this was written. CTCV wrote this article with a language model from CSS-Tricks’s reporting (model: @cf/meta/llama-3.3-70b-instruct-fp8-fast); it describes that reporting rather than adding to it. The original, linked above, is the full story.
Key points from CSS-Tricks’s report
- CSS-Tricks has reimagined the :nth-child() feature to enable dynamic adjustments and explore new CSS features.
- The reimplementation relies on a simple code structure and utilizes CSS variables to control the :nth-child() behavior.
- The if() syntax and the sibling-index() function are used to implement the :nth-child() selectors.
- The reimplementation covers various cases, including selecting single and multiple elements, range selection, and selecting elements based on a multiple of A.
- The generic form of :nth-child() can be implemented by combining the learned tricks.
According to CSS-Tricks, the reimplementation of :nth-child() is driven by two main reasons: to enable dynamic adjustments to the :nth-child() arguments and to explore new CSS features for fun and learning. The existing :nth-child() feature requires manual rewriting of the selector to update its arguments, which can be impractical.
The reimplementation relies on a simple code structure, consisting of a container with multiple div elements, and utilizes CSS variables to control the :nth-child() behavior. CSS-Tricks reported that this approach allows for easy adjustments to the :nth-child() arguments and enables developers to learn new CSS tricks, such as using the sibling-index() and if() functions.
The simplest case of the reimplementation involves selecting a single element based on its index, which can be achieved using the if() syntax and the sibling-index() function. For example, the selector div:nth-child(5) can be translated into CSS code using the if() syntax and the sibling-index() function, as demonstrated by CSS-Tricks.
CSS-Tricks also explored more complex cases, including selecting multiple elements using the :nth-child(n + B) and :nth-child(-n + B) selectors. These selectors can be implemented using the if() syntax and the sibling-index() function, with minor adjustments to the condition. Additionally, CSS-Tricks demonstrated how to implement a range selection using the combination of both selectors.
The reimplementation also covers the :nth-child(An) selector, which selects elements based on a multiple of A. This can be achieved using the mod() function to test if the index is a multiple of A. However, CSS-Tricks noted that this implementation has a small gotcha when A is a negative value, which can be addressed by adding an additional condition to test the sign of A.
Finally, CSS-Tricks combined the learned tricks to implement the generic form of :nth-child(), which involves combining the An and B arguments. This implementation enables developers to dynamically adjust the :nth-child() arguments and explore new CSS features.
The reimplementation of :nth-child() with modern CSS, as reported by CSS-Tricks, provides a new approach to using this feature and enables developers to learn new CSS tricks and techniques.
Questions and answers
Why was :nth-child() reimplemented?
The reimplementation was driven by two main reasons: to enable dynamic adjustments to the :nth-child() arguments and to explore new CSS features for fun and learning.
How does the reimplementation work?
The reimplementation relies on a simple code structure, consisting of a container with multiple div elements, and utilizes CSS variables to control the :nth-child() behavior.
What are the benefits of the reimplementation?
The reimplementation enables developers to dynamically adjust the :nth-child() arguments and explore new CSS features, such as using the sibling-index() and if() functions.


