16

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>
1
  • 1
    Don't get multiples of 6 and multiples of 3 mixed up... not every multiple of 3 (3, 6, 9, 12...) is a multiple of 6 (6, 12, 18, 24...).
    – BoltClock
    Commented May 17, 2012 at 16:37

2 Answers 2

34

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");
1
  • Additionally I would recommend this link that as a nice table with Result Sets for Pseudo-class Expressions!
    – Zuul
    Commented May 17, 2012 at 16:32
1

Use the nth selector in css

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

see more here

Not the answer you're looking for? Browse other questions tagged or ask your own question.