Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select elements multiple of 3 in parent element

is there a way to select with css, elements which have the index multiple of 6 inside a parent element?

for example, in this case i want to choose only multiple of 3:

<div>
    <p></p>
    <p></p>
    <p></p> <!--to select -->
    <p></p>
    <p></p>
    <p></p> <!--to select -->
    <p></p>
    <p></p>
    <p></p> <!--to select -->
</div>
like image 969
Luke Avatar asked May 17 '12 16:05

Luke


People also ask

How do I select multiple elements at once?

Selecting multiple elements 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.

How do you select multiple child elements in CSS?

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).

What are the 3 selector in an element type selector?

Pseudo-class selectors (select elements based on a certain state) Pseudo-elements selectors (select and style a part of an element) Attribute selectors (select elements based on an attribute or attribute value)

Can I select multiple elements at once with CSS?

When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing.


1 Answers

Use :nth-child(n):

p:nth-child(3n) {
  background: red
}

Demo: http://jsbin.com/azehum/4/edit

This method works in IE9+ (source: caniuse.com). If you need support in older browsers, you could use jQuery to select the elements and add a class to them:

$("p:nth-child(3n)").addClass("redbg");
like image 194
Sampson Avatar answered Nov 16 '22 02:11

Sampson