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>
  <h1>One details element open at a time &ndash; An accordion</h1>
  
  <div class=flex>
  
    <div class=accordion>
      
      <!-- name attribute added 14/12/2023 for native accordian support in Safari 17.2, Chrome 120, Edge 120, but not 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: 1.5em;
}
p {
  margin-top: 1.5rem;
  text-align: center;
}
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;
}
              
            
!

JS

              
                // JS is required, falls back to default details behaviour

// Update 2023 from this article: https://developer.chrome.com/docs/css-ui/exclusive-accordion
// Unfortunately, it has serious amount of jank in Firefox
// Left in so you can try yourself

/*
document.querySelectorAll("details[name]").forEach(($details) => {
  $details.addEventListener("toggle", (e) => {
    const name = $details.getAttribute("name");

    if (e.newState == "open") {
      document
        .querySelectorAll(`details[name=${name}][open]`)
        .forEach(($openDetails) => {
          if (!($openDetails === $details)) {
            $openDetails.removeAttribute("open");
          }
        });
    }
  });
});
*/


// The original version still works well

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