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>Progressively enhanced, accessible accordions</h1>

<h2>2 accordions that supplement a help guide</h2>

<h3 class="accordion">I have some content to hide</h3>
<div class="accordion__panel">I am the content that should hide or display</div>

<h3 class="accordion">I have some more content to hide</h3>
<div class="accordion__panel">I am the other content that should hide or display</div>
              
            
!

CSS

              
                .accordion[data-open="false"] + .accordion__panel {
  display: none;
 }

.accordion[data-open="true"] + .accordion__panel {
  display: block;
}

/* Some styles to make it look like an actual accordion */
.accordion[data-open],
.accordion[data-open] + .accordion__panel {
  max-width: 65rem;
}

.accordion[data-open] {
  margin: 0 0 .5rem;
  border: 2px solid rebeccapurple;
  border-radius: 4px;
}

.accordion__btn {
  position: relative;
  display: flex;
  align-items: center;
  margin: 0;
  border: none;
  padding: 0.5rem 0.75rem;
  width: 100%;
  font-size: 1.75rem;
  text-align: left;
  font-weight: bold;
  background-color: #fff;
  color: rebeccapurple;
  cursor: pointer;
}

.accordion__btn:focus {
  outline: 3px solid transparent;
  outline-offset: 3px;
}

.accordion__btn:focus,
.accordion__btn:hover {
  background: rebeccapurple;
  color: #fff;
}

.accordion__btn::before {
  content: "";
  position: absolute;
  inset: -5px;
  border: 2px solid transparent;
  border-radius: 6px;
  z-index: 2;
}

.accordion__btn:focus-visible::before {
  border-color: rebeccapurple;
}

.accordion__btn::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border-style: solid;
  right: .75rem;
  font-size: 1.75rem;
  z-index: 1;
  color: rebeccapurple;
}

.accordion__btn[aria-expanded="false"]::after {
  border-color: transparent transparent transparent rebeccapurple;
  border-width: 6px 0 6px 14px;
}

.accordion__btn[aria-expanded="true"]::after {
  border-color: rebeccapurple transparent transparent transparent;
  border-width: 12px 7px 0 7px;
}

.accordion__btn[aria-expanded="false"]:focus::after,
.accordion__btn[aria-expanded="false"]:hover::after {
  border-color: transparent transparent transparent #fff;
}

.accordion__btn[aria-expanded="true"]:focus::after,
.accordion__btn[aria-expanded="true"]:hover::after {
  border-color: #fff transparent transparent transparent;
}

.accordion[data-open] + .accordion__panel {
  padding: .75rem .5rem;
  font-size: 1.25rem;
  line-height: 1.5;
}

@media (forced-colors: active) {
  .accordion__btn::before {
    display: none;
  }
  
  .accordion__btn[aria-expanded]::after {
    forced-color-adjust: none;
  }
  
  .accordion__btn[aria-expanded="false"]::after {
    border-color: transparent transparent transparent ButtonText;
  }
  
  .accordion__btn[aria-expanded="true"]::after {
    border-color: ButtonText transparent transparent transparent;
  }
}
              
            
!

JS

              
                const accordions = document.querySelectorAll('.accordion');

accordions.forEach((accordion, idx) => {
  const title = accordion.innerText;
  const panel = accordion.nextElementSibling;
  panel.id = `panel-${idx + 1}`
  accordion.innerHTML = `<button class="accordion__btn" aria-controls="panel-${idx + 1}" aria-expanded="false">${title}</button>`;
  
  const btn = accordion.firstElementChild;
  accordion.setAttribute('data-open', false);
  
  btn.addEventListener('click', () => {
    if (btn.getAttribute('aria-expanded') == 'false') {
      btn.setAttribute('aria-expanded', 'true');
      accordion.setAttribute('data-open', "true");
    } else {
      btn.setAttribute('aria-expanded', 'false');
      accordion.setAttribute('data-open', false);
    }
  });
});
              
            
!
999px

Console