Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the nth child of an element using CSS2 selectors?

Tags:

Is there any possibility to get the nth element of an ordered list in CSS2?

(similar to :nth-child() in CSS3)


FYI: I'm trying to program the German version of Parcheesi to be XHTML 1.1 and CSS2 compliant and trying to generate the playfield by using ordered lists, so movement options are predetermined by the data structure. For positioning the way around the center I'd like to select the list elements by number.

like image 556
myAces Avatar asked Mar 13 '12 10:03

myAces


People also ask

How do you find the nth child of an element?

Use the querySelector() method to get the nth child of an element, e.g. document. querySelector('#parent :nth-child(3)') . The :nth-child pseudo class returns the element that matches the provided position. Here is the HTML for the examples in this article.

How do you find the nth element using CSS selector?

The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

What is the nth child () selector used for?

The :nth-child selector allows you to select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.

How do you use the nth child in querySelector?

To get the nth-child of an element using the querySelector method, pass the :nth-child() pseudo-class as a parameter to the method, e.g. document. querySelector('#parent :nth-child(1)') . The nth-child pseudo-class returns the element that matches the specified position.


2 Answers

You can use adjacent sibling combinators in conjunction with the :first-child pseudo-class, repeating the combinators as many times as you need to reach a certain nth child. This is also mentioned in an answer to a different question.

For any element E, start with E:first-child, then add + E for subsequent children until you reach the element that you're targeting. You don't have to use the same element E, of course; you could switch that out for any type, class, ID, etc, but the important bits are the :first-child and + signs.

As an example, to get the third li of its ol, the following CSS3 selector:

ol > li:nth-child(3) 

Would be replicated in CSS2 like so:

ol > li:first-child + li + li 

An illustration:

<ol>   <li></li> <!-- ol > li:nth-child(1), ol > li:first-child -->   <li></li> <!-- ol > li:nth-child(2), ol > li:first-child + li -->   <li></li> <!-- ol > li:nth-child(3), ol > li:first-child + li + li -->   <li></li> <!-- ol > li:nth-child(4), ol > li:first-child + li + li + li --> </ol> 

Note that since there are no sibling combinators that look at preceding siblings (neither in CSS2 nor CSS3), you cannot emulate :nth-last-child() or :last-child using CSS2 selectors.

Additionally, you can only emulate :nth-child(b) for one specific child at a time, where b is a constant number in the formula an+b (as described in the spec); you can't achieve any complex formulas with adjacent sibling combinators alone.

like image 110
BoltClock Avatar answered Sep 28 '22 07:09

BoltClock


No you can't. nth-child() got introduced in CSS3 , so I don't think that there is an way around that problem.

  • http://www.w3.org/TR/selectors/#nth-child-pseudo
like image 44
mas-designs Avatar answered Sep 28 '22 07:09

mas-designs