W3C home > Mailing lists > Public > www-style@w3.org > August 2010

Re: [Selectors3] :nth-child(-negative)

From: Jorrit Vermeiren <mercator+w3c@gmail.com>
Date: Tue, 17 Aug 2010 18:02:57 +0200
Message-ID: <AANLkTiknpo=R0DeCQa2hCbYyXV_XP=iG1WOitAkTVJUd@mail.gmail.com>
To: Christoph Päper <christoph.paeper@crissov.de>
Cc: "www-style@w3.org list" <www-style@w3.org>
On Tue, Aug 17, 2010 at 11:31, Christoph Päper
<christoph.paeper@crissov.de> wrote:
> Currently :nth-child(an+b) etc. only match anything if the argument, an+b, evaluates to a positive value.
(...)
> I assume it helps implementation optimization that authors have to resort to :nth-last-child() if they need to count from the end. If this is not the case, would it break any other use case to define :nth-child(-1) as matching the last child (and other negative values accordingly)?

I don't think it's a matter of optimizing implementations. Having
negative numbers start counting from the end could mean you get two
expressions for the price of one.

The following example currently matches only the the first five list
items. If negative values start matching elements counting from the
end, this would simply match all list items:

  li:nth-child(-n+5) { color: red }


Even more, the following selector matches only the 2nd and 5th child:

  li:nth-child(-3n+5) { color: red }

With negative values counting too, though, that would become the same as this:

  li:nth-child(-3n+5), li:nth-last-child(3n+1) { color: red }


And finally,

  li:nth-child(3n-4) { color:red }

currently does the same as this:

  li:nth-child(3n+2) { color: red }

but with your suggestion, it would do this instead:

  li:nth-child(3n+2), li:nth-last-child(-3n+4) { color: red }


Counting from the end and counting from the start are simply two
different operations. Only if a and b in the (an+b) expression have
the same sign, or at least one of them is zero, can you be sure that
you don't get those operations mixed up.

For simple expressions such as :nth-child(-1) there wouldn't be any
problem, but I don't think it would be a good idea to have negative
numbers only work like that in some cases.
Received on Tuesday, 17 August 2010 16:03:36 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 17:20:30 GMT