Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed some explanation on HTML, nth-child
    primarykey
    data
    text
    <p><strong>NOTE: SEE BELOW FOR CLEARER EXPLANATION</strong></p> <p>I'm trying to figure out why this is happening.</p> <p><a href="http://jsfiddle.net/TeqUF/1/" rel="nofollow">jsFiddle 1 - Before</a></p> <p><strong>HTML</strong></p> <pre><code>&lt;div class="chicken"&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>CSS</strong></p> <pre><code>.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; } </code></pre> <p>What I'm trying to achieve here is to put a different background for <code>.big-chix</code> class for nth children 1, 3 , 5... and 2, 4, 6...</p> <p>But when I put in a paragraph (or anything else like a div, etc for that matter), it becomes like this:</p> <p><a href="http://jsfiddle.net/TeqUF/" rel="nofollow">jsFiddle 2 - After</a></p> <p><strong>HTML</strong></p> <pre><code>&lt;div class="chicken"&gt; &lt;p&gt;paragraphy&lt;/p&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>CSS</strong></p> <pre><code>.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; } </code></pre> <p>The nth-child placement switches places. Why is this so? Isn't <code>.big-chix:nth-child()</code> only suppose to select all the <code>.big-chix</code> classes (which is 6 <code>.big-chix</code>), then set 1, 3, 5 to a <code>background-color</code> of <code>#eee</code>, and 2, 4, 6 to <code>#aaa</code>?</p> <hr> <p>EDIT: From what I gather, <code>nth-child</code> will not apply to an element child in the element parent in a code like this:</p> <p><a href="http://jsfiddle.net/TeqUF/3/" rel="nofollow">jsFiddle - nth-child(1) when <code>&lt;p&gt;</code> paragraph is the first element</a></p> <p><strong>HTML</strong></p> <pre><code>&lt;div class="chicken"&gt; &lt;p&gt;paragraphy&lt;/p&gt; [this is nth-child(1)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(2)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(3)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(4)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(5)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(6)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(7)] &lt;/div&gt; </code></pre> <p><strong>CSS</strong></p> <pre><code>.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; } </code></pre> <p><strong>BUT</strong>, it will work in a parent element that has <code>.big-chix</code> as the first element.</p> <p><a href="http://jsfiddle.net/TeqUF/4/" rel="nofollow">jsFiddle - nth-child with <code>.big-chix</code> as the first element</a></p> <p><strong>HTML</strong></p> <pre><code>&lt;div class="chicken"&gt; &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(1)] &lt;p&gt;paragraphy&lt;/p&gt; [this is nth-child(2)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(3)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(4)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(5)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(6)] &lt;div class="big-chix"&gt;Contento&lt;/div&gt; [this is nth-child(7)] &lt;/div&gt; </code></pre> <p><strong>CSS</strong></p> <pre><code>.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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload