| title | Collapse |
|---|---|
| description | Toggle the visibility of content across your project with a few classes and our JavaScript plugins. |
| toc | true |
The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from its current value to 0. Given how CSS handles animations, you cannot use padding on a .collapse element. Instead, use the class as an independent wrapping element.
Click the buttons below to show and hide another element via class changes:
.collapsehides content.collapsingis applied during transitions.collapse.showshows content
Generally, we recommend using a <button> with the data-bs-target attribute. While not recommended from a semantic point of view, you can also use an <a> link with the href attribute (and a role="button"). In both cases, the data-bs-toggle="collapse" is required.
<Example code={`
Link with href Button with data-bs-target
The collapse plugin supports horizontal collapsing. Add the .collapse-horizontal modifier class to transition the width instead of height and set a width on the immediate child element. Feel free to write your own custom Sass, use inline styles, or use our width utilities.
<Example code={`
Toggle width collapse
A <button> or <a> element can show and hide multiple elements by referencing them with a selector in its data-bs-target or href attribute.
Conversely, multiple <button> or <a> elements can show and hide the same element if they each reference it with their data-bs-target or href attribute.
<Example code={`
Toggle first element Toggle second element Toggle both elements
Collapse triggers can be placed inside another .collapse element. This is useful when you want to progressively reveal additional content.
<Example code={`
Toggle parent
This is the parent collapse. It can contain links, buttons, and even other collapse triggers.
<p class="d-inline-flex gap-1 mb-0">
<button class="btn btn-outline-primary btn-sm" type="button" data-bs-toggle="collapse" data-bs-target="#collapseNestedChild" aria-expanded="false" aria-controls="collapseNestedChild">
Toggle nested collapse
</button>
</p>
<div class="collapse mt-2" id="collapseNestedChild">
<div class="card card-body">
This is the nested collapse content.
</div>
</div>
</div>
Be sure to add aria-expanded to the control element. This attribute explicitly conveys the current state of the collapsible element tied to the control to screen readers and similar assistive technologies. If the collapsible element is closed by default, the attribute on the control element should have a value of aria-expanded="false". If you’ve set the collapsible element to be open by default using the show class, set aria-expanded="true" on the control instead. The plugin will automatically toggle this attribute on the control based on whether or not the collapsible element has been opened or closed (via JavaScript, or because the user triggered another control element also tied to the same collapsible element). If the control element’s HTML element is not a button (e.g., an <a> or <div>), the attribute role="button" should be added to the element.
If your control element is targeting a single collapsible element – i.e. the data-bs-target attribute is pointing to an id selector – you should add the aria-controls attribute to the control element, containing the id of the collapsible element. Modern screen readers and similar assistive technologies make use of this attribute to provide users with additional shortcuts to navigate directly to the collapsible element itself.
Note that Bootstrap’s current implementation does not cover the various optional keyboard interactions described in the ARIA Authoring Practices Guide accordion pattern - you will need to include these yourself with custom JavaScript.
Collapse transition classes can be found in scss/_transitions.scss as these are shared across multiple components (collapse and accordion).
The collapse plugin utilizes a few classes to handle the heavy lifting:
.collapsehides the content.collapse.showshows the content.collapsingis added when the transition starts, and removed when it finishes
These classes can be found in _transitions.scss.
Just add data-bs-toggle="collapse" and a data-bs-target to the element to automatically assign control of one or more collapsible elements. The data-bs-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you’d like it to default open, add the additional class show.
To add accordion-like group management to a collapsible area, add the data attribute data-bs-parent="#selector". Refer to the accordion page for more information.
Enable manually with:
const collapseElementList = document.querySelectorAll('.collapse')
const collapseList = [...collapseElementList].map(collapseEl => new bootstrap.Collapse(collapseEl))Activates your content as a collapsible element. Accepts an optional options object.
You can create a collapse instance with the constructor, for example:
const bsCollapse = new bootstrap.Collapse('#myCollapse', {
toggle: false
})Bootstrap’s collapse class exposes a few events for hooking into collapse functionality.
| Event type | Description | | --- | --- | | `hide.bs.collapse` | This event is fired immediately when the `hide` method has been called. | | `hidden.bs.collapse` | This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete). | | `show.bs.collapse` | This event fires immediately when the `show` instance method is called. | | `shown.bs.collapse` | This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete). |const myCollapsible = document.getElementById('myCollapsible')
myCollapsible.addEventListener('hidden.bs.collapse', event => {
// do something...
})