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>Accessible Accordion</h1>

<dl class="accordion">
  <!-- Accordion Item 1 -->
  <dt>
    <button aria-expanded="false" aria-controls="section1" id="accordion-header-1">
      What is an accessible accordion?
      <span class="accordion-icon" aria-hidden="true">▼</span>
    </button>
  </dt>
  <dd id="section1" role="region" aria-labelledby="accordion-header-1" aria-hidden="true">
    An accessible accordion is a UI pattern that allows users to expand and collapse sections of content. It follows WAI-ARIA standards to ensure it's usable for keyboard and screen reader users.
  </dd>

  <!-- Accordion Item 2 -->
  <dt>
    <button aria-expanded="false" aria-controls="section2" id="accordion-header-2">
      Why is accessibility important?
      <span class="accordion-icon" aria-hidden="true">▼</span>
    </button>
  </dt>
  <dd id="section2" role="region" aria-labelledby="accordion-header-2" aria-hidden="true">
    Accessibility ensures that users with disabilities can use and interact with web content. It promotes inclusivity and helps you comply with accessibility laws like the ADA and European Accessibility Act.
  </dd>

  <!-- Accordion Item 3 -->
  <dt>
    <button aria-expanded="false" aria-controls="section3" id="accordion-header-3">
      How do I make an accordion accessible?
      <span class="accordion-icon" aria-hidden="true">▼</span>
    </button>
  </dt>
  <dd id="section3" role="region" aria-labelledby="accordion-header-3" aria-hidden="true">
    To make an accordion accessible, use semantic HTML and WAI-ARIA attributes. Ensure proper keyboard interaction (like "Enter" and "Space" keys to toggle items), announce state changes, and maintain focus order.
  </dd>
</dl>
              
            
!

CSS

              
                body {
  font-family: Arial, sans-serif;
  line-height: 1.5;
}

.accordion {
  border: 1px solid #ccc;
  border-radius: 5px;
  overflow: hidden;
}

.accordion dt {
  background-color: #00796b;
  color: white;
  padding: 1rem;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
}

.accordion dt button {
  all: unset;
  cursor: pointer;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.accordion dt button:focus {
  outline: 3px solid #ffbf47;
}

.accordion dd {
  margin: 0;
  padding: 1rem;
  display: none;
  border-bottom: 1px solid #ccc;
}

.accordion dd[aria-hidden="false"] {
  display: block;
}

.accordion-icon {
  transition: transform 0.3s ease;
}

.accordion-icon.open {
  transform: rotate(180deg);
}

              
            
!

JS

              
                const accordionButtons = document.querySelectorAll(".accordion dt button");

accordionButtons.forEach((button) => {
  button.addEventListener("click", () => {
    const isExpanded = button.getAttribute("aria-expanded") === "true";
    const icon = button.querySelector(".accordion-icon");
    const content = document.getElementById(
      button.getAttribute("aria-controls")
    );

    // Toggle the expanded state
    button.setAttribute("aria-expanded", !isExpanded);
    content.setAttribute("aria-hidden", isExpanded);

    // Update icon rotation
    if (!isExpanded) {
      icon.classList.add("open");
    } else {
      icon.classList.remove("open");
    }
  });

  button.addEventListener("keydown", (e) => {
    if (e.key === "Enter" || e.key === " ") {
      e.preventDefault();
      button.click();
    }
  });
});

              
            
!
999px

Console