Skip to main content

Components

Collapse

Show and hide content with a native details element; the height animates where the engine allows it.

How it works

A collapse is a <details class="collapse">. The <summary> is the trigger — style it as a button, a nav link or a list-group item — and whatever follows it is the panel. The open attribute is the state. No data-bs-toggle, no data-bs-target, no .show.

  • Baseline: <details> / <summary>. Every engine.
  • Enhancement: the panel's height (or width, for the horizontal variant) animates via ::details-content with interpolate-size: allow-keywords. Without it, the panel snaps open and closed.
  • Fallback: the legacy static classes still render: a <div class="collapse"> is hidden and .collapse.show is visible. They just cannot be toggled.
One trigger per panel. A <details> is toggled by its own <summary> and nothing else, so Bootstrap's "multiple targets" pattern (one button opening several panels, or several buttons for one panel) has no equivalent. See Multiple targets.

Example

Click the button. The summary carries the button classes; the panel is a .card.card-body for padding and a border.

Toggle content
Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the summary. The card above it stays where it is.
HTML
<details class="collapse">
  <summary class="btn btn-primary">Toggle content</summary>
  <div class="card card-body mt-3">
    Some placeholder content for the collapse component. This panel is hidden by default but revealed when the
    user activates the summary. The card above it stays where it is.
  </div>
</details>

Open on load with the open attribute.

Toggle content (open)
This one starts expanded. Remove the open attribute to start it closed.
HTML
<details class="collapse" open>
  <summary class="btn btn-outline-primary">Toggle content (open)</summary>
  <div class="card card-body mt-3">
    This one starts expanded. Remove the open attribute to start it closed.
  </div>
</details>

Any trigger style works: a link, a nav link, a plain summary.

Show the full card
  1. Opening contest — ten-minute time limit
  2. Tag team titles
  3. Steel cage — main event
HTML
<details class="collapse">
  <summary class="btn btn-link px-0">Show the full card</summary>
  <ol class="mt-2 mb-0">
    <li>Opening contest — ten-minute time limit</li>
    <li>Tag team titles</li>
    <li>Steel cage — main event</li>
  </ol>
</details>

Horizontal

Add .collapse-horizontal and the width animates instead of the height. Give the inner element an explicit width, as Bootstrap's version requires. Without ::details-content support the panel appears instantly, full width.

Toggle width collapse
This is some placeholder content for a horizontal collapse. It is hidden by default and shown when triggered.
HTML
<details class="collapse collapse-horizontal">
  <summary class="btn btn-primary">Toggle width collapse</summary>
  <div class="card card-body mt-3" style="width: 300px;">
    This is some placeholder content for a horizontal collapse. It is hidden by default and shown when
    triggered.
  </div>
</details>

Multiple targets

Bootstrap lets one button open two panels, or two buttons share one panel. Neither exists here, because a <details> has exactly one summary. Two workable alternatives:

  • Put all the content that should open together in one panel.
  • Use the accordion markup with a shared name if you want "open this one, close the others".

Two independent collapses side by side, each with its own trigger, is the honest version of the Bootstrap demo:

Each button opens only its own panel.

Toggle first element
Some placeholder content for the first collapse component of this example.
Toggle second element
Some placeholder content for the second collapse component of this example.
HTML
<p>Each button opens only its own panel.</p>
<div class="row">
  <div class="col">
    <details class="collapse">
      <summary class="btn btn-navy">Toggle first element</summary>
      <div class="card card-body mt-3">Some placeholder content for the first collapse component of this example.</div>
    </details>
  </div>
  <div class="col">
    <details class="collapse">
      <summary class="btn btn-navy">Toggle second element</summary>
      <div class="card card-body mt-3">Some placeholder content for the second collapse component of this example.</div>
    </details>
  </div>
</div>

Legacy static markup

Server-rendered state still works. A <div class="collapse"> without .show is hidden; with .show it is visible. There is no toggle.

Visible: a div with the classes collapse and show.
Hidden: a div with only the collapse class (you cannot see this one).
HTML
<div class="collapse show">
  <div class="card card-body">Visible: a div with the classes collapse and show.</div>
</div>
<div class="collapse">
  <div class="card card-body">Hidden: a div with only the collapse class (you cannot see this one).</div>
</div>

Accessibility

  • The <summary> is a native disclosure button. Do not add aria-expanded or aria-controls; engines expose them from the element's state.
  • Keyboard: Tab to the summary, Enter or Space to toggle.
  • Engines that support fragment navigation into a closed <details> open it automatically, so a link to an id inside the panel still lands on its target.
  • The animation is removed under prefers-reduced-motion: reduce.

CSS

Collapse has no component variables. The transition timing comes from the motion tokens.

$transition-collapse: height 320ms cubic-bezier(0.2, 0, 0, 1);         // $rb-duration-slow, $rb-ease-standard
$transition-collapse-width: width 320ms cubic-bezier(0.2, 0, 0, 1);

The rules from scss/components/_transitions.scss:

details.collapse                              // display: block
summary.btn, summary.nav-link, …              // marker removed, pointer cursor
details.collapse::details-content             // height: 0 → auto, animated (progressive)
details.collapse-horizontal::details-content  // width: 0 → auto, animated
.collapse:not(details):not(.show)             // legacy static div stays hidden