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

              
                <nav aria-label="navigation menu">
  <ul>
    <li><a href="#">Home</a></li>
    <li class="selected">
      <a class="toggle" href="#submenu">Billing (click to expand/collapse)</a>
      <div class="toggle-content" id="submenu">
        <ul>
          <li><a href="#">My Bill</a></li>
          <li><a href="#">Pay History</a></li>
        </ul>
      </div>
    </li>
    <li><a href="#">Employees</a></li>
    <li><a href="#">Claims</a></li>
    <li><a href="#">Docs</a></li>
  </ul>
</nav>
              
            
!

CSS

              
                .toggle-content {
  display: none;
  height: 0;
  overflow: hidden;
  transition: height .3s ease-in-out;

  &.is-visible {
    display: block;
    height: auto;
  }
}














/* general styling */
:root {
  --border-width: 6px;
  --color-main-hover: #f2f6f9;
  --color-sub-hover: #d8ebf9;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Oxygen-Sans, Ubuntu, Cantarell, sans-serif;
  margin: 0;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;

  li {
    color: #404040;
    font-size: 1rem;
    letter-spacing: .5px;
    text-transform: uppercase;

    ul {
      padding: 0;  
    }

    li {
      text-transform: initial;
    }

    a {
      color: inherit;
      display: flex;
      padding: 1.25rem 2rem;
      text-decoration: none;

      &:hover {
        background-color: var(--color-main-hover);
      }
    }
  }
}

.selected {
  background-color: var(--color-main-hover);

  a {
    &:hover {
      background-color: var(--color-sub-hover);
    }
  }
}

.toggle {
  padding-left: calc(2rem - var(--border-width));
}

.toggle,
.toggle-content {
  border-left: var(--border-width) solid #015295;
}
              
            
!

JS

              
                // Show an element
const show = function (elem) {

  // Get the natural height of the element
  const getHeight = function () {
    elem.style.display = 'block'; // Make it visible
    const height = elem.scrollHeight + 'px'; // Get it's height
    elem.style.display = ''; //  Hide it again
    return height;
  };

  const height = getHeight(); // Get the natural height
  elem.classList.add('is-visible'); // Make the element visible
  elem.style.height = height; // Update the max-height

  // Once the transition is complete, remove the inline max-height so the content can scale responsively
  window.setTimeout(function () {
    elem.style.height = '';
  }, 350);

};

// Hide an element
const hide = function (elem) {

  // Give the element a height to change from
  elem.style.height = elem.scrollHeight + 'px';

  // Set the height back to 0
  window.setTimeout(function () {
    elem.style.height = '0';
    console.log('window.setTimeout');
  }, 1);

  // When the transition is complete, hide it
  window.setTimeout(function () {
    elem.classList.remove('is-visible');
    console.log('window.setTimeout:remove is-visible');
  }, 300);

};

// Toggle element visibility
const toggle = function (elem, timing) {

  // If the element is visible, hide it
  if (elem.classList.contains('is-visible')) {
    hide(elem);
    return;
    console.log('hide element');
  }

  // Otherwise, show it
  show(elem);
  console.log('show element');

};

// Listen for click events
document.addEventListener('click', function (event) {

  
  // Make sure clicked element is our toggle
  if (!event.target.closest('a').classList.contains('toggle')) {
    return;
  }

  // Prevent default link behavior
  event.preventDefault();

  // Get the content
  console.log(event.target.closest('a').hash);
  const content = document.querySelector(event.target.closest('a').hash);
  if (!content) return;

  // Toggle the content
  toggle(content);

}, false);
              
            
!
999px

Console