<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Derek Price</title>
    <description>The latest articles on DEV Community by Derek Price (@derekprice).</description>
    <link>https://dev.to/derekprice</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F862095%2F000899c6-6f28-4a32-b89c-7acbc3606961.jpeg</url>
      <title>DEV Community: Derek Price</title>
      <link>https://dev.to/derekprice</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/derekprice"/>
    <language>en</language>
    <item>
      <title>nth-child and nth-last-child</title>
      <dc:creator>Derek Price</dc:creator>
      <pubDate>Fri, 13 May 2022 17:59:30 +0000</pubDate>
      <link>https://dev.to/derekprice/nth-child-and-nth-last-child-293o</link>
      <guid>https://dev.to/derekprice/nth-child-and-nth-last-child-293o</guid>
      <description>&lt;p&gt;Have you ever wanted to apply styles to certain child elements in your HTML but not others?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;nth-child&lt;/code&gt; and &lt;code&gt;nth-last-child&lt;/code&gt; may be exactly the CSS selectors that you need!&lt;/p&gt;

&lt;h3&gt;
  
  
  What do they do?
&lt;/h3&gt;

&lt;p&gt;These selectors make it possible for you to apply styles to specific children elements. Both of these selectors allow you to iterate over the child elements of an element and apply styles to any of them that match the argument you pass them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which one should I choose?
&lt;/h3&gt;

&lt;p&gt;There's one key difference between the two: the order in which they count the child elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;nth-child:&lt;/strong&gt; This selector applies styles to the list of children, starting with the &lt;em&gt;first&lt;/em&gt; child element until it reaches the last one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;nth-last-child:&lt;/strong&gt; This selector behaves the same way, with the only difference being that it starts with the &lt;em&gt;last&lt;/em&gt; child element and iterating until it reaches the first element.&lt;/p&gt;

&lt;h3&gt;
  
  
  What arguments can you pass these selectors?
&lt;/h3&gt;

&lt;p&gt;You can pass a few different types of arguments to each of the selectors. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a positive integer - &lt;code&gt;nth-child(3)&lt;/code&gt; and &lt;code&gt;nth-last-child(3)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;odd&lt;/code&gt;/&lt;code&gt;even&lt;/code&gt; - &lt;code&gt;nth-child(odd)&lt;/code&gt; and &lt;code&gt;nth-last-child(odd)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a mathematical expression in the form of &lt;code&gt;an+b&lt;/code&gt; - &lt;code&gt;nth-child(an+b)&lt;/code&gt; and &lt;code&gt;nth-last-child(an+b)&lt;/code&gt; (e.g. &lt;code&gt;2n-3&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of them take this mathematical expression that you provide and, starting with &lt;code&gt;n=0&lt;/code&gt; and increasing it by 1 as it iterates, returns a list of element indices (or positions in the children “array”) that it should apply to.&lt;/p&gt;

&lt;p&gt;Let's look at some examples and compare how each expression affects the elements for both &lt;code&gt;nth-child&lt;/code&gt; and &lt;code&gt;nth-last-child&lt;/code&gt; side-by-side.&lt;/p&gt;




&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;We’ll have 1 parent element and 10 child elements to start (all outlined containers with no background color), like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j0yAZRAw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k294q0h3xwo26b162crl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j0yAZRAw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k294q0h3xwo26b162crl.png" alt="Image description" width="880" height="83"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"parent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.parent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.parent&lt;/span&gt; &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;n&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="err"&gt;5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.child&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the sections below, any of the children that get selected will get &lt;code&gt;background-color: blue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: I've added numbers inside each of the boxes in the examples below &lt;em&gt;only&lt;/em&gt; to help visualize the order that CSS counts those elements. The actual order of the elements on the page is the same in both cases.&lt;/p&gt;

&lt;p&gt;To follow along and apply the values yourself, you can use &lt;a href="http://jsfiddle.net/zyv5p9e3/"&gt;this&lt;/a&gt; as a template.&lt;/p&gt;

&lt;h3&gt;
  
  
  3
&lt;/h3&gt;

&lt;p&gt;This is the most straightforward example, since we’re specifically choosing a certain element. No real “math” gets done here, we’re just picking “the 3rd child element from whichever end the selector starts on”.&lt;/p&gt;

&lt;p&gt;nth-child(3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--okjbxHdn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwl7m4ffrjgm65wy1esn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--okjbxHdn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwl7m4ffrjgm65wy1esn.png" alt="Image description" width="525" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;nth-last-child(3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aCKD1K3I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6csx6ps9s456f80oobm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aCKD1K3I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6csx6ps9s456f80oobm.png" alt="Image description" width="517" height="70"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  even
&lt;/h3&gt;

&lt;p&gt;Whenever the position of element is even, apply the styles!&lt;/p&gt;

&lt;p&gt;nth-child(even)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---n2VILQi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujruixgwg1tuvhx0a9ul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---n2VILQi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujruixgwg1tuvhx0a9ul.png" alt="Image description" width="514" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;nth-last-child(even)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OKq4yx5E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60wpg6m963vr5l3ivoae.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OKq4yx5E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60wpg6m963vr5l3ivoae.png" alt="Image description" width="514" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  odd
&lt;/h3&gt;

&lt;p&gt;And similarly, whenever the position of the element is odd, apply the styles.&lt;/p&gt;

&lt;p&gt;nth-child(odd)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UepJkd3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cb2f59hjzvwph4peopb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UepJkd3B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cb2f59hjzvwph4peopb7.png" alt="Image description" width="517" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;nth-last-child(odd)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3pFYJYSh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p43y8t0id6q7r2386x3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3pFYJYSh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p43y8t0id6q7r2386x3b.png" alt="Image description" width="514" height="69"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  n
&lt;/h3&gt;

&lt;p&gt;Let’s start using &lt;code&gt;n&lt;/code&gt;. This is where things get a little math-involved.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The iterator starts at &lt;code&gt;n=0&lt;/code&gt; and the value of the expression &lt;code&gt;n&lt;/code&gt; when &lt;code&gt;n=0&lt;/code&gt; is just 0. However, there is no “element 0”, so no styles get applied.&lt;/li&gt;
&lt;li&gt;The iterator increases to &lt;code&gt;n=1&lt;/code&gt;. The value of the expression &lt;code&gt;n&lt;/code&gt; when &lt;code&gt;n=1&lt;/code&gt; is 1. So it will apply the defined styles to the 1st element.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;n=2&lt;/code&gt;, the value of the expression &lt;code&gt;n&lt;/code&gt; is 2, so the 2nd element also gets styles applied.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As shown below, this is the case for every child element since &lt;code&gt;n&lt;/code&gt; just continues to positive infinity, so styles will be applied to &lt;em&gt;all&lt;/em&gt; child elements, regardless of how many you add.&lt;/p&gt;

&lt;p&gt;nth-child(n)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RhnMb4lZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz12hxe3ztbhqf291xsd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RhnMb4lZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz12hxe3ztbhqf291xsd.png" alt="Image description" width="524" height="150"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;nth-last-child(n)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--go4d7dNL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjpnux3021ebnjsxpfqc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--go4d7dNL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjpnux3021ebnjsxpfqc.png" alt="Image description" width="516" height="146"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h3&gt;
  
  
  n+1
&lt;/h3&gt;

&lt;p&gt;This is a slight variation from the one above, but all elements still get the styles applied. This is because when &lt;code&gt;n=0&lt;/code&gt; the value of &lt;code&gt;n+1&lt;/code&gt; becomes 1, meaning that the style gets applied to the 1st child, as shown below.&lt;/p&gt;

&lt;p&gt;nth-child(n+1)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PrOQcNF---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nywe0gtmmfkzv5dx79bl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PrOQcNF---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nywe0gtmmfkzv5dx79bl.png" alt="Image description" width="526" height="151"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;nth-last-child(n+1)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OE5Qbh8L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eikpncegn9uk44aj2ean.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OE5Qbh8L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eikpncegn9uk44aj2ean.png" alt="Image description" width="521" height="147"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h3&gt;
  
  
  n+3
&lt;/h3&gt;

&lt;p&gt;This is where we’ll start to see some variation. When &lt;code&gt;n=0&lt;/code&gt;, the value of &lt;code&gt;n+3&lt;/code&gt; is 3, meaning that the first child that gets styles applied is element 3. &lt;/p&gt;

&lt;p&gt;nth-child(n+3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k4628bcr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/218l2vvcmau8rjb49js4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k4628bcr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/218l2vvcmau8rjb49js4.png" alt="Image description" width="519" height="147"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;nth-last-child(n+3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UGv3vXpo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xa4l2h5gezcfvk6fkiev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UGv3vXpo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xa4l2h5gezcfvk6fkiev.png" alt="Image description" width="517" height="146"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h3&gt;
  
  
  n-1
&lt;/h3&gt;

&lt;p&gt;Similar to how &lt;code&gt;n+1&lt;/code&gt; applied to all elements, so does &lt;code&gt;n-1&lt;/code&gt;. When &lt;code&gt;n=0&lt;/code&gt;, &lt;code&gt;n-1&lt;/code&gt; is -1. When &lt;code&gt;n=1&lt;/code&gt;, &lt;code&gt;n-1&lt;/code&gt; is 0. Starting with &lt;code&gt;n=2&lt;/code&gt;, the value of &lt;code&gt;n-1&lt;/code&gt; is 1 and increases by 1 each time, so all child elements to infinity will get selected.&lt;/p&gt;

&lt;p&gt;nth-child(n-1)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d0IdklPH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjgu3h95o2lkyqr8euu0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d0IdklPH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjgu3h95o2lkyqr8euu0.png" alt="Image description" width="519" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;nth-last-child(n-1)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W9Wz9H9S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4k4b0kdts4vybbrnp1hp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W9Wz9H9S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4k4b0kdts4vybbrnp1hp.png" alt="Image description" width="515" height="144"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  n-3
&lt;/h3&gt;

&lt;p&gt;Just like above, &lt;code&gt;n=0&lt;/code&gt; to &lt;code&gt;n=3&lt;/code&gt; all equal to negative numbers or 0, so no elements get selected. But at &lt;code&gt;n=4&lt;/code&gt; (when &lt;code&gt;n-3&lt;/code&gt; equals 1) and above, all positive numbers get selected, so every element will get styles applied. &lt;/p&gt;

&lt;p&gt;nth-child(n-3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ErKt_WZ4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xqu5es4er2dn2olcbu1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ErKt_WZ4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xqu5es4er2dn2olcbu1.png" alt="Image description" width="518" height="153"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;nth-last-child(n-3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BBVrv1OF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igkv26i4ss20pg92cn8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BBVrv1OF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igkv26i4ss20pg92cn8y.png" alt="Image description" width="517" height="114"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h3&gt;
  
  
  3n
&lt;/h3&gt;

&lt;p&gt;Let’s add in an &lt;code&gt;a&lt;/code&gt; value! This becomes a multiplier that allows you to skip elements. When &lt;code&gt;n=0&lt;/code&gt; the value of &lt;code&gt;3n&lt;/code&gt; is 0, so no element gets selected. But when &lt;code&gt;n=1&lt;/code&gt;, the value of &lt;code&gt;3n&lt;/code&gt; is 3, so the 3rd element gets styled applied. Then the 6th (at &lt;code&gt;n=2&lt;/code&gt;) and the 9th (at &lt;code&gt;n=3&lt;/code&gt;) and so on.&lt;/p&gt;

&lt;p&gt;nth-child(3n)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YLeQRJOu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3hjnpdja57obgxerp0t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YLeQRJOu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3hjnpdja57obgxerp0t.png" alt="Image description" width="516" height="149"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;nth-last-child(3n)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qNFPl978--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fa2yijw7b76tzx80roi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qNFPl978--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fa2yijw7b76tzx80roi.png" alt="Image description" width="515" height="144"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h3&gt;
  
  
  3n+1
&lt;/h3&gt;

&lt;p&gt;Now let’s put it all together. If we add &lt;code&gt;+1&lt;/code&gt;, it takes the results of the expression &lt;code&gt;3n&lt;/code&gt; that we did above and shifts the element the styles get applied to. &lt;/p&gt;

&lt;p&gt;At &lt;code&gt;n=0&lt;/code&gt;, the value of &lt;code&gt;3n+1&lt;/code&gt; is 1. At &lt;code&gt;n=1&lt;/code&gt;, the value of &lt;code&gt;3n+1&lt;/code&gt; is 4. &lt;/p&gt;

&lt;p&gt;nth-child(3n+1)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cmbePyLt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgkcgmbcr3euy8pk0m40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cmbePyLt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgkcgmbcr3euy8pk0m40.png" alt="Image description" width="516" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;nth-last-child(3n+1)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PcKDHNkt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acc9ug37euo57tl2md03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PcKDHNkt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acc9ug37euo57tl2md03.png" alt="Image description" width="517" height="146"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  -n
&lt;/h3&gt;

&lt;p&gt;So far, we’ve only done positive values of &lt;code&gt;a&lt;/code&gt; . What if we make &lt;code&gt;a&lt;/code&gt; equal &lt;code&gt;-1&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;We saw that the expression &lt;code&gt;n&lt;/code&gt; resulted in &lt;em&gt;all&lt;/em&gt; elements getting selected. Inversely, the expression &lt;code&gt;-n&lt;/code&gt; results in &lt;em&gt;no&lt;/em&gt; elements getting selected. &lt;/p&gt;

&lt;p&gt;When &lt;code&gt;n=0&lt;/code&gt;, &lt;code&gt;-n&lt;/code&gt; equals 0. When &lt;code&gt;n=1&lt;/code&gt;, &lt;code&gt;-n&lt;/code&gt; equals -1. When &lt;code&gt;n=2&lt;/code&gt;, &lt;code&gt;-n&lt;/code&gt; equals -2. And so on. So no positive integers will ever get returned, meaning no elements will ever get selected.&lt;/p&gt;

&lt;p&gt;nth-child(-n)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--crdasLwR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y4nv9bj0juj95e976nbw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--crdasLwR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y4nv9bj0juj95e976nbw.png" alt="Image description" width="514" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;nth-last-child(-n)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fffqngy2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8nbli63uw763cgb2ee2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fffqngy2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8nbli63uw763cgb2ee2.png" alt="Image description" width="516" height="144"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h3&gt;
  
  
  -n+3
&lt;/h3&gt;

&lt;p&gt;If we add something to &lt;code&gt;-n&lt;/code&gt; however, we’ll be able to select some elements. &lt;/p&gt;

&lt;p&gt;At &lt;code&gt;n=0&lt;/code&gt;, &lt;code&gt;-n+3&lt;/code&gt; equals 3. At &lt;code&gt;n=1&lt;/code&gt;, &lt;code&gt;-n+3&lt;/code&gt; equals 2. At &lt;code&gt;n=2&lt;/code&gt;, &lt;code&gt;-n+3&lt;/code&gt; equals 1. But once &lt;code&gt;n=3&lt;/code&gt; or higher, the value of &lt;code&gt;-n+3&lt;/code&gt; becomes either 0 or negative, so those won’t select any of the child elements. &lt;/p&gt;

&lt;p&gt;This is a cool trick because it allows us to say I only want to select the:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(in the case of &lt;code&gt;nth-child&lt;/code&gt;) first  child elements&lt;/li&gt;
&lt;li&gt;(in the case of &lt;code&gt;nth-last-child&lt;/code&gt; ) last  child elements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;nth-child(-n+3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MOpCXCiy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvcjc73nhm50pcvhgcsr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MOpCXCiy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvcjc73nhm50pcvhgcsr.png" alt="Image description" width="518" height="146"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;nth-last-child(-n+3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6c8v78Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yqupkq0e150hygta00y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6c8v78Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yqupkq0e150hygta00y.png" alt="Image description" width="514" height="148"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h3&gt;
  
  
  -n-3
&lt;/h3&gt;

&lt;p&gt;What if we subtract from &lt;code&gt;-n&lt;/code&gt;? The result is very similar to the expression &lt;code&gt;-n&lt;/code&gt; itself, since no values of &lt;code&gt;n&lt;/code&gt; will ever result in &lt;code&gt;-n-3&lt;/code&gt; being positive. At &lt;code&gt;n=0&lt;/code&gt;, &lt;code&gt;-n-3&lt;/code&gt; is -3. At &lt;code&gt;n=1&lt;/code&gt;, &lt;code&gt;-n-3&lt;/code&gt; is -4. And so on.&lt;/p&gt;

&lt;p&gt;nth-child(-n-3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0WHjTfW_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ucf9fbvwo77pcd6cea7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0WHjTfW_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ucf9fbvwo77pcd6cea7d.png" alt="Image description" width="514" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;nth-last-child(-n-3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zs1mREOb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66pj08fh5w5pn0br4vsj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zs1mREOb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66pj08fh5w5pn0br4vsj.png" alt="Image description" width="522" height="146"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  -2n+6
&lt;/h3&gt;

&lt;p&gt;Let’s go back to adding to &lt;code&gt;-n&lt;/code&gt; but also add a multiplier. This will let us use that same pattern of “only the first x elements in the list”, but allow us to skip over some between them.&lt;/p&gt;

&lt;p&gt;At &lt;code&gt;n=0&lt;/code&gt;, &lt;code&gt;-2n+6&lt;/code&gt; equals 6, so the 6th element gets selected. At &lt;code&gt;n=1&lt;/code&gt;, &lt;code&gt;-2n+6&lt;/code&gt; equals 4. At &lt;code&gt;n=2&lt;/code&gt;, &lt;code&gt;-2n+6&lt;/code&gt; equals 2. Once you hit &lt;code&gt;n=3&lt;/code&gt;, the value of the expression is 0 and then following values are negative. So this becomes an effective way to say “For the first x styles in the list, apply styles to every other one.”&lt;/p&gt;

&lt;p&gt;nth-child(-2n+6)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mi3gAEKC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pw37webjj99zyqqanrol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mi3gAEKC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pw37webjj99zyqqanrol.png" alt="Image description" width="517" height="145"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;nth-last-child(-2n+6)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2GtuJX5u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5w0pu7g3irnm5zcplty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2GtuJX5u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5w0pu7g3irnm5zcplty.png" alt="Image description" width="519" height="147"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;The important thing to remember when thinking about &lt;code&gt;nth-child&lt;/code&gt; vs &lt;code&gt;nth-last-child&lt;/code&gt; is that given the same expression (e.g. &lt;code&gt;2n+3&lt;/code&gt;), the selectors will act very similarly. As you saw from each of the examples above, the images are basically mirrored versions of each other. &lt;/p&gt;

&lt;p&gt;The only difference is whether they apply styles from the beginning (nth-child) of the list of child elements or the end (nth-last-child). &lt;/p&gt;

&lt;p&gt;To learn more about these selectors with deeper technical explanations, see the &lt;a href="https://www.w3schools.com/cssref/sel_nth-child.asp#:~:text=Definition%20and%20Usage,name%2C%20of%20its%20parent."&gt;w3 documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading, and let me know if you have any questions in the comments!&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
