Bootstrap 5 Accordion: Collapsible Sections Without jQuery

An accordion is a stack of headers where clicking one expands its content and, usually, collapses the others. FAQ pages are the classic use. In Bootstrap 3 you built it from panels plus the collapse plugin. Bootstrap 5 has a dedicated .accordion component, and the old markup does not work with it at all.

Page setup

The accordion needs the JavaScript bundle for the collapse behavior. jQuery is no longer required.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap accordion</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <main class="container py-4">
    <!-- accordion goes here -->
  </main>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

The accordion

<div class="accordion" id="faq">
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button" type="button" data-bs-toggle="collapse"
              data-bs-target="#faq-html" aria-expanded="true" aria-controls="faq-html">
        What is HTML?
      </button>
    </h2>
    <div id="faq-html" class="accordion-collapse collapse show" data-bs-parent="#faq">
      <div class="accordion-body">
        HTML (HyperText Markup Language) is the standard markup language for web pages.
      </div>
    </div>
  </div>

  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
              data-bs-target="#faq-css" aria-expanded="false" aria-controls="faq-css">
        What is CSS?
      </button>
    </h2>
    <div id="faq-css" class="accordion-collapse collapse" data-bs-parent="#faq">
      <div class="accordion-body">
        CSS (Cascading Style Sheets) describes how a document written in a markup language is displayed.
      </div>
    </div>
  </div>

  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
              data-bs-target="#faq-bootstrap" aria-expanded="false" aria-controls="faq-bootstrap">
        What is Bootstrap?
      </button>
    </h2>
    <div id="faq-bootstrap" class="accordion-collapse collapse" data-bs-parent="#faq">
      <div class="accordion-body">
        Bootstrap is a popular HTML, CSS and JavaScript framework for building responsive websites.
      </div>
    </div>
  </div>
</div>

Three details decide whether the accordion behaves correctly:

  • The open section has .show on its collapse element, and its button has aria-expanded="true" and no .collapsed class. Every closed section has the opposite. If they disagree, the arrow icon points the wrong way until the first click.
  • data-bs-parent="#faq" is what makes opening one section close the others. It sits on each collapse element and points at the accordion’s id.
  • The trigger is a <button>. Bootstrap draws the chevron on it and handles the expanded state, and keyboard users can open it with Enter or Space.

Use whatever heading level fits your page outline for .accordion-header; <h2> is only Bootstrap’s default.

Converting from the Bootstrap 3 panel-group

The mapping is mostly one to one, with a few traps:

Bootstrap 3Bootstrap 5
.panel-group.accordion
.panel.panel-default.accordion-item
.panel-heading / .panel-title.accordion-header
<a data-toggle="collapse" href="#id"><button class="accordion-button" data-bs-toggle="collapse" data-bs-target="#id">
.panel-collapse.collapse.in.accordion-collapse.collapse.show
.panel-body.accordion-body

The one that catches most people: data-parent was set on the trigger in Bootstrap 3, while data-bs-parent goes on the collapsible element in Bootstrap 5. Move it to the wrong place and every section opens independently. The other is .in, which became .show in Bootstrap 4; a panel still marked .in starts out closed.

Variants

Always open. Remove data-bs-parent from every collapse element and sections no longer close each other. This is often the better choice for long FAQ pages, where users compare answers.

Flush. Add .accordion-flush next to .accordion to remove the outer borders and rounded corners, so the accordion sits edge to edge inside a card or sidebar.

Opening a section from JavaScript

To open the section that matches a link like faq.html#faq-css:

const target = location.hash && document.getElementById(location.hash.slice(1));

if (target?.classList.contains('accordion-collapse')) {
  bootstrap.Collapse.getOrCreateInstance(target, { toggle: false }).show();
}

The { toggle: false } option matters. A collapse instance toggles its element as soon as it is created, so without it, creating the instance and then calling hide() on a closed section would leave it open. With toggle: false, show() and hide() do exactly what they say. Bootstrap keeps aria-expanded and the .collapsed class on the button in sync either way.

Bootstrap raises show.bs.collapse and shown.bs.collapse events on the collapse element if you want to track which questions people open, or load a section’s content lazily the first time it is shown.

Related examples

Leave a Comment