Skip to main content
SiteScan.top

Dynamic nth-child Snippet: How It Works

A breakdown of the dynamic nth-child snippet using sibling-index(), if() and style(): how the formula works, the risks, and why to audit CSS on your site По

Blog · Published: 28 November 2026 · 4 min read

Modern CSS increasingly offers alternatives to familiar selectors, and a telling example is the dynamic nth-child snippet, which reinvents the classic :nth-child() using the new sibling-index(), if(), and style() functions. For developers tracking the latest browser capabilities, this approach is interesting not merely as an experiment, but as a reason to check how ready the code is for real-world use on a site.

Illustration of dynamic nth-child and checking CSS browser support

What a dynamic nth-child is and why it matters

A regular nth-child is hardcoded into the selector: a formula like 3n+5 can't be changed without rewriting the CSS rule. If you need to adjust the step or offset for different blocks on a page, you end up duplicating styles or reaching for a preprocessor.

Dynamic element selection solves this differently: the formula's parameters live in CSS variables --A and --B, while the logic itself is calculated within a single universal rule. This is handy when values need to change on the fly — say, from a script — without editing the styles.

How the snippet works with sibling-index(), if(), and style()

At its core lies a combination of three new CSS tools. The container is given the variables --A: 3 and --B: 5, and then for the child divs a condition is calculated: if the remainder of (sibling-index() − B) divided by A equals zero and the division result is non-negative, the element's background turns red.

This notation replaces the familiar :nth-child(An+B) selector with a full formula that can be recalculated without altering the CSS rule's markup — only the variable values change.

Browser support and production risks

As elegant as this scheme is, it's worth keeping in mind that if(), style(), and sibling-index() are functions from very recent CSS specifications, and their behavior doesn't always match across browsers yet. Before using this in a production project, it's worth checking the actual rendering picture rather than relying on documentation alone.

Browser compatibility

Some browsers already support individual pieces of this combination, but the full set of three functions working together isn't equally stable everywhere.

Code maintainability complexity

The second risk is readability and maintenance. A formula with mod(), calc(), and nested conditions is harder to grasp than the familiar :nth-child(3n+5), and the case of --A: 0 deserves special attention, since it must be handled manually — otherwise the division produces an error.

How a site audit helps spot such experiments

When new CSS functions appear on a site, it's worth separately confirming they don't break rendering across certain browsers and devices. A technical page audit helps catch these spots before visitors notice them.

Checking the markup reveals which blocks actually receive the formula-based styles and which remain visually unchanged because a particular function isn't supported.

Check how much of this is on your own site

A free scan surfaces broken links, JS errors and performance problems in about a minute.