benhowdle89
benhowdle89

Reputation: 37504

A question on nth CSS

I have 12 divs.

I need to select the ones marked THIS. I was thinking some sort of nth child magic?

Thanks!

ie.

<div class="feature"></div>
<div class="feature"></div> -> THIS
<div class="feature"></div>
<div class="feature"></div>
<div class="feature"></div> -> THIS
<div class="feature"></div>
<div class="feature"></div>
<div class="feature"></div> -> THIS
<div class="feature"></div>
<div class="feature"></div>
<div class="feature"></div> -> THIS
<div class="feature"></div>

Upvotes: 2

Views: 106

Answers (3)

Nicole
Nicole

Reputation: 33227

You can use the nth-child pseudo-selector with a number expression:

div.feature:nth-child(3n+2) {
    background-color:#cccccc;
}

3n+2 means every third row starting with the second row.

By the way, the link at SitePoint says that all modern browsers except IE8 support have full support. IE9 has full support, but make sure you are not in compatibility mode.

Upvotes: 2

drfranks3
drfranks3

Reputation: 665

Building off ob Sarfraz's answer, N can be set to 3n - 1

.feature:nth-child(3n-1){ ... }

Upvotes: 0

Nightfirecat
Nightfirecat

Reputation: 11610

You're looking at needing the selector :nth-child(3n+2).

Reference: http://css-tricks.com/examples/nth-child-tester/

Upvotes: 1

Related Questions