css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <link href="https://fonts.googleapis.com/css?family=Droid+Serif" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Droid+Sans" rel="stylesheet" type="text/css">

<h1>Repeating patterns with :nth-child</h1> 
<h2>Example #1</h2>
<div class="example ex1">
  
<!-- 256 divs --><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  
</div> <!-- END ex1-->  
<p>Repeating 3-item pattern...</p>
<code class="block">  
div:nth-child(3n+1){
  background: #FF7100; 
}
div:nth-child(3n+2){
    background: #FFB173; 
}
div:nth-child(3n+3){
    background: #A64A00; 
}
</code>
  <p>The equation passed into <code>nth-child</code> above is basically <code>(an+b)</code>, where <code>n={0,1,2,3...}</code> (that is, <code>n</code> is a set of all positive integers).</p> <p>So when we substitute in all the values of <code>n</code> then the first selector above evaluates to <code>nth-child(3*0+1)</code> or <code>nth-child(1)</code>, then <code>nth-child(3*1+1)</code> or <code>nth-child(4)</code>, then <code>nth-child(3*2+1)</code> or <code>nth-child(7)</code> and so on.</p><p>The second selector evaluates to <code>nth-child(3*0+2)</code> or <code>nth-child(2)</code>, then <code>nth-child(3*1+2)</code> or <code>nth-child(5)</code>, then <code>nth-child(3*2+2)</code> or <code>nth-child(8)</code> and so on.</p><p>And following the same pattern the 3rd selector results in <code>nth-child(3)</code>, <code>nth-child(6)</code>, <code>nth-child(9)</code>, and so on.</p>
  <p>Below are a couple more examples.</p>  
  
  <hr>
<h2>Example #2</h2>
<div class="example ex2">
  
<!-- 256 divs --><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  
</div> <!-- END ex2-->  
<p>Repeating 4-item pattern...</p>
<code class="block">  
div:nth-child(4n+1){
  background: #ccc; 
}
div:nth-child(4n+2){
    background: #999; 
}
div:nth-child(4n+3){
    background: #666; 
}
div:nth-child(4n+4){
    background: #333; 
}
</code>

<hr>
<h2>Example #3</h2>
<div class="example ex3">
  
<!-- 256 divs --><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
  
</div> <!-- END ex3-->  
<p>Repeating 7-item pattern...</p>
<code class="block">  
.ex3 div:nth-child(7n+1){
  background: red; 
}
.ex3 div:nth-child(7n+2){
    background: orange; 
}
.ex3 div:nth-child(7n+3){
    background: yellow; 
}
.ex3 div:nth-child(7n+4){
    background: green; 
}
.ex3 div:nth-child(7n+5){
  background: blue; 
}
.ex3 div:nth-child(7n+6){
    background: indigo; 
}
.ex3 div:nth-child(7n+7){
    background: violet; 
}
</code>
  <hr>
<h2>MDN says:</h2>
<blockquote>
<p>The :nth-child(an+b) CSS pseudo-class matches an element that has an+b-1 siblings before it in the document tree, for a given positive or zero value for n, and has a parent element.</p>
<p>This can more clearly be described this way: the matching element is the bth child of an element after all its children have been split into groups of a elements each.</p>
  <p>The values a and b must both be integers, and the index of an element's first child is 1.</p>
  <p>In other words, this class matches all children whose index fall in the set { an + b; n = 0, 1, 2, ... }.</p>
  <cite><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child">nth-child</a>, Mozilla Developer Network</cite>
</blockquote>
<p>Also more at CSS-Tricks: <a href="https://css-tricks.com/how-nth-child-works/">There is a CSS selector, really a pseudo-selector, called nth-child</a>.</p>
            
          
!
            
              *{box-sizing: border-box;}
html{background: #eee;}
body{
  max-width: 410px;
  margin: 10px auto;
  padding: 5px 40px;
  background: #fff;
  border: 5px dotted #ccc;
  font-family: 'Droid Sans', Helvetica, Arial, sans-serif;
}
h1, h2 {
  font-family: 'Droid Serif', Georgia, Times, serif;
}
h1{
  font-size: 1.8em;
  line-height: 1.2em;
}
h2{
  font-size: 1.4em;
}
.example{
  width: 320px; 
}
.example:after {
  content: "";
  display: table;
  clear: both;
}
.example div{
  border: 1px solid #fff;
  height: 20px;
  width: 20px;
  float: left;
}
.ex1 div:nth-child(3n+1){
  background: #FF7100; 
}
.ex1 div:nth-child(3n+2){
    background: #FFB173; 
}
.ex1 div:nth-child(3n+3){
    background: #A64A00; 
}

.ex2 div:nth-child(4n+1){
  background: #ccc; 
}
.ex2 div:nth-child(4n+2){
    background: #999; 
}
.ex2 div:nth-child(4n+3){
    background: #666; 
}
.ex2 div:nth-child(4n+4){
    background: #333; 
}

.ex3 div:nth-child(7n+1){
  background: red; 
}
.ex3 div:nth-child(7n+2){
    background: orange; 
}
.ex3 div:nth-child(7n+3){
    background: yellow; 
}
.ex3 div:nth-child(7n+4){
    background: green; 
}
.ex3 div:nth-child(7n+5){
  background: blue; 
}
.ex3 div:nth-child(7n+6){
    background: indigo; 
}
.ex3 div:nth-child(7n+7){
    background: violet; 
}
code{font-size: 1.2em;}
code.block{
  display: block;
  margin-top: 20px;
  padding: 0 20px 20px 20px;
  border: 1px solid #ccc;
  background: #eee; 
  white-space: pre-wrap;
  max-width: 320px;
}
hr{
  margin:3em .5em;
  border: 1px dotted #ccc;
}
blockquote{
  border-left: 2px dotted #ccc;
  margin-left: 0;
  padding-left: 1em; 
}
cite{
  display: block;
  text-align: right;
}
cite:before{
  content: "\2014";;  
}
p{
  margin-top: 20px;
  font-size: 1.1em;

}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console