TExcelTAD
Home › Bootstrap › BS5 Collapse

Bootstrap 5 Collapse

The collapse plugin shows and hides content with a sliding animation. It powers "Read more" sections, FAQ accordions and the mobile navbar menu. It needs Bootstrap's JavaScript.

Basic collapse

A trigger points to the content with data-bs-toggle="collapse" and a target. The content has the class .collapse (hidden); Bootstrap adds .show when it opens.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <p>
    <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#details"
            aria-expanded="false" aria-controls="details">Show details</button>
    <a class="btn btn-outline-primary" data-bs-toggle="collapse" href="#details" role="button"
       aria-expanded="false" aria-controls="details">Link trigger</a>
  </p>
  <div class="collapse" id="details">
    <div class="card card-body">
      📦 Ships in 2–3 days. Free returns within 30 days.
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

A <button> uses data-bs-target="#id"; a link can use href="#id" instead. Bootstrap keeps aria-expanded in sync for you.

Attributes and classes

Item Meaning
.collapse Hidden content
.collapse.show Visible content (add show to start open)
.collapsing Applied during the animation
.collapse-horizontal Animate width instead of height (child needs a fixed width)
data-bs-toggle="collapse" Makes an element a trigger
data-bs-target / href Selector of the content to toggle (can match several elements)
data-bs-parent Accordion behaviour: close siblings inside this parent

Toggling several targets

data-bs-target takes any CSS selector, so one button can toggle multiple elements at once.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button class="btn btn-dark mb-2" data-bs-toggle="collapse" data-bs-target=".tips" aria-expanded="false">Toggle all tips</button>
  <div class="row g-2">
    <div class="col-6"><div class="collapse tips show"><div class="alert alert-info mb-0">Tip A (starts open)</div></div></div>
    <div class="col-6"><div class="collapse tips"><div class="alert alert-warning mb-0">Tip B (starts closed)</div></div></div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Each target toggles independently, so the open one closes and the closed one opens.

Accordion

An accordion is a stack of collapsible panels where opening one closes the others. Bootstrap has dedicated .accordion classes, and data-bs-parent provides the "only one open" behaviour.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <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="#q1" aria-expanded="true" aria-controls="q1">
          Is Bootstrap free?
        </button>
      </h2>
      <div id="q1" class="accordion-collapse collapse show" data-bs-parent="#faq">
        <div class="accordion-body">Yes — it's open source under the MIT licence.</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="#q2" aria-expanded="false" aria-controls="q2">
          Do I need jQuery?
        </button>
      </h2>
      <div id="q2" class="accordion-collapse collapse" data-bs-parent="#faq">
        <div class="accordion-body">No. Bootstrap 5 uses plain JavaScript.</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="#q3" aria-expanded="false" aria-controls="q3">
          Does it support dark mode?
        </button>
      </h2>
      <div id="q3" class="accordion-collapse collapse" data-bs-parent="#faq">
        <div class="accordion-body">Yes, since 5.3 via <code>data-bs-theme</code>.</div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Remove data-bs-parent to let several panels stay open at the same time. Add .accordion-flush to the wrapper for an edge-to-edge look with no outer borders.

Accordion class Part
.accordion Wrapper
.accordion-item One panel
.accordion-header Heading containing the button
.accordion-button (+ .collapsed when closed) The clickable header
.accordion-collapse The collapsing region
.accordion-body Padded content

Collapse from JavaScript

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <button class="btn btn-primary" id="btn">Toggle with JS</button>
  <div class="collapse mt-2" id="box"><div class="card card-body">Controlled by the Collapse API.</div></div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    const box = document.getElementById('box');
    const c = new bootstrap.Collapse(box, { toggle: false });
    document.getElementById('btn').addEventListener('click', () => c.toggle());
    box.addEventListener('shown.bs.collapse', () => console.log('opened'));
    box.addEventListener('hidden.bs.collapse', () => console.log('closed'));
  </script>
</body>
</html>

Tip: Don't put padding or margin directly on a .collapse element — the height animation can jump. Put an inner wrapper (like the .card above) inside it instead.

Test yourself

Which class does Bootstrap add to a .collapse element when it is open?

  1. .open
  2. .show
  3. .active
  4. .in

What gives an accordion its "only one panel open" behaviour?

  1. data-bs-parent on each collapsing panel
  2. .accordion-single on the wrapper
  3. data-bs-toggle="accordion"
  4. It's automatic for all collapses

Which class should an accordion button have while its panel is closed?

  1. .closed
  2. .collapsed
  3. .hidden
  4. .inactive