Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                *{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;

}

              
            
!

JS

              
                
              
            
!
999px

Console