Yeah, it is a bit difficult to understand.  I've searched the web for 
explanations and have even discovered inconsistent explanations.  Here 
is how I understand it.

The position of the returned elements have to equal an+b for every n >= 
0.  This is because the nth-child selector is supposed to return 
elements that have an+b-1 elements before it.  Therefore, to have an+b-1 
elements before it, the element would have to be at position an+b.  So,

position = an+b
position-b = an
(position-b)/a = n
(position-b)/a >= n (only if (position-b)/a is an integer - no fractions)

So, for your examples:

(n-2) would select all elements because (n-2) => (position+2) which is 
 >= 0 for all positions

(3n-2) would select elements 1,4,7,10 because (3n-2) => (position+2)/3
Another way to look at this is to say, create groups of three and grab 
the element at -2 (or 1 as 3-2 = 1)

This site might be useful:


Andrew Dupont wrote:
> I'm frustrated at the ambiguity of the CSS3 spec on this issue.  See
> for yourself at (
> #nth-child-pseudo). The spec says that a and b "must be zero, negative
> integers or positive integers," but does not give any examples in
> which b is negative -- neither in the spec nor in the test suite.
> Firstly: I can only assume that if b is negative the syntax is, for
> instance, (3n-2) instead of (3n+-2), as the latter would just be
> silly.
> Secondly: If I assume the same sort of logic for negative values of b
> as for positive values, then out of a group of 10 elements:
>  (n-2)  selects elements 1-8 (just like n+2 selects 2-10 and -n+2
> selects 1-2)
>  (3n-2) selects elements 1, 4, and 7 (the -2 value of b determines the
> modulus and also excludes the last two items in the collection)
>  (n-9)  selects element 1
> Here's a pastebin of the rewritten logic. (
> 47481) Note that the "total" argument specifies the length of the
> collection (so that it returns only indices that actually fall within
> the result set).
> Thoughts on either of these issues?
> Cheers,
> Andrew
> >

You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at

Reply via email to