Stefan Perju
Stefan Perju

Reputation: 298

Select only the second child of an element with CSS

I have something like this:

<div id="someID">
   <div class="text">
     -----other html tags
   </div>
   <div class="text">
      -----other html tags
   </div>
   <div class="text">
     -----other html tags
   </div>
</div>

And some CSS for the text div. It is possible to set different CSS for the second div with the class of text?

Upvotes: 3

Views: 6111

Answers (2)

stmfunk
stmfunk

Reputation: 683

You can use the pseudo selector nth-child i.e div.text:nth-child(2)

Upvotes: 1

wesbos
wesbos

Reputation: 26317

You can easily do with with nth-child:

#someID .text:nth-child(2) {
 background:red;   
}

Demo: http://jsfiddle.net/P6FKf/

Upvotes: 4

Related Questions