Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple child elements using nth-child

div div p:nth-child(1 to 5)

How can I select multiple numbers with the nth-child, so I get the child elements 1 to 5 without having to write:

div div p:nth-child(1),
div div p:nth-child(2),
div div p:nth-child(3),
div div p:nth-child(4),
div div p:nth-child(5) {

}

So it should look like this:

div div p:nth-child(1 to 5)
like image 708
user3537202 Avatar asked Apr 21 '14 18:04

user3537202


People also ask

How do Nth children choose multiple children?

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.

How do I select all 3 children in CSS?

formula (an + b) In addition to the value n being able to be any number, you can also use a formula. nth-child(3n) would affect every third child element. nth-child(3n+1) would apply to every third element starting from the first one.

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 select multiple elements?

Selecting multiple elements To select multiple elements, you can simply click and drag your cursor over your them. Alternatively, hold down the Ctrl / Shift key on your keyboard (Cmd key for Mac) and click the elements to select them. Tip: You can select all elements at once by pressing Ctrl+A on your keyboard.


2 Answers

div div p:nth-child(n+1):nth-child(-n+5){

}

will select elements 1 to 5

like image 128
Banana Avatar answered Oct 05 '22 08:10

Banana


div div p:nth-child(-n+5){

}

This will select the first 5 children.

like image 33
jonode Avatar answered Oct 05 '22 08:10

jonode