W3C home > Mailing lists > Public > www-style@w3.org > November 2012

Re: nth descendent selector

From: Lea Verou <lea@w3.org>
Date: Fri, 9 Nov 2012 22:14:21 +0200
Cc: www-style w3 <www-style@w3.org>
Message-Id: <C259E3D1-B460-4AA7-916E-1BFB902B6745@w3.org>
To: Dave Smith <da__smith@hotmail.com>
Sounds like this would be solved by the :nth-match() pseudo-class [1] in Selectors 4. Specifically, the selector in your example would be li:nth-match(7 of li) or just :nth-match(7 of li).

[1]: http://www.w3.org/TR/selectors4/#the-nth-match-pseudo

Lea Verou
W3C developer relations
http://w3.org/people/all#leahttp://lea.verou.me ✿ @leaverou






On Nov 9, 2012, at 12:33, Dave Smith wrote:

> Hi
> 
> 
> The :nth-of-type and similar selectors are really great but is there reason why we don't have a selector equivalent to document.getElementsByTagName?
> 
> 
> A selector like ":nth" that would only ever select zero or one element in comparison to :nth-of-type or similar which can select zero or more elements.
> 
> 
> For example
> li:nth(7)
> would be equivalent to document.getElementsByTagName('li')[6] 
> 
> 
> I for one would find this useful in making my CSS more robust and easier to write, for example take this very rough and contrived scenario:
> 
> 
> Document 1:
> <h1></h1>
> <h1></h1>
> 
> 
> Document 2:
> <p></p>
> <div><h1></h1></div> 
> <h1></h1> 
> 
> 
> Document 3:
> <div> <div> <h1></h1> </div> </div> 
> <h1></h1> 
> 
> 
> In this example I want to ensure that the first h1 always has less of a margin above it (or conversely I want to ensure that any subsequent h1s have more of a margin above them) and importantly I have no control over the markup I'm receiving.
> 
> 
> There may be are other use-cases for a selector equivalent to document.getElementsByTagName.
> 
> 
> all the best
> Dave


Received on Friday, 9 November 2012 20:14:32 UTC

This archive was generated by hypermail 2.4.0 : Monday, 23 January 2023 02:14:21 UTC