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

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 UTC