Page 1 of 1

:nth-child(expression)

Posted: Mon Feb 22, 2010 2:36 pm
by loftinsulation
Hello,

Given that, say,

Code: Select all

:nth-child(4n)
matches [0, 4, 8, ...], I was just wondering what something like

Code: Select all

:nth-child(-n+4)
would match?

Thanks very much.

Re: :nth-child(expression)

Posted: Mon Feb 22, 2010 5:38 pm
by pickle
I've not seen any practical examples where n is negative. What happens when you try it?

Re: :nth-child(expression)

Posted: Wed Feb 24, 2010 11:00 am
by loftinsulation
Probably should have given this a bit more thought before posting, as on reflection it seems fairly obvious. Assuming negative values return nothing, (-n+4) yields [3, 2, 1, 0, -1, ...] for n = 1, 2, 3..., and so this should give the first three elements of a set (given 1 based css indicies). Hmm, yes, refelection; always a good idea.

Re: :nth-child(expression)

Posted: Wed Feb 24, 2010 11:53 am
by pickle
It might be better then to just do :nth-child(0), :nth-child(1), :nth-child(2).

Re: :nth-child(expression)

Posted: Wed Feb 24, 2010 1:31 pm
by loftinsulation
Yes, that is probably true - certainly would make things more readily readable. I was just wondering about the use of negatives in expressions such as this, since I'd been reading about :nth-child and it wasn't mentioned. Although assuming it does work as I'm guessing above (haven't actually tested it yet), it does seem quite a nice solution to selecting the first x rows.