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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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="wrapper">

  <h2>Accordion Group 1</h2>
  <div class="accordion-wrapper">
    
    <div class="accordion-item">
      <button aria-expanded="false" aria-pressed="false">Accordion 1</button>
      <div class="accordion-content" aria-hidden="true">
        <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
      </div>  
    </div>
    <div class="accordion-item">
      <button aria-expanded="false" aria-pressed="false">Accordion 2</button>
      <div class="accordion-content" aria-hidden="true">
        <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
      </div>  
    </div>
    <div class="accordion-item">
      <button aria-expanded="false" aria-pressed="false">Accordion 3</button>
      <div class="accordion-content" aria-hidden="true">
        <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
      </div>  
    </div>
    
  </div>  

  <h2>Accordion Group 2</h2>
  <div class="accordion-wrapper">
    <div class="accordion-item">
      <button aria-expanded="false" aria-pressed="false">Accordion 1</button>
      <div class="accordion-content" aria-hidden="true">
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
      </div> 
    </div>
    <div class="accordion-item">
      <button aria-expanded="false" aria-pressed="false">Accordion 2</button>
      <div class="accordion-content" aria-hidden="true">
        <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
      </div>  
    </div> 
    <div class="accordion-item">
      <button aria-expanded="false" aria-pressed="false">Accordion 3</button>
      <div class="accordion-content" aria-hidden="true">
        <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
      </div>  
    </div>
    
    <div class="accordion-item">
      <button aria-expanded="false" aria-pressed="false">Accordion 4</button>
      <div class="accordion-content" aria-hidden="true">
        <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
      </div>  
    </div>    
    
  </div>  
  
</div>  
              
            
!

CSS

              
                .wrapper {
  max-width: 1140px;
  margin: 0 auto;
}

.accordion-wrapper {
  background-color: #fafafa;
}

.accordion-wrapper > .accordion-item > button {
  width: 100%;
  border: 0;
  text-align: left;
  padding: 15px;
  background-color: #f0f0f0;
  font-size: 16px;
  line-height: 16px;
  display: flex;
}

.accordion-wrapper > .accordion-item > button::before {
  content: '\203A';
  margin-right: 10px;
  font-size: 20px;
}

.accordion-wrapper > .accordion-item > button.active::before { 
  transform: rotate(90deg);
  align-items: center;
}

.accordion-wrapper > .accordion-item > .accordion-content {
  padding: 0 15px;
  font-size: 13px;
  line-height: 16px;
  display: none;
}

.accordion-item > .accordion-content.active {
   display: block;
}
              
            
!

JS

              
                // Use Immediately Invoked Function Express (IIFE)
// to keep JS scoped to this function
// and avoid overwriting of identifiers
(() => {
  const accordion = () => {
    /*
    Assign variable to 
    .accordion-wrapper > .accordion-item > button element
    to iterate through for loop below to add attributes
    to said button element and 
    sibling .accordion-wrapper > .accordion-item > .accordion-content
    element and
    add click event handler to said button element
    */
    const accordionBtn = document.querySelectorAll('.accordion-wrapper > .accordion-item > button');
    
    /*
    Assign a large random number (1000 to 1999) to a variable
    to use in the .accordion-wrapper > .accordion-item > button element
    for loop below to generate unique values
    for id, aria-controls, and aria-labelledby attributes
    for said button element and sibling .accordion-wrapper > .accordion-item > .accordion-content
    element
    */
    const largeRandNum = Math.floor( (Math.random() * 1000) + 1000 );

    // On click event handler that will be attached
    // to each .accordion-wrapper > .accordion-item > button element
    // in a for loop below
    const accordionBtnOnClick = (e) => {
      const accordionBtnClicked = e.target;
      // nextElementSibling property grabs the next HTML element node and
      // nextSibling property grabs the next node that code be a non-HTML element,
      // such as a line break
      const accordionContentSibling = accordionBtnClicked.nextElementSibling;
      
      /*
      IF accordionBtnClicked
      (.accordion-wrapper > .accordion-item > button element clicked on)
      has aria-expanded="false" set accessiblity (ARIA) properties for 
      attribute/value pair to make it's screen reader state readable
      as aria-expanded="true" (or "expanded")
      and aria-pressed="true" (or "pressed") and make
      accordionContentSibling screen reader readable (and not hidden)
      by adding aria-hidden="false" HTML attribute/value pair 
      and display: block CSS style
      
      ELSE make accordionBtnClicked aria-expanded="false",
      aria-pressed="false", and accordionContentSibling not 
      screen reader readable by adding aria-hidden="true"
      and removing the .active HTML class which
      adds a display: none CSS style
      */
      if(accordionBtnClicked.getAttribute('aria-expanded') === 'false') {   
        accordionBtnClicked.classList.add('active');          
        accordionBtnClicked.setAttribute('aria-expanded', 'true');
        accordionBtnClicked.setAttribute('aria-pressed', 'true');

        accordionContentSibling.classList.add('active'); 
        accordionContentSibling.setAttribute('aria-hidden', 'false');     
      } else {                 
        accordionBtnClicked.classList.remove('active');
        accordionBtnClicked.setAttribute('aria-expanded', 'false');
        accordionBtnClicked.setAttribute('aria-pressed', 'false');

        accordionContentSibling.classList.remove('active'); 
        accordionContentSibling.setAttribute('aria-hidden', 'true');       
      }
    };
  
    /*
    1.
    Iterate through the accordionBtn nodelist
    in a for loop (for IE10 and above support)
    
    2. 
    Inside the for loop:
    a.
    Add attributues id and aria-controls attribute/value pairs for the accordionBtn individual node 
    and get the accordionBtn sibling
    (.accordion-wrapper > .accordion-item > .accordion-content) and add the corresponding
    id and aria-labelledby attribute/value pairs
    
    b.
    Attach accordionBtnOnClick on click handler to each accordionBtn item
    to ensure the click event works
    */
    for(let i = 0; i < accordionBtn.length; i++) {
      const singleAccordionBtn = accordionBtn[i];
      const singleAccordionContent = singleAccordionBtn.nextElementSibling;
      
      const singleAccordionBtnId = 'accordion-btn-id-' + largeRandNum + '-' + [i];
      const singleAccordionContentId = 'accordion-content-id-' + largeRandNum + '-' + [i];
      
      singleAccordionBtn.setAttribute('id', singleAccordionBtnId);
      singleAccordionBtn.setAttribute('aria-controls', singleAccordionContentId);
      
      singleAccordionContent.setAttribute('id', singleAccordionContentId);
      singleAccordionContent.setAttribute('aria-labelledby', singleAccordionBtnId);
      
      singleAccordionBtn.addEventListener('click', accordionBtnOnClick);
    }

  };

  // Envoke the  accordion() method in a on load event handler
  // in case there as any images inside the .accordion-content elements
  // so they will fully load
  window.addEventListener('load', () => {
    accordion();
  });

})();
              
            
!
999px

Console