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

Collapse - 图2

  1. <p>
  2. <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>
  3. <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>
  4. </p>
  5. <div class="row">
  6. <div class="col">
  7. <div class="collapse multi-collapse" id="multiCollapseExample1">
  8. <div class="card card-body">
  9. 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.
  10. </div>
  11. </div>
  12. </div>
  13. <div class="collapse multi-collapse" id="multiCollapseExample2">
  14. <div class="card card-body">
  15. 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.
  16. </div>
  17. </div>
  18. </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).

  1. .collapse {
  2. &:not(.show) {
  3. display: none;
  4. }
  5. }
  6. .collapsing {
  7. height: 0;
  8. overflow: hidden;
  9. @include transition($transition-collapse);
  10. }

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:

  1. var myCollapse = document.getElementById('myCollapse')
  2. var bsCollapse = new bootstrap.Collapse(myCollapse, {
  3. })