✦ A decade of Canvas craft, now driven by AI — describe it, watch it build live.Start building
Bootstrap 5Utilities

Bootstrap 5 Collapse

Bootstrap 5 Collapse is a JavaScript-powered utility that toggles the visibility of content by expanding and collapsing it with a smooth height transition. It is triggered by a button or anchor element using the data-bs-toggle='collapse' attribute paired with a target ID or href. Use it whenever you need to hide secondary content until the user requests it — such as FAQs, filter panels, or supplementary details — keeping the interface uncluttered without navigating away.

Primary Class

.collapse

Common Use Cases

  • FAQ sections where each question expands inline to reveal the answer, reducing page scroll length and letting users find answers without leaving the page
  • Product filter sidebars on e-commerce sites that collapse filter groups (size, colour, price) so mobile users aren't overwhelmed by a wall of options
  • Step-by-step onboarding checklists where each completed step collapses to show only the active task, giving users a sense of progress
  • Admin dashboard panels that hide advanced configuration settings behind a 'Show advanced options' toggle, keeping the default view clean for non-technical users

Variants & Classes

VariantDescription
Collapse DefaultStandard collapse with Bootstrap's default styling.
Collapse ResponsiveResponsive variant that adapts to different screen sizes.

Code Example

<p><button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#shippingDetails" aria-expanded="false" aria-controls="shippingDetails">View Shipping Information</button></p><div class="collapse" id="shippingDetails"><div class="card card-body"><h6 class="fw-semibold mb-2">Standard Shipping — 3 to 5 Business Days</h6><p class="mb-1">Free on all orders over £40. Orders placed before 2pm GMT are dispatched the same day.</p><p class="mb-0">Express next-day delivery is available at checkout for £4.99.</p></div></div>

Live Examples

Basic Collapse

Example 1

Canvas Framework Variants

The Canvas template extends Bootstrap 5 with 1,658+ component variants. Generate any of these using Canvas Builder:

  • Canvas Builder generated collapse with custom colours
  • Collapse with interactive states
  • Responsive collapse for all screen sizes

Best Practices

Use aria-expanded for accessible toggles

Always include aria-expanded='false' on your trigger element — Bootstrap automatically updates this to 'true' when the panel opens, giving screen readers accurate state information without any extra JavaScript.

Trigger one collapse from multiple buttons

You can point several buttons at the same collapse target by giving each the same data-bs-target value. This is useful for 'Read more' links that appear both above and below a content block.

Show content by default without JavaScript

Add the 'show' class alongside 'collapse' (i.e., class='collapse show') to render the panel open on page load. Users who disable JavaScript still see the content, and Bootstrap will manage the toggle state from that open position.

Animate height transitions with custom CSS

Bootstrap's collapse transition duration is set via the CSS variable --bs-transition-collapse-duration (defaults to 0.35s). Override it on your .collapse element to speed up or slow down the animation to match your brand's motion style.

FAQ

Can I have multiple collapse panels open at the same time on the same page?
Yes — by default each collapse panel is independent. If you want mutually exclusive panels where opening one closes another (accordion behaviour), wrap them in a parent element and use data-bs-parent='#parentId' on each .collapse element. Bootstrap will close any open sibling when a new one is triggered.
How do I customise the collapse animation speed and add a fade effect?
Override the CSS variable --bs-transition-collapse-duration on the .collapse or .collapsing selector in your stylesheet (e.g., .collapsing { --bs-transition-collapse-duration: 0.2s; }). Bootstrap does not add opacity fading by default, but you can layer a CSS transition on opacity within the .collapsing class to combine a fade with the height slide.
How does Canvas Builder handle Collapse components?
When you describe a collapsible section in Canvas Builder — such as 'FAQ block' or 'expandable filter panel' — the AI generates fully wired Bootstrap 5 Collapse markup including correct data attributes, ARIA roles, and unique IDs. It also applies your project's brand colours to the trigger buttons automatically, so the output is immediately usable without manual class editing.