nth-child with mod (or modulo) operator
No, :nth-child()
only supports addition, subtraction and coefficient multiplication.
I gather you're trying to pick up the first 6 elements (as n mod 7
for any positive integer n
only gives you 0
to 6
). For that, you can use this formula instead:
:nth-child(-n+6)
By negating n
, element counting is done backwards starting from zero, so these elements will be selected:
0 + 6 = 6
-1 + 6 = 5
-2 + 6 = 4
-3 + 6 = 3
-4 + 6 = 2
-5 + 6 = 1
...
jsFiddle demo
If you want to use nth-child
with modulo k
, just specify:
nth-child(kn)
For example, if you want to specify style for 3, 6, 9, .. elements, just specify (k
= 3), and use:
nth-child(3n)
You can also specify an offset:
nth-child(kn+offset)