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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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="d-accordion">
  <dl>
    <dt id="accordionItem-1">
      <a class="d-accordionTitle" href="#accordionItem-1">First Accordion heading</a>
    </dt>
    <dd class="d-accordionItem animateOut is-collapsed">

      <p>
        This accordion uses the max-height attribute in conjunction with a CSS animation for opacity and scale to achieve the effect of stretching out and revealing. This is a great way to add motion to progressively disclosed content.
      </p>
      <p>

        The problem with max-height is that you need to know how tall the content will be, an alternative to this is to animate line-height.
      </p>

    </dd>

    <dt id="accordionItem-2">
      <a href="#accordionItem-2" class="d-accordionTitle">Second Accordion heading</a>
    </dt>

    <dd class="d-accordionItem animateOut is-collapsed">

      <p>
        This accordion uses the max-height attribute in conjunction with a CSS animation for opacity and scale to achieve the effect of stretching out and revealing. This is a great way to add motion to progressively disclosed content.
      </p>
      <p>

        The problem with max-height is that you need to know how tall the content will be, an alternative to this is to animate line-height.
      </p>


    </dd>

    <dt id="accordionItem-3">
      <a href="#accordionItem-3" class="d-accordionTitle">Third Accordion heading</a>
    </dt>

    <dd class="d-accordionItem animateOut is-collapsed">
      <p>
        This accordion uses the max-height attribute in conjunction with a CSS animation for opacity and scale to achieve the effect of stretching out and revealing. This is a great way to add motion to progressively disclosed content.
      </p>
      <p>

        The problem with max-height is that you need to know how tall the content will be, an alternative to this is to animate line-height.
      </p>

    </dd>
  </dl>
</div>
              
            
!

CSS

              
                .d-accordion {
  background: #fff;
  position: relative;
  max-width: 700px;
  margin: 0 auto;
}

.d-accordion dl {
  border: 1px solid #6c0543;
}
.d-accordion dl:after {
  background: #913256;
  content: "";
  display: block;
  height: 1em;
  width: 100%;
  font-size: 16px;
}

.d-accordion dd {
  margin-left: 0;
}

.d-accordion dd > p {
  color: #333;
  font: 400 0.8em/1.6 "brandon-grotesque", sans-serif;
  padding: 1em 2em 1em 2em;
}

.d-accordionTitle {
  background: #bc4572;
  border-bottom: 1px solid #913256;
  color: #fff;
  display: block;
  font: 700 1em/1.6 "brandon-grotesque", sans-serif;
  padding: 2em;
  position: relative;
  text-align: center;
  text-decoration: none;
  -webkit-transition: background 0.5s ease-in-out;
  transition: background 0.5s ease-in-out;
}
.d-accordionTitle:before {
  content: "\2715";
  float: left;
  font: 400 1em/1.6 "brandon-grotesque", sans-serif;
  -webkit-transform: rotate(225deg);
  -ms-transform: rotate(225deg);
  transform: rotate(225deg);
  -webkit-transition: -webkit-transform 320ms ease-in-out;
  transition: transform 320ms ease-in-out;
}
.d-accordionTitle:hover {
  background: #a43961;
}

.d-accordion-is-active {
  background: #a43961;
}
.d-accordion-is-active:before {
  -webkit-transform: rotate(0);
  -ms-transform: rotate(0);
  transform: rotate(0);
}

.d-accordionItem {
  height: auto;
  max-height: 50em;
  overflow: hidden;
  -webkit-transition: max-height 1s;
  transition: max-height 1s;
}
@media all and (min-width: 48em) {
  .d-accordionItem {
    max-height: 12em;
    -webkit-transition: max-height 0.4s;
    transition: max-height 0.4s;
  }
}

.is-collapsed {
  max-height: 0;
}

#accordionItem-1:target + .is-collapsed,
#accordionItem-2:target + .is-collapsed,
#accordionItem-3:target + .is-collapsed {
  -webkit-animation: accordionIn 0.65s 0s ease-in-out both 1;
  animation: accordionIn 0.65s 0s ease-in-out both 1;
  max-height: 12em;
}

#accordionItem-1:target a:before,
#accordionItem-2:target a:before,
#accordionItem-3:target a:before {
  -webkit-transform: rotate(-225deg);
  -ms-transform: rotate(-225deg);
  transform: rotate(-225deg);
}

.animateIn {
  -webkit-animation: accordionIn 0.65s 0s ease-in-out both 1;
  animation: accordionIn 0.65s 0s ease-in-out both 1;
}

.animateOut {
  -webkit-animation: accordionOut  0.75s 0s ease-in-out alternate both 1;
  animation: accordionOut  0.75s 0s ease-in-out alternate both 1;
}

@-webkit-keyframes accordionIn {
  0% {
    opacity: 0;
    -webkit-transform: scale(0.8);
    transform: scale(0.8);
  }

  100% {
    opacity: 1;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
}

@keyframes accordionIn {
  0% {
    opacity: 0;
    -webkit-transform: scale(0.8);
    transform: scale(0.8);
  }

  100% {
    opacity: 1;
    -webkit-transform: scale(1);
    transform: scale(1);
  }
}
@-webkit-keyframes accordionOut {
  0% {
    opacity: 1;
    -webkit-transform: scale(1);
    transform: scale(1);
  }

  100% {
    opacity: 0;
    -webkit-transform: scale(0.8);
    transform: scale(0.8);
  }
}
@keyframes accordionOut {
  0% {
    opacity: 1;
    -webkit-transform: scale(1);
    transform: scale(1);
  }

  100% {
    opacity: 0;
    -webkit-transform: scale(0.8);
    transform: scale(0.8);
  }
}

              
            
!

JS

              
                //javascript for accordion

( function( window ) {

'use strict';

// class helper functions from bonzo https://github.com/ded/bonzo

function classReg( className ) {

  return new RegExp("(^|\\s+)" + className + "(\\s+|$)");

}

// classList support for class management
// altho to be fair, the api sucks because it won't accept multiple classes at once
var hasClass, addClass, removeClass;

if ( 'classList' in document.documentElement ) {

  hasClass = function( elem, c ) {

    return elem.classList.contains( c );

  };

  addClass = function( elem, c ) {

    elem.classList.add( c );

  };

  removeClass = function( elem, c ) {

    elem.classList.remove( c );

  };
}
else {

  hasClass = function( elem, c ) {

    return classReg( c ).test( elem.className );

  };

  addClass = function( elem, c ) {

    if ( !hasClass( elem, c ) ) {

      elem.className = elem.className + ' ' + c;

    }

  };

  removeClass = function( elem, c ) {

    elem.className = elem.className.replace( classReg( c ), ' ' );

  };
}

function toggleClass( elem, c ) {

  var fn = hasClass( elem, c ) ? removeClass : addClass;
  fn( elem, c );

}

var classie = {
  // full names
  hasClass: hasClass,
  addClass: addClass,
  removeClass: removeClass,
  toggleClass: toggleClass,
  // short names
  has: hasClass,
  add: addClass,
  remove: removeClass,
  toggle: toggleClass
};

// transport
if ( typeof define === 'function' && define.amd ) {
  // AMD
  define( classie );
} else {
  // browser global
  window.classie = classie;
}

})( window );


var accordion = document.querySelector('.d-accordion');



//add event listener to all anchor tags with accordion title class
accordion.addEventListener( "click", function(e) {

  e.stopPropagation();
  e.preventDefault();

  if( e.target && e.target.nodeName == "A" ) {

    var classes = e.target.className.split(" ");

    if(classes) {

      for(var x = 0, len=classes.length; x < len; x++) {

        if(classes[x] == "d-accordionTitle") {

          var title = e.target;

          //next element sibling needs to be tested in IE8+ for any crashing problems
          var content = e.target.parentNode.nextElementSibling;
          
          //use classie to then toggle the active class which will then open and close the accordion
         
          classie.toggle(title, 'd-accordion-is-active');
          //this is just here to allow a custom animation to treat the content
          if(classie.has(content, 'is-collapsed')) {

            if(classie.has(content, 'animateOut')){

              classie.remove(content, 'animateOut');
              
            }

            classie.add(content, 'animateIn');

          } else {

             classie.remove(content, 'animateIn');
             classie.add(content, 'animateOut');
          }
          //remove or add the collapsed state
          classie.toggle(content, 'is-collapsed');



          
        }
      }
    }
    
  }
});
              
            
!
999px

Console