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.

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.
- flexible configuration of step and offset through variables, without rewriting the selector;
- the ability to adapt the selection to different containers using the same block of code;
- potential for scenarios where parameters are set dynamically rather than fixed once.
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.
- sibling-index() — returns the element's ordinal position among its siblings in the container;
- style() — the condition inside if() that checks the computed value of a CSS expression;
- if() — a built-in conditional function that substitutes the right result based on that condition.
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.
- desktop browsers on newer versions more often handle the basic if() and style() syntax;
- mobile versions and older builds may ignore the condition or fall back to a default value;
- without a fallback, elements that miss the formula-based highlighting risk being left with no styling at all.
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.
- more room for typos in the formula due to nested functions;
- harder to explain the logic to colleagues without comments in the code;
- a separate plan is needed for edge-case variable values, including zero.
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.
- analysis of CSS for the use of new and experimental constructs;
- comparing how the page renders across different browser engines;
- suggestions for adding fallback styles for older versions.
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.