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

              
                <!-- Note works with or without JS -->
<nav id='accordion' class='dropdown'>
  <input
    type='checkbox'
    id='toggle-01'
    class='toggle burger-toggle'
    hidden
  >
  <label for='toggle-01' class='toggle-button'>
    <div class='burger'>
      <div class='line1'></div>
      <div class='line2'></div>
      <div class='line3'></div>
    </div>
  </label>
  <ul class='dropdown-menu lvl-1'>
    <li class='item'><a><i class='bi bi-house-fill'></i> Home</a></li>
    <li class='item'><a>About</a></li>
    <li class='dropdown'>
      <input type='checkbox' id='toggle-02' class='toggle' hidden>
      <label for='toggle-02' class='toggle-button caret'><i class='bi bi-image'></i> Gallery </label>
      <ul class='dropdown-menu lvl-2'>
        <li class='dropdown'>
          <input type='checkbox' id='toggle-03' class='toggle' hidden>
          <label for='toggle-03' class='toggle-button caret'>3D Graphics </label>
          <ul class='dropdown-menu lvl-3'>
            <li class='item'><a>Characters</a></li>
            <li class='item'><a>Landscapes</a></li>
          </ul>
        </li>
        <li class='item'><a>2D Illustration</a></li>
      </ul>
    </li>
    <li class='dropdown'>
      <input type='checkbox' id='toggle-04' class='toggle' hidden>
      <label for='toggle-04' class='toggle-button caret'><i class='bi bi-code-slash'></i> Web Development </label>
      <ul class='dropdown-menu lvl-2'>
        <li class='item'><a>Articles</a></li>
        <li class='dropdown'>
          <input type='checkbox' id='toggle-05' class='toggle' hidden>
          <label for='toggle-05' class='toggle-button caret'>Projects </label>
          <ul class='dropdown-menu lvl-3'>
            <li class='item'><a>JS</a></li>
            <li class='item'><a>CSS</a></li>
          </ul>
        </li>
        <li class='item'><a>Tutorials</a></li>
      </ul>
    </li>
    <li class='item'><a><i class='bi bi-envelope-fill'></i> Contact</a></li>
  </ul>
</nav>
              
            
!

CSS

              
                @import "https://assets.codepen.io/3351103/__reset.css";
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500&display=swap');

:root {
  --nav-bg-color: rgb(21, 21, 21);
  --toggle-color: rgb(221, 221, 221);
  --toggle-bg-color: rgb(40 40 40);
  --nav-link-bg-color-01: rgb(50 50 50);
  --nav-link-bg-color-02: rgb(30 30 30);
  --nav-link-bg-color-03: rgb(15 15 15);
  --nav-link-color: hsl(0 0% 90%);
  --nav-secondary-color: rgb(255, 193, 7);
  --nav-caret-color: var(--nav-secondary-color);
  --nav-hover-color: hsl(0 0% 90%);
  --nav-bg-hover-color-01: rgb(209, 59, 45);
  
  --ff-body: 'Poppins', sans-serif;
  --ff-nav: 'Poppins', sans-serif;
  
  --fw-200: 200;
  --fw-300: 300;
  --fw-400: 400;
  --fw-500: 500;
  
  --fs-300: .9rem
  --fs-400: .95rem;
  --fs-500: 1.2rem;
  
  --calc-height: auto;
  --calc-transition-delay: 500ms; 
  --dropdown-indent: 0px;
  --burger-width: 40px;
  --mobile-break: 480px;
}

body {
  display: grid;
  justify-items: center;
  height: 100vh;
  padding: 1rem;
  font-family: var(--ff-body);
  font-weight: var(--fw-300);
  font-size: var(--fs-400);
  background-color: var(--nav-bg-color);
}

ul {
  list-style: none;
  margin: 0;
}

a {
  cursor: pointer;
}

#accordion {
  width: 100%;
  max-width: var(--mobile-break);
  height: fit-content;
  color: var(--nav-link-color);
  font-size: var(--fs-300);
  
  [hidden] {
    margin: 0;
  }
  
  a {
    display: block;
    padding: 0.75rem 0;
    font-family: var(--ff-nav);
    text-align: center;
    color: var(--nav-link-color);
    transition: color 150ms, background-color 150ms;
  }
  
  /* dropdown links level 1 */
  ul > li > a {
    background-color: var(--nav-link-bg-color-01);
  }

  /* dropdown links level 2 */
  ul ul > li > a {
    background-color: var(--nav-link-bg-color-02);
  }

  /* dropdown links level 3 */
  ul ul ul > li > a {
    background-color: var(--nav-link-bg-color-03);
  }
  
  ul > li > a:hover {
    color: var(--nav-hover-color);
    background-color: var(--nav-bg-hover-color-01);
  }
  
  .toggle-button {
    display: block;
    padding: 0.75rem 1rem;
    color: var(--toggle-color);
    background-color: var(--toggle-bg-color);
    cursor: pointer;
    user-select: none;
  }

  .dropdown-menu {
    position: relative;
    height: 0;
    overflow: hidden;
    padding-left: var(--dropdown-indent);
    transition: height var(--calc-transition-delay);
  }

  .toggle:checked ~ .dropdown-menu {
    height: var(--calc-height);
  }

  .caret:after {
    content: '\25BC';
    font-size: .9rem;
    line-height: 1;
    display: inline-block;
    color: var(--nav-caret-color);
    transform: rotatez(-90deg);
    transition: transform 150ms linear;
  }

  .toggle:checked + .caret:after {
    transform: rotatez(0deg);
  }
}

/* ---- burger toggle ----- */
nav {
  .burger {
    display: grid;
    place-content: center;
    width: var(--burger-width);
    height: calc(var(--burger-width) * .8);
    border: 2px solid var(--nav-secondary-color);
    border-radius: 4px;
    cursor: pointer;

    [class^='line'] {
      width: 26px;
      height: 2px;
      margin-bottom: 5px;
      background-color: var(--nav-secondary-color);
      transition: transform .2s ease-out;
    }
    
    .line3 {
      margin-bottom: 0px;
    }
  }
  
  .burger:hover {
    .line1{
      transform: translate(0px, 2px);
    }

    .line3 {
      transform: translate(0px, -2px);
    }
  }

  .burger-toggle:checked + .toggle-button {
    .burger {
      .line1 {
        transform: rotate(45deg) translate(5px, 5px);
      }

      .line2 {
        opacity: 0;
      }

      .line3 {
        transform: rotate(-45deg) translate(5px, -5px);
      }
    }
  }
}
/* ---- END burger toggle ----- */

/* ---- some custom styling ---- */
#accordion {
  --nav-link-bg-color-01: hsl(200 80% 13%);
  --nav-link-bg-color-02: hsl(200 80% 11%);
  --nav-link-bg-color-03: hsl(200 80% 9%);
  --nav-link-color: hsl(0 0% 90%);
  --nav-secondary-color: hsl(11 100% 65%);
  --nav-caret-color: hsl(11 100% 65%);
  --toggle-bg-color: hsl(200 80% 15%);
  
  --fs-300: .95rem;
  
  --burger-width: 36px;
  --mobile-break: 400px;
  
  .burger {
    margin: .15rem 0;
  }

  a {
    text-align: left;
  }
  
  .bi {
    margin-right: 5px;
  }
  
  .toggle:checked + .toggle-button .bi {
    color: var(--nav-secondary-color);
  }
  
  .lvl-1 a, .lvl-1 .toggle-button {
    padding-left: 1.5rem;
  }
  
  .lvl-2 a, .lvl-2 .toggle-button {
    padding-left: 2.5rem;
  }
  
  .lvl-3 a {
    padding-left: 3.5rem;
  }
}


              
            
!

JS

              
                window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 6000;

// searches next siblings for a target element
const findNextSibling = (elem, selector) => {
  while (elem !== null) {
    elem = elem.nextElementSibling
    if (elem.matches(selector)) return elem
  }
  return elem
}

// if unable to toggle between a and b or b and a
// will return false
const toggleBetween = (elem, a, b) => {
  return elem.classList.replace(a, b) || elem.classList.replace(b, a)
}

// A recursive function to set the total height of an open dropdown
// based on the heights of its child elements
const calcHeights = function (elems) {
  let height = 0
  
  // argument for first call will be a single root element
  // recursive calls will be children
  if (elems instanceof Element) elems = [elems]

  for (const elem of elems) {
    
    // only include calculations for opened dropdowns
    if (elem.matches('.dropdown, .open')) {
      const parentHeight = calcHeights(elem.children)

      if (elem.matches('.open')) {
        elem.style.setProperty('--calc-height', parentHeight + 'px')
      }

      height += parentHeight     
    } else {
      // getBoundingClientRect gives a more accurate height
      const elemHeight = elem.getBoundingClientRect().height
      
      height += (elem.matches('.closed')) ? 0 : elemHeight
    }
  }
  return height
}

const toggleMenu = (dropdown, rootElem) => { 
  if (!toggleBetween(dropdown, 'open', 'closed')) {
    // first time clicking on this dropdown
    dropdown.classList.add('open')
  }
  calcHeights(rootElem)
}

window.addEventListener('DOMContentLoaded', () => {
  const rootElem = document.querySelector('#accordion .dropdown-menu')
  
  // change --calc-height from auto to 0 for initial transition
  document
    .documentElement
    .style.setProperty('--calc-height', '0px')
  
  document
    .querySelectorAll('#accordion .toggle')
    .forEach((toggle) => {
      const dropdown = findNextSibling(toggle, '.dropdown-menu')
      
      toggle.addEventListener('click', (event) => {
        toggleMenu(dropdown, rootElem)
      })
    })
  
    document.querySelector('#toggle-01').click()
})

              
            
!
999px

Console