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

              
                <div class="accordion">
  <h2>
    <button id="accordion-trigger-1" aria-expanded="false" class="accordion__trigger" aria-controls="accordion-content-1">Accordion Title 1</button>

  </h2>
  <div class="accordion__content" id="accordion-content-1" aria-labelledby="accordion-trigger-1">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec at bibendum nunc. Etiam justo leo, suscipit non ex id, rhoncus viverra neque. Maecenas convallis ex vel arcu porta, ornare faucibus dolor iaculis. Ut rutrum felis eget rutrum commodo. Lorem
    ipsum dolor sit amet, consectetur adipiscing elit.
  </div>
  <h2>
    <button id="accordion-trigger-2" aria-expanded="false" class="accordion__trigger" aria-controls="accordion-content-2">Accordion Title 2</button>

  </h2>
  <div class="accordion__content" id="accordion-content-2" aria-labelledby="accordion-trigger-2">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec at bibendum nunc. Etiam justo leo, suscipit non ex id, rhoncus viverra neque. Maecenas convallis ex vel arcu porta, ornare faucibus dolor iaculis. Ut rutrum felis eget rutrum commodo. Lorem
    ipsum dolor sit amet, consectetur adipiscing elit.
  </div>
<h2>
    <button id="accordion-trigger-3" aria-expanded="false" class="accordion__trigger" aria-controls="accordion-content-3">Accordion Title 3</button>

  </h2>
  <div class="accordion__content" id="accordion-content-3" aria-labelledby="accordion-trigger-3">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec at bibendum nunc. Etiam justo leo, suscipit non ex id, rhoncus viverra neque. Maecenas convallis ex vel arcu porta, ornare faucibus dolor iaculis. Ut rutrum felis eget rutrum commodo. Lorem
    ipsum dolor sit amet, consectetur adipiscing elit.
  </div>
  
</div>
              
            
!

CSS

              
                body {
  font-family: Helvetica, Arial, Verdana;
}

.accordion {
  
   width: 400px;
  
  &__trigger {
    padding: 10px;
    padding-top: 20px;
    padding-bottom: 20px;
    background-color: #ccc;
    border-width: 0px;
    color: #000;
    font-size:1rem;
    display: block;
    width:100%;
    text-align: left;    
    border-bottom: 1px solid #666;
  }
  
  &__content {
    padding: 10px;
    line-height:1.2;
    display: none;
    
    &--expanded {
      display: block;
    }
  }
  
}
              
            
!

JS

              
                const triggers = document.querySelectorAll('.accordion__trigger');

triggers.forEach(
  function(item, index) {
    
    //
    // Add click handler
    //
    item.addEventListener(
      'click', 
      function() {
        return accordion_toggle(item);
      }
    );
    
    //
    // Add keyboard handlers
    // Borrowed from https://www.w3.org/TR/wai-aria-practices/examples/accordion/accordion.html
    //
    item.addEventListener(
      'keydown', 
      function(event) {
        
        let trigger_arr = Array.prototype.slice.call( triggers );
        let target = event.target;
        let key = event.which.toString();
        let ctrl_modifier = (event.ctrlKey && key.match(/33|34/));

        if (key.match(/38|40/) || ctrl_modifier) {
          let index = trigger_arr.indexOf(target);
          let direction = (key.match(/34|40/)) ? 1 : -1;
          let length = triggers.length;
          let newIndex = (index + length + direction) % length;

          trigger_arr[newIndex].focus();

          event.preventDefault();
        }
        else if (key.match(/35|36/)) {
          // 35 = End, 36 = Home keyboard operations
          switch (key) {
            // Go to first accordion
            case '36':
              trigger_arr[0].focus();
              break;
              // Go to last accordion
            case '35':
              trigger_arr[trigger_arr.length - 1].focus();
              break;
          }
          event.preventDefault();
        }
      }
    );
    
  }
);

function accordion_toggle(trigger_item) {
  let content_element = document.getElementById( trigger_item.getAttribute('aria-controls') );
  
  if ( content_element ) {
    if ( content_element.classList.contains('accordion__content--expanded') ) {
      trigger_item.setAttribute('aria-expanded', false);
      accordion_content_collapse(content_element);
    } 
    else {
      trigger_item.setAttribute('aria-expanded', true);
      accordion_content_expand(content_element);
    }
  }
}

function accordion_content_collapse(content_element) {
  
  if ( content_element ) {
    content_element.classList.remove('accordion__content--expanded');
  }
}

function accordion_content_expand(content_element) {

  if ( content_element ) {
    content_element.classList.add('accordion__content--expanded');
  }
}
              
            
!
999px

Console