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

              
                <h1>🛠 Simple CSS+JS dynamic <code>max-height</code> accordions!</h1>

<h2>⚠️ The problem with <code>max-height</code> accordions</h2>
<p>I love the technique of using <code>max-height</code> to create accordions. <strong>Buuuuut</strong>, a big caveat is you can't transition between a <code>max-height</code> of <code>0</code> and <code>auto</code>. Leaving you to either:</p>
<ol>
  <li>Use something like <code>max-height: 999em</code> to account for the tallest content length possible. This causes your opening and closing animations to be too quick.</li>
  <li>Use a smaller value for <code>max-height</code>, but what if your content is too large and it "cuts off" part of it?</li>
</ol>

<h2>💪 transitionend to the rescue!</h2>
<p>You can add a <code>transitionend</code> eventListener to the accordion body (part that opens and closes) to dynamically calculate the <code>max-height</code> value! This event fires after a CSS transition completes. Meaning:</p>
<ul>
  <li>When the accordion is closed the content is positioned absolute so we can hide the content, but still get the <code>scrollHeight</code> value</li>
  <li>When clicking a closed accordion we, store the <code>scrollHeight</code> in a variable and briefly set the <code>max-height</code> to <code>0</code>. We then immediately set the <code>max-height</code> to the value of the <code>scrollHeight</code> on the absolutely positioned element.</li>
  <li>Once the transition is complete we remove the <code>max-height</code> so the content isn't capped at this value. Otherwise, if you resize your browser you might have overflow issues.</li>
  <li>When a user closes the accordion we immediately capture the height again, set the <code>max-height</code> to this value briefly and finally change the <code>max-height</code> back to <code>0</code> to close the accordion seamlessly.</li>
</ul>


<h2>🎉 Demo</h2>
<div class="Accordion" data-accordion>
  <div class="Accordion__title" data-title>
    <h3>Example Heading</h3>
  </div>
  <div class="Accordion__content" data-content>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Aperiam tempora voluptatum eligendi rem molestias numquam totam dignissimos animi ullam a culpa modi soluta, vero quisquam vel similique quibusdam neque nam?</p>
    <p>Ipsam dolorem ut similique ullam architecto eum corrupti dolor laboriosam neque error iure aliquam, inventore esse nemo, nisi laborum eaque, est officiis rem id. Doloremque velit excepturi eos tempore neque?</p>
    <p>Pariatur eius harum, quidem cumque corporis ipsa odio quo at, fugit atque inventore sit accusamus dolor unde? Porro ab nihil maxime sunt quasi? Asperiores animi, consectetur provident a aspernatur totam.</p>
    <p>Corrupti, voluptatem! Rem eveniet labore itaque velit amet vel sapiente qui harum quaerat aliquam veniam molestiae, corporis optio molestias magnam, nihil unde! Tempore voluptatem, quaerat sit neque repellat dignissimos earum?</p>
  </div>
</div>

              
            
!

CSS

              
                .Accordion {
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 1rem;
  position: relative;

  &__title {
    cursor: pointer;
    user-select: none;
  }

  &__title h3 {
    margin: 0;
  }

  &__content {
    overflow: hidden;
    transition: .2s max-height;
    will-change: max-height;
  }

  &.is-hidden &__content {
    position: absolute;
    opacity: 0;
    visibility: hidden;
  }
}


/*
 * Demo stuff
 */
body {
  padding: 2rem;
  line-height: 1.5;
}

code {
  background: #4A484C;
  color: #fff;
  border-radius: 5px;
  padding: 0 8px;
}
              
            
!

JS

              
                class Accordion {
  constructor($el) {
    this.$el = $el;
    this.$title = this.$el.querySelector('[data-title]');
    this.$content = this.$el.querySelector('[data-content]');
    this.isOpen = false;
    this.height = 0;

    this.events();
    this.close();
  }

  events() {
    this.$title.addEventListener('click', this.handleClick.bind(this));
    this.$content.addEventListener('transitionend', this.handleTransition.bind(this));
  }

  handleClick() {
    this.height = this.$content.scrollHeight;

    if (this.isOpen) {
      this.close();
    } else {
      this.open();
    }
  }

  close() {
    this.isOpen = false;
    this.$el.classList.remove('is-active');
    this.$content.style.maxHeight = `${this.height}px`;

    setTimeout(() => {
      this.$content.style.maxHeight = `${0}px`;
    }, 1);
  }

  open() {
    this.isOpen = true;
    this.$el.classList.add('is-active');
    this.$el.classList.remove('is-hidden');
    this.$content.style.maxHeight = `${0}px`;

    setTimeout(() => {
      this.$content.style.maxHeight = `${this.height}px`;
    }, 1);
  }

  handleTransition() {
    if (!this.isOpen) {
      this.$el.classList.add('is-hidden');
    }

    this.$content.style.maxHeight = '';
  }
}

/*
 * Instantiate a new Accordion
 */
new Accordion(document.querySelector('[data-accordion]'));
              
            
!
999px

Console