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

              
                <!-- ########################################## -->
<!-- How things work in one stacking context -->
<!-- ########################################## -->

<div class="container">
  <h1>No z-index</h1>
  <p>The layers below have no z-indexes set on them, and they're all positioned absolutely within the context of the layer-container.</p><p>Because there's no z-index they're all part of the same stacking context, they layer based on DOM order—last inserted goes on top.</p>
  <div class="layer-container nozindex">
    <div class="bottom">
      <div class="box red">bottom</div>
    </div>
    <div class="middle">
      <div class="box blue">middle</div>
    </div>
    <div class="top">
      <div class="box green">top</div>
    </div>  
  </div>  
</div>

<!-- ########################################## -->
<!-- How z-indexes work -->
<!-- ########################################## -->

<div class="container">
  <h1>With z-index</h1>
  <p>The layers below have z-indexes set on them to reverse the order of stacking within the context of the layer-container.</p><p>Because they're all part of the same stacking context, they layer based on the how each of their z-indexes compare (larger z-indexes go on top).</p>
  <div class="layer-container nozindex">
    <div class="bottom">
      <div class="z-index-3 box red">bottom</div>
    </div>
    <div class="middle">
      <div class="z-index-2 box blue">middle</div>
    </div>
    <div class="top">
      <div class="z-index-1 box green">top</div>
    </div>  
  </div>  
</div>

<!-- ########################################## -->
<!-- Creating new stacking contexts -->
<!-- ########################################## -->

<div class="container">
  <h1>Creating new stacking contexts to prevent z-index issues</h1>
  <p>The layers below have z-indexes set on them to reverse the order of stacking within the context of the layer-container.</p><p>The containers of those layers are set to create</p><p>Because they're all part of the same stacking context, they layer based on the how each of their z-indexes compare (larger z-indexes go on top).</p>
  <div class="layer-container nozindex">
    <div class="new-stacking-context bottom">
      <div class="z-index-3 box red">bottom</div>
    </div>
    <div class="new-stacking-context middle">
      <div class="z-index-2 box blue">middle</div>
    </div>
    <div class="new-stacking-context top">
      <div class="z-index-1 box green">top</div>
    </div>  
  </div>  
</div>


              
            
!

CSS

              
                .box {
  width: 100px;
  height: 100px;
  color: white;
  padding: 16px;
  position: absolute;
}

/**********************************************
We set position relative so that position: absolute
on the boxes is relative to this context.
************************************************/

.layer-container {
  position: relative;
  z-index: 0;
}

.red {
  background-color: red;
  top: 16px;
  left: 16px;
}

.blue {
  background-color: blue;
  top: 32px;
  left: 32px;
}

.green {
  background-color: green;
  top: 48px;
  left: 48px;
}

.z-index-1 {
  z-index: 1;
}

.z-index-2 {
  z-index: 2;
}

.z-index-3 {
  z-index: 3;
}

/**********************************************
There are many ways of creating new stacking contexts.
You can see some of them here:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context#The_stacking_context

For this example we're using:
> Element with a position value "absolute" or "relative" and z-index value other than "auto".
************************************************/

.new-stacking-context {
  position: relative;
  z-index: 0;
}

/* ########################################## */
/* unimportant document styles */
/* ########################################## */

body {
  display: flex;
  padding: 24px;
}

.container {
  margin-right: 72px;
  width: 320px;
}
              
            
!

JS

              
                
              
            
!
999px

Console