The animation effect of this component is dependent on the prefers-reduced-motion
media query. See the reduced motion section of our accessibility documentation.
Example
Click the buttons below to show and hide another element via class changes:
.collapse
hides content.collapsing
is applied during transitions.collapse.show
shows 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 a link with the href
attribute (and a role="button"
). In both cases, the data-bs-toggle="collapse"
is required.
A <button>
or <a>
can show and hide multiple elements by referencing them with a selector in its href
or data-bs-target
attribute. Multiple <button>
or <a>
can show and hide an element if they each reference it with their href
or data-bs-target
attribute
<p>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target=".multi-collapse" aria-expanded="false" aria-controls="multiCollapseExample1 multiCollapseExample2">Toggle both elements</button>
</p>
<div class="row">
<div class="col">
<div class="collapse multi-collapse" id="multiCollapseExample1">
<div class="card card-body">
Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
<div class="collapse multi-collapse" id="multiCollapseExample2">
<div class="card card-body">
Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
Accessibility
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 - you will need to include these yourself with custom JavaScript.
Classes
Collapse transition classes can be found in scss/_transitions.scss
as these are shared across multiple components (collapse and accordion).
.collapse {
&:not(.show) {
display: none;
}
}
.collapsing {
height: 0;
overflow: hidden;
@include transition($transition-collapse);
}
Usage
The collapse plugin utilizes a few classes to handle the heavy lifting:
.collapse
hides the content.collapse.show
shows the content- is 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
.
Via JavaScript
Enable manually with:
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-bs-
, as in data-bs-parent=""
.
Methods
Asynchronous methods and transitions
All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.
.
Activates your content as a collapsible element. Accepts an optional options object
.
You can create a collapse instance with the constructor, for example:
var myCollapse = document.getElementById('myCollapse')
var bsCollapse = new bootstrap.Collapse(myCollapse, {
})