greater-than and less-than use of :nth-child() (was Re: Selectors in jQuery)

On Thursday 2008-10-09 10:10 +0200, Daniel Glazman wrote:
> - :gt(n) and :lt(n) are redundant with combinations of :not()
>   and :nth-child() but are far easier to manage if n is high

I'm writing this offline, and assuming these are "greater than" and
"less than" on the child index...

There's actually a tricky little way of doing these with
:nth-child() alone and without :not(), since the rule is that a
child matches :nth-child(an+b) if there is a *nonnegative* value of
n such that its index is an+b.

This means that:
  :nth-child(n+5) matches children 5, 6, 7, ...
  :nth-child(2n+5) matches children 5, 7, 9, ...
  :nth-child(-n+7) matches children 1, 2, 3, 4, 5, 6, 7
  :nth-child(-3n+8) matches children 2, 5, and 8

It's ugly, but it is possible.

-David

-- 
L. David Baron                                 http://dbaron.org/
Mozilla Corporation                       http://www.mozilla.com/

Received on Friday, 10 October 2008 01:14:39 UTC