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

              
                <main lang=en>
  <!-- Updated March 2025 to animate in Chrome -->
  <h1>One details element open at a time &ndash; An accordion</h1>
  <p>Animated by CSS in that Chrome, others to follow.</p>
  
  <div class=flex>
  
    <div class=accordion>
      
      <!-- name attribute added 14/12/2023 for native accordian support in Safari 17.2, Chrome 120, Edge 120. And eventually Firefox -->

      <details name=accordian1>
        <summary>One</summary>
        <div aria-label=Yes>πŸ‘</div>
      </details>

      <details name=accordian1>
        <summary>Two</summary>
        <div aria-label="Yes, yes">πŸ‘πŸ‘</div>
      </details>

      <details name=accordian1>
        <summary>Three</summary>
        <div aria-label="Yes, yes, yes">πŸ‘πŸ‘πŸ‘</div>
      </details>

    </div>

    <div class=accordion lang=es>

      <details name=accordian2>
        <summary>Uno</summary>
        <div aria-label=Bueno>πŸ‘Œ</div>
      </details>

      <details name=accordian2>
        <summary>Dos</summary>
        <div aria-label="Bueno, bueno">πŸ‘ŒπŸ‘Œ</div>
      </details>

      <details name=accordian2>
        <summary>Tres</summary>
        <div aria-label="Bueno, bueno, bueno">πŸ‘ŒπŸ‘ŒπŸ‘Œ</div>
      </details>

    </div>

    <div class=accordion lang=fr>

      <details name=accordian3>
        <summary>Un</summary>
        <div aria-label="Tu gères">🀘</div>
      </details>

      <details name=accordian3>
        <summary>Deux</summary>
        <div aria-label="Tu gères, tu gères">🀘🀘</div>
      </details>

      <details name=accordian3>
        <summary>Trois</summary>
        <div aria-label="Tu gères, tu gères, tu gères">🀘🀘🀘</div>
      </details>

    </div>
    
  </div>
  
  <p>Also see: <a href="https://codepen.io/2kool2/pen/MWRLOrv?editors=1000" target="_self">Multi-level accordions</a></p>

</main>
              
            
!

CSS

              
                /* CSS not required, but makes the page purtie */

body {
  background-color: #1a1a1a;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 2056 2056' xmlns='http://www.w3.org/2000/svg'%3E%3CradialGradient id='g73'%3E%3Cstop offset='0%' stop-color='%23222'/%3E%3Cstop offset='100%' stop-color='%23000'/%3E%3C/radialGradient%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.19'/%3E%3C/filter%3E%3Crect width='2056' height='2056' fill='url(%23g73)' opacity='1.25'/%3E%3Crect width='2056' height='2056' filter='url(%23noiseFilter)' opacity='0.25'/%3E%3C/svg%3E");
  background-position: center;
  color: #fff;
  display: grid;
  font-family: sans-serif;
  font-size: 1.25rem;
  font-weight: 100;
  margin: 0;
  min-height: 100vh;
  place-content: center;
  text-shadow: 0 2px 2px #000;
}
* {
  margin: 0;
}
* + * {
  margin-top: 1rem;
}
main {
  display: block;
  margin: 0 1rem;
  min-height: 14rem;
}
h1 {
  font-size: 1.5rem;
  font-weight: 100;
  margin-bottom: 0;
}
p {
  font-size: 1rem;
  margin: 2rem auto;
  text-align: center;
}
h1 + p {
  margin:1rem auto 2rem;
}
a, a:visited {
  color: #fff;
  text-underline-offset: .15em;
}

.flex {
  display: flex;
  gap: 1rem;
}
.flex > div {
  margin: 0;
  width: 33%;
  border: 1px solid #333;
  background-color: rgb(0,0,0,.0625);
  padding: 0.25rem .5rem;
}

summary:focus-visible {
  outline: 2px solid #007aff;
  outline-offset: 8px;
}


/* 

Coming soon to all browsers not just Chrome.
Animating a details summary dropdown with CSS in a progressive enhancement manner which doesn't require any JavaScript.

14/03/2025 - Doesn't animate in Firefox or Safari yet. Still opens & closes but without the animation (progressive enhancement).

Hopefully, Interop 2025 will address the browser differences when it comes to details & summary. (I'm hoping the a11y issues in iOS Safari will finally be resolved!)

*/

:root {
  interpolate-size: allow-keywords;
}
details::details-content {
  block-size: 0;
  overflow: hidden;
  transition:
    block-size .3s ease-in-out,
    content-visibility .3s ease-in-out;
  transition-behavior: allow-discrete;
}
details[open]::details-content {
  block-size: auto;
}
summary {
  line-height: 1.3;
  margin-inline-start: 1rem;
  /* Maintain indent over multiple lines */
  list-style-position: outside;
  cursor: pointer;
  position: relative;
}
              
            
!

JS

              
                // As of 2025 JS is no longer needed as all modern browsers now support the name attribute on a details element.

// JS was only required to keep one element open at a time in older browsers. The fallback is to default details behaviour.


// Kept for archieval purposes:
/*
const accordions = (_ => {

  // Class used, but could be a data attribute or custom element
  const accordionQuery = '.accordion';

  const accordionClicked = event => {
    const summary = event.target;
    if (summary.tagName.toLowerCase() !== 'summary') return;

    const currentDetails = summary.parentElement;
    const accordion = currentDetails.closest(accordionQuery);

    const allOpenDetails = accordion.querySelectorAll('details[open]');
    for (const details of allOpenDetails) {

      // But not the clicked summary
      if (details === currentDetails) continue;

      details.removeAttribute('open');
    }
  };

  // Consider each group of details (individual accordian)
  const accordions = document.querySelectorAll(accordionQuery);
  for (const accordion of accordions) {
    accordion.addEventListener('click', accordionClicked);
  }

})();

// */
              
            
!
999px

Console