Reputation: 1215
Given the following markup, how do I select every third 'location-block' in CSS? Thank you! I tried .location-block:nth-child(3n){}
but that did not work.
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block"> <!-- Want to Select This -->
Lorem Ipsum Dolor
</div>
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block"> <!-- Want to Select This -->
Lorem Ipsum Dolor
</div>
Upvotes: 1
Views: 2106
Reputation: 14002
Hello there if you know your markup(HTML) is going to be all div's you don't need the use of clases.
div:nth-of-type(3n){background:tomato}
<div>
Lorem Ipsum Dolor
</div>
<div>
Lorem Ipsum Dolor
</div>
<div>
Lorem Ipsum Dolor
</div>
<div>
Lorem Ipsum Dolor
</div>
<div>
Lorem Ipsum Dolor
</div>
<div>
Lorem Ipsum Dolor
</div>
Thanks T04435.
Upvotes: 0
Reputation: 2896
.location-block:nth-child(3n){ }
works. You can see here
Also you can use :nth-of-type
.location-block:nth-of-type(3n) {
}
Here you have a working example https://jsfiddle.net/8eh2r06h/
Upvotes: 3
Reputation: 1588
Works fine for me. Maybe you're using IE 8 or earlier. And, as drosam said, maybe use :nth-of type()
instead, if the div
s aren't the only children in whatever container they are in.
.location-block:nth-child(3n){color:red}
.location-block:nth-of-type(3n){background:yellow}
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block">
Lorem Ipsum Dolor
</div>
<div class="location-block">
Lorem Ipsum Dolor
</div>
Upvotes: 2