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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div class="container">
  <h1>CSS-only Dropdown Menu Animation</h1>
  <ul class="menu">
    <li>
      <a href="#">Item 1</a>
      <ul class="sub-menu">
        <li><a href="#">Child 1</a></li>
        <li><a href="#">Child 2</a></li>
        <li><a href="#">Child 3</a></li>
        <li><a href="#">Child 4</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 2</a>
      <ul class="sub-menu">
        <li><a href="#">Child 1</a></li>
        <li><a href="#">Child 2</a></li>
        <li><a href="#">Child 3</a></li>
        <li><a href="#">Child 4</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 3</a>
      <ul class="sub-menu">
        <li><a href="#">Child 1</a></li>
        <li><a href="#">Child 2</a></li>
        <li><a href="#">Child 3</a></li>
        <li><a href="#">Child 4</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 4</a>
      <ul class="sub-menu">
        <li><a href="#">Child 1</a></li>
        <li><a href="#">Child 2</a></li>
        <li><a href="#">Child 3</a></li>
        <li><a href="#">Child 4</a></li>
      </ul>
    </li>
  </ul>
  
  <p>I used this technique on my personal blog, <a href="https://joshmccarty.com">https://joshmccarty.com</a>. The goal is to closely mimic an animated dropdown menu (typically done with JavaScript by animating the sub-menu's CSS <code>height</code>) with only CSS. Because you can't <code>transition</code> to <code>auto</code> values (i.e. the height), this uses a combination of transforms, opacity, and the clip property (instead of using <code>display: none;</code>) to hide/show the dropdown menu.</p>
</div>
              
            
!

CSS

              
                .container {
  margin: 2rem auto;
  padding: 0 1em;
  max-width: 60em;
  min-height: 10em;
}

// Menu styles
.menu {
  background-color: lighten(gray, 15%);
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  
  // Menu link styles
  a {
    background-color: gray;
    color: white;
    display: block;
    padding: 1em;
    text-decoration: none;
    
    &:hover {
      background-color: lighten(gray, 10%);
    }
  }
  
  // Top-level menu items
  > li {
    display: block;
    position: relative;
  }
}

// Sub-menu styles
.sub-menu {
  
  // These are the key CSS properties to create the effect
  clip: rect(1px, 1px, 1px, 1px);
  opacity: 0.1;
  transition: transform 0.1s cubic-bezier(0.2, 0, 0.2, 1), opacity 0.2s cubic-bezier(0.2, 0, 0.2, 1);
  transform: translateY(-10px) scaleY(0.5);
  transform-origin: top;
  
  // Typical sub-menu styling
  box-shadow: 5px 5px 5px rgba(black, 0.3);
  list-style: none;
  margin: 0;
  padding: 0;
  position: absolute;
  top: 100%;
  left: 0;
  width: 10em;
}

// Sub-menu styles when shown
.menu > li:hover > .sub-menu {
  clip: auto;
  opacity: 1;
  transform: translateY(0) scaleY(1);
}
              
            
!

JS

              
                
              
            
!
999px

Console