Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="article">
  <h1>Create sections with alternating backgrounds using <code>&lt;hr&gt;</code></h1>
  <hr>
  <p>Every time an <code>&lt;hr&gt;</code> element is added, the background alternates creating awesome sections.</p>
  <hr>
  <p>This could be used as an easy way to create sections without additional divs and markup in, say, a WordPress theme.</p>
  <hr>
  <p>This <del>does not work in any version of</del> now works in Internet Explorer 9+ and falls back nicely.</p>
  <hr>
  <h2>Here's an example of a section</h2>
  <p> A paragraph. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Donec sed odio dui. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Maecenas faucibus mollis interdum.</p>
  <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec sed odio dui. Maecenas sed diam eget risus varius blandit sit amet non magna. Vestibulum id ligula porta felis euismod semper.</p>
  <hr>
  <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/5247/city.jpg" alt="city" />
  <hr>
  <h2>Here's an example of another section</h2>
  <img class="align-left" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/5247/city.jpg" alt="city" />
  <p> A paragraph. Nullam id dolor id nibh ultricies vehicula ut id elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.</p>
  <p>Aenean lacinia bibendum nulla sed consectetur. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Sed posuere consectetur est at lobortis. Curabitur blandit tempus porttitor. Integer posuere erat a ante venenatis dapibus posuere velit aliquet.</p>
  <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Nulla vitae elit libero, a pharetra augue. Etiam porta sem malesuada magna mollis euismod. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Donec id elit non mi porta gravida at eget metus.</p>
  <select name="select">
    <option value="value1">Value 1</option> 
    <option value="value2" selected>Value 2</option>
    <option value="value3">Value 3</option>
    <option value="value1">Value 4</option> 
    <option value="value2">Value 5</option>
    <option value="value3">Value 6</option>
  </select>
  <hr>
  <h3>You're welcome.</h3>
</div>
              
            
!

CSS

              
                // Base styles (unimportant)
body {
  background: #7db1ff;
  color: #333;
  font: 18px/1.5 Helvetica, Arial, sans-serif;
}

.article {
  max-width: 560px;
  margin: 30px auto;
  padding: 30px 30px;
  box-sizing: border-box;
  overflow: hidden;

  
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #fff;
    z-index: -1;
  }
}

h1 {
  margin: 0;
  padding: 0;
  font-size: 24px;
  text-align: center;
}

p {
  margin: 0 0 1em;
}

img {
  display: block;
  max-width: 100%;
}

.align-left {
  float: left;
  max-width: 35%;
  margin: 0 30px 15px 0;
}

// The magic (separated for easy of understanding)
.article {
  position: relative;
  background: #ffffff; // fallback bg for IE 8
  background: none, none; // removes fallback on newer browsers
  
  // The background color cannot be set on article or section backgrounds will be covered. I'm using the :before pseudo element instead to acheive the same result.
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #fff;
    z-index: -1;
  }
}

hr {
  height: 0;
  margin: 30px -30px;
  border: 0;
  border-bottom: 1px solid #eee;
  position: relative;
  overflow: visible;

  // Also uses a pseudo element to set the background for each section.
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 9999em;
    background: #fff;
    z-index: -1;
  }

  &:nth-of-type(odd){
    &:before {
      background: #fafafa;
    }
  }
}

              
            
!

JS

              
                
              
            
!
999px

Console