W3C home > Mailing lists > Public > www-style@w3.org > October 2008

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

From: L. David Baron <dbaron@dbaron.org>
Date: Thu, 9 Oct 2008 10:32:55 -0700
To: Daniel Glazman <daniel.glazman@disruptive-innovations.com>
Cc: "www-style@w3.org" <www-style@w3.org>, jresig@mozilla.com
Message-ID: <20081009173255.GA10962@pickering.dbaron.org>

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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 27 April 2009 13:55:15 GMT