Reputation: 2368
NOTE: SEE BELOW FOR CLEARER EXPLANATION
I'm trying to figure out why this is happening.
HTML
<div class="chicken">
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
</div>
CSS
.chicken { width:100%; background:#999; float:left; }
.big-chix { width:48%; margin:0.5%; padding:0.5%; background:#666; float:left; }
.big-chix:nth-child(2n+1) { background-color:#eee; }
.big-chix:nth-child(2n+2) { background-color:#aaa; }
What I'm trying to achieve here is to put a different background for .big-chix
class for nth children 1, 3 , 5... and 2, 4, 6...
But when I put in a paragraph (or anything else like a div, etc for that matter), it becomes like this:
HTML
<div class="chicken">
<p>paragraphy</p>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
<div class="big-chix">Contento</div>
</div>
CSS
.chicken { width:100%; background:#999; float:left; }
.big-chix { width:48%; margin:0.5%; padding:0.5%; background:#666; float:left; }
.big-chix:nth-child(2n+1) { background-color:#eee; }
.big-chix:nth-child(2n+2) { background-color:#aaa; }
The nth-child placement switches places. Why is this so? Isn't .big-chix:nth-child()
only suppose to select all the .big-chix
classes (which is 6 .big-chix
), then set 1, 3, 5 to a background-color
of #eee
, and 2, 4, 6 to #aaa
?
EDIT: From what I gather, nth-child
will not apply to an element child in the element parent in a code like this:
jsFiddle - nth-child(1) when <p>
paragraph is the first element
HTML
<div class="chicken">
<p>paragraphy</p> [this is nth-child(1)]
<div class="big-chix">Contento</div> [this is nth-child(2)]
<div class="big-chix">Contento</div> [this is nth-child(3)]
<div class="big-chix">Contento</div> [this is nth-child(4)]
<div class="big-chix">Contento</div> [this is nth-child(5)]
<div class="big-chix">Contento</div> [this is nth-child(6)]
<div class="big-chix">Contento</div> [this is nth-child(7)]
</div>
CSS
.chicken { width:100%; background:#999; float:left; }
.big-chix { width:48%; margin:0.5%; padding:0.5%; background:#666; float:left; }
.big-chix:nth-child(1) { background-color:#eee; }
BUT, it will work in a parent element that has .big-chix
as the first element.
jsFiddle - nth-child with .big-chix
as the first element
HTML
<div class="chicken">
<div class="big-chix">Contento</div> [this is nth-child(1)]
<p>paragraphy</p> [this is nth-child(2)]
<div class="big-chix">Contento</div> [this is nth-child(3)]
<div class="big-chix">Contento</div> [this is nth-child(4)]
<div class="big-chix">Contento</div> [this is nth-child(5)]
<div class="big-chix">Contento</div> [this is nth-child(6)]
<div class="big-chix">Contento</div> [this is nth-child(7)]
</div>
CSS
.chicken { width:100%; background:#999; float:left; }
.big-chix { width:48%; margin:0.5%; padding:0.5%; background:#666; float:left; }
.big-chix:nth-child(1) { background-color:#eee; }
Upvotes: 2
Views: 226
Reputation: 609
use these
.big-chix:nth-child(even) { background-color:#eee; }
.big-chix:nth-child(odd) { background-color:#aaa; }
works in http://jsfiddle.net/TeqUF/2/
Upvotes: -2
Reputation: 943561
Isn't .big-chix:nth-child() only suppose to select all the .big-chix classes (which is 6 .big-chix), then set 1, 3, 5 to a background-color of #eee, and 2, 4, 6 to #aaa?
No.
:nth-child()
selects "The nth element in the parent", not "The nth element that also matches the other parts of the selector".
Each selector is applied independently and only elements that match all the components will match the complete selector.
Note, however, that there is :nth-of-type()
which should do what you want.
Upvotes: 9