Select elements multiple of 3 in the parent element

Is there a way to select css, elements that have an index multiple of 6 inside the parent element?

for example, in this case I want to select only a few 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> 
+6
source share
2 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 can use jQuery to select elements and add a class to them:

 $("p:nth-child(3n)").addClass("redbg"); 
+20
source

Use nth selector in css

 p:nth-child(6n) {/*css here*/} 

see here for more details

+1
source

Source: https://habr.com/ru/post/916007/


All Articles