Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As the name implies, <code>:<b>n</b>th-child()</code> allows you to construct an arithmetic expression using the <code>n</code> variable in addition to constant numbers. You can perform addition (<code>+</code>), subtraction (<code>-</code>) and <a href="http://en.wikipedia.org/wiki/Coefficient" rel="noreferrer">coefficient multiplication</a> (<code>an</code> where <code>a</code> is an integer, including positive numbers, negative numbers and zero).</p> <p>Here's how you would rewrite the above selector list:</p> <pre><code>div:nth-child(4n) </code></pre> <p>For an explanation on how these arithmetic expressions work, see my answer to <a href="https://stackoverflow.com/questions/11576324/understanding-nth-childan-b-selector-with-formula-in-css3">this question</a>, as well as the <a href="http://www.w3.org/TR/selectors/#nth-child-pseudo" rel="noreferrer">spec</a>.</p> <p>Note that this answer assumes that all of the child elements within the same parent element are of the same element type, <code>div</code>. If you have any other elements of different types such as <code>h1</code> or <code>p</code>, you will need to use <code>:nth-of-type()</code> instead of <code>:nth-child()</code> to ensure you only count <code>div</code> elements:</p> <pre><code>&lt;body&gt; &lt;h1&gt;&lt;/h1&gt; &lt;div&gt;1&lt;/div&gt; &lt;div&gt;2&lt;/div&gt; &lt;div&gt;3&lt;/div&gt; &lt;div&gt;4&lt;/div&gt; &lt;h2&gt;&lt;/h2&gt; &lt;div&gt;5&lt;/div&gt; &lt;div&gt;6&lt;/div&gt; &lt;div&gt;7&lt;/div&gt; &lt;div&gt;8&lt;/div&gt; &lt;h2&gt;&lt;/h2&gt; &lt;div&gt;9&lt;/div&gt; &lt;div&gt;10&lt;/div&gt; &lt;div&gt;11&lt;/div&gt; &lt;div&gt;12&lt;/div&gt; &lt;h2&gt;&lt;/h2&gt; &lt;div&gt;13&lt;/div&gt; &lt;div&gt;14&lt;/div&gt; &lt;div&gt;15&lt;/div&gt; &lt;div&gt;16&lt;/div&gt; &lt;/body&gt; </code></pre> <p>For everything else (classes, attributes, or any combination of these), where you're looking for the nth child that matches an arbitrary selector, you will not be able to do this with a pure CSS selector. See my answer to <a href="https://stackoverflow.com/questions/5545649/css3-nth-child-or-nth-of-type-with-a-class-selector">this question</a>.</p> <hr> <p>By the way, there's not much of a difference between 4n and 4n + 4 with regards to <code>:nth-child()</code>. If you use the <code>n</code> variable, it starts counting at 0. This is what each selector would match:</p> <p><strong><code>:nth-child(4n)</code></strong></p> <pre><code>4(0) = 0 4(1) = 4 4(2) = 8 4(3) = 12 4(4) = 16 ... </code></pre> <p><strong><code>:nth-child(4n+4)</code></strong></p> <pre><code>4(0) + 4 = 0 + 4 = 4 4(1) + 4 = 4 + 4 = 8 4(2) + 4 = 8 + 4 = 12 4(3) + 4 = 12 + 4 = 16 4(4) + 4 = 16 + 4 = 20 ... </code></pre> <p>As you can see, both selectors will match the same elements as above. In this case, there is no difference.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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