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>
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.
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).
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)
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With