The argument of :nth-child() is fixed at authoring time; changing 5 or 8 in :nth-child(5n + 8) means rewriting the selector. Re-implementing the selector in pure CSS removes that constraint: the container exposes the arguments as custom properties, and the browser recomputes the selection whenever they change. It is also a practical tour of two recent features, sibling-index() and the if() syntax inside style().
Every example uses one structure — a container holding a set of <div> children — with the parameters defined on that container:
.container {
--A: ;
--B: ;
}
.container > div {
/* based on the values of --A and --B change the background value */
background: ??;
}
The markup is not a requirement of the technique; it just keeps the examples readable.
<div class="container">
<div></div>
<div></div>
<div></div>
<!-- ... -->
</div>
A single index: :nth-child(B)
The simplest form selects one element, where B is that element's position. With sibling-index() returning the current element's index, the selector becomes an equality test:
div:nth-child(5) { }
5 is nothing more than the index of the chosen element, so the element is selected when B equals its index. In CSS, that reads as:
.container {
--B: 5;
}
.container div {
background: if(style(sibling-index() = --B): red);
}
This is valid CSS. The new if() syntax lets a condition be declared inside style(): when the condition holds, the background turns red; otherwise nothing is applied. An if/else branch is equally possible:
.container {
--B: 5;
}
.container div {
background: if(style(sibling-index() = --B): red; else: lightblue);
}
With a single condition, if() reduces to:
.container div {
background:
if(style( condition ): <color_if_condition_true>;
else: <color_if_condition_false>);
}
The subtle part is how a condition is written within style(); see the linked write-up “The Hidden Trick of Style Queries and if()” for the details. Several equivalent spellings are valid:
.container div {
background: if(style(sibling-index() = --B): red);
background: if(style(sibling-index() = var(--B)): red);
background: if(style(var(--B) = sibling-index()): red);
background: if(style(--B = sibling-index()): red);
}
That completes the first implementation, and the same pattern applies to any property, not just background color:
.container {
--B: 5;
}
.container div {
background: if(style(sibling-index() = --B): red; else: lightblue);
color: if(style(sibling-index() = --B): white);
}
The repetition in the code is a genuine drawback of this approach, but the value of --B can now be changed live. Two techniques are already in hand: sibling-index() and if().
Inequalities and ranges
The next form selects more than one element. It differs from the equality case only in the comparison: if the element's index is greater than or equal to --B, the element is selected.
div:nth-child(n + 5) { }
.container {
--B: 5;
}
.container div {
background: if(style(sibling-index() >= --B): red; else: lightblue);
}
Flipping the operator gives the leading -n form, which is the mirror image — use <= instead of >=:
.container {
--B: 5;
}
.container div {
background: if(style(sibling-index() <= --B): red; else: lightblue);
}
Combining the two yields a range selection, again a small change to the same comparison chain:
/* select any element in the range [X Y] */
div:nth-child(n + X):nth-child(-n + Y) {
background: red;
}
.container {
--X: ;
--Y: ;
}
.container div {
background: if(style(--X <= sibling-index() <= --Y): red);
}
That code needs no explanation, and it is arguably more intuitive than the :nth-child() notation, which takes a few minutes to decipher for anyone out of practice.
Multiples: :nth-child(An)
The A argument selects every third element starting at the third — in other words, elements whose index is a multiple of 3. The mod() function tests exactly that: when mod(index, 3) is 0, the remainder of dividing the index by 3 is zero, so the index is a multiple of 3.
div:nth-child(3n) { }
.container {
--A: 3;
}
.container div {
background: if(style(mod(sibling-index(), var(--A)) = 0): red);
}
Two values of --A deserve note. At 1 the selector is :nth-child(n), matching every element; at 2 it is :nth-child(2n), alias :nth-child(even).
There is a gotcha with negative values of A. Logically, a negative step should match nothing, since all indices are positive — yet with the naive mod() test some elements are still selected. mod() accepts negative numbers and can return 0 for them, so the sign of A has to be tested as well:
.container {
--A: 3;
}
.container div {
background:
if(style( (mod(sibling-index(), var(--A)) = 0) and (var(--A) > 0): red);
}
A second condition is added inside style() with the and keyword. Despite first impressions, the if() syntax is like any other language: a set of conditions inside style(), combinable with and and or in any number. The selection is now correct for negative steps.
The general form: :nth-child(An + B)
Here the index must equal An + B, so n can be isolated as:
n = (index - B)/A
n must be a non-negative integer (zero included). The non-negativity gives:
(index - B)/A >= 0
The integer part requires index - B to be a multiple of A, again tested with mod():
mod(index - B, A) = 0
Translated into CSS:
.container {
--A: 3;
--B: 5;
}
.container div {
background:
if(style( (mod(sibling-index() - var(--B), var(--A)) = 0)
and (calc((sibling-index() - var(--B))/var(--A)) >= 0)
): red);
}
The remaining failure case is --A: 0, which breaks the selection: mod() returns NaN when its second argument is 0 and corrupts the condition, which was never designed for a zero step. One option is to declare the implementation valid only for non-zero --A. The alternative is to handle the case explicitly:
.container {
--A: 3;
--B: 5;
}
.container div {
--n: calc((sibling-index() - var(--B))/var(--A));
--g: if(style(--A = 0):
calc(sign(sibling-index() - var(--B)) + 1);
else:
calc(sign(var(--n) + 1) + mod(var(--n),1))
);
background: if(style(--g = 1): red);
}
The mechanism is left as an exercise in reading. A new variable is introduced, meant to equal 1 exactly when the element is selected by :nth-child(An + B). A fuller explanation is available in the post “Dynamic nth-child() using sibling-index() and if().”
Deriving the earlier forms
Every selector covered above falls out of the complete implementation by adjusting the variables:

:nth-child(7)
:nth-child(n + 10)
:nth-child(-n + 10)
:nth-child(3n)There is one natural extension left: :nth-last-child() can be built from the same code with one change — it likely needs sibling-count(). Underneath the syntax, a selector is only a set of conditions filtering a sibling list, which is precisely how this re-implementation treats it.



