Collapse

Toggle the visibility of content across your project with a few classes and our JavaScript plugins.

How it works

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.

Examples

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

You can use a link with the href attribute, or a button with the data-target attribute. In both cases, the data-toggle="collapse" is required.

The quick brown fox jumps over the lazy dog.

<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">Link with href</a>
<button class="btn btn-primary" data-toggle="collapse" data-target="#collapseExample" type="button" aria-expanded="false" aria-controls="collapseExample">Button with data-target</button>
<div class="collapse" id="collapseExample">
  <p>The quick brown fox jumps over the lazy dog.</p>
</div>

Multiple targets

A <button> or <a> can show and hide multiple elements by referencing them with a selector in its href or data-target attribute. Multiple <button> or <a> can show and hide an element if they each reference it with their href or data-target attribute

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

<a class="btn btn-primary" data-toggle="collapse" href="#multiCollapseExampleOne" role="button" aria-expanded="false" aria-controls="multiCollapseExampleOne">Toggle first element</a>
<button class="btn btn-primary" data-toggle="collapse" data-target="#multiCollapseExampleTwo" type="button" aria-expanded="false" aria-controls="multiCollapseExampleTwo">Toggle second element</button>
<button class="btn btn-primary" data-toggle="collapse" data-target=".multi-collapse" type="button" aria-expanded="false" aria-controls="multiCollapseExampleOne multiCollapseExampleTwo">Toggle both elements</button>
<div class="row">
  <div class="col">
    <div class="collapse multi-collapse" id="multiCollapseExampleOne">
      <p>The quick brown fox jumps over the lazy dog.</p>
    </div>
  </div>
  <div class="col">
    <div class="collapse multi-collapse" id="multiCollapseExampleTwo">
      <p>The quick brown fox jumps over the lazy dog.</p>
    </div>
  </div>
</div>

Accordion example

Using the card component, you can extend the default collapse behavior to create an accordion.

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog.

<div id="accordion">
  <div class="card py-0 mb-3">
    <div class="d-flex align-items-center justify-content-between py-5" id="headingOne">
      <h4 class="fs-xs lh-lg">The quick brown fox jumps over the lazy dog</h4>
      <button class="btn btn-neutral d-inline-flex flex-shrink-0 align-items-center justify-content-center p-0 rounded-circle" data-toggle="collapse" data-target="#collapseOne" style="width: 1.5rem; height: 1.5rem;" type="button" aria-expanded="false" aria-controls="collapseOne">
        <svg class="gi gi-arrow-ios-downward" width="1em" height="1em" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
          <path d="M18.707 8.293a1 1 0 0 0-1.32-.083l-.094.083L12 13.585 6.707 8.293a1 1 0 0 0-1.32-.083l-.094.083a1 1 0 0 0-.083 1.32l.083.094 6 6a1 1 0 0 0 1.32.083l.094-.083 6-6a1 1 0 0 0 0-1.414z"/>
        </svg>
      </button>
    </div>
    <div class="collapse" id="collapseOne" data-parent="#accordion" aria-labelledby="headingOne">
      <div class="py-5 border-top">
        <p>The quick brown fox jumps over the lazy dog.</p>
      </div>
    </div>
  </div>
  <div class="card py-0 mb-3">
    <div class="d-flex align-items-center justify-content-between py-5" id="headingTwo">
      <h4 class="fs-xs lh-lg">The quick brown fox jumps over the lazy dog</h4>
      <button class="btn btn-neutral d-inline-flex flex-shrink-0 align-items-center justify-content-center p-0 rounded-circle" data-toggle="collapse" data-target="#collapseTwo" style="width: 1.5rem; height: 1.5rem;" type="button" aria-expanded="false" aria-controls="collapseTwo">
        <svg class="gi gi-arrow-ios-downward" width="1em" height="1em" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
          <path d="M18.707 8.293a1 1 0 0 0-1.32-.083l-.094.083L12 13.585 6.707 8.293a1 1 0 0 0-1.32-.083l-.094.083a1 1 0 0 0-.083 1.32l.083.094 6 6a1 1 0 0 0 1.32.083l.094-.083 6-6a1 1 0 0 0 0-1.414z"/>
        </svg>
      </button>
    </div>
    <div class="collapse" id="collapseTwo" data-parent="#accordion" aria-labelledby="headingTwo">
      <div class="py-5 border-top">
        <p>The quick brown fox jumps over the lazy dog.</p>
      </div>
    </div>
  </div>
  <div class="card py-0 mb-3">
    <div class="d-flex align-items-center justify-content-between py-5" id="headingThree">
      <h4 class="fs-xs lh-lg">The quick brown fox jumps over the lazy dog</h4>
      <button class="btn btn-neutral d-inline-flex flex-shrink-0 align-items-center justify-content-center p-0 rounded-circle" data-toggle="collapse" data-target="#collapseThree" style="width: 1.5rem; height: 1.5rem;" type="button" aria-expanded="false" aria-controls="collapseThree">
        <svg class="gi gi-arrow-ios-downward" width="1em" height="1em" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
          <path d="M18.707 8.293a1 1 0 0 0-1.32-.083l-.094.083L12 13.585 6.707 8.293a1 1 0 0 0-1.32-.083l-.094.083a1 1 0 0 0-.083 1.32l.083.094 6 6a1 1 0 0 0 1.32.083l.094-.083 6-6a1 1 0 0 0 0-1.414z"/>
        </svg>
      </button>
    </div>
    <div class="collapse" id="collapseThree" data-parent="#accordion" aria-labelledby="headingThree">
      <div class="py-5 border-top">
        <p>The quick brown fox jumps over the lazy dog.</p>
      </div>
    </div>
  </div>
  <div class="card py-0">
    <div class="d-flex align-items-center justify-content-between py-5" id="headingFour">
      <h4 class="fs-xs lh-lg">The quick brown fox jumps over the lazy dog</h4>
      <button class="btn btn-neutral d-inline-flex flex-shrink-0 align-items-center justify-content-center p-0 rounded-circle" data-toggle="collapse" data-target="#collapseFour" style="width: 1.5rem; height: 1.5rem;" type="button" aria-expanded="false" aria-controls="collapseFour">
        <svg class="gi gi-arrow-ios-downward" width="1em" height="1em" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
          <path d="M18.707 8.293a1 1 0 0 0-1.32-.083l-.094.083L12 13.585 6.707 8.293a1 1 0 0 0-1.32-.083l-.094.083a1 1 0 0 0-.083 1.32l.083.094 6 6a1 1 0 0 0 1.32.083l.094-.083 6-6a1 1 0 0 0 0-1.414z"/>
        </svg>
      </button>
    </div>
    <div class="collapse" id="collapseFour" data-parent="#accordion" aria-labelledby="headingFour">
      <div class="py-5 border-top">
        <p>The quick brown fox jumps over the lazy dog.</p>
      </div>
    </div>
  </div>
</div>

Accessibility

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-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.

Usage

The collapse plugin utilizes a few classes to handle the heavy lifting:

  • .collapse hides the content
  • .collapsing is added when the transition starts, and removed when it finishes
  • .collapse.show shows the content

Data attributes

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-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-parent="#selector". Refer to the demo to see this in action.