Tutorials References Exercises Videos Menu
Free Website Get Certified Pro

Bootstrap JS Collapse


JS Collapse (collapse.js)

Get base styles and flexible support for collapsible components like accordions and navigation.

Plugin dependency: Collapse requires the transitions plugin to be included in your version of Bootstrap.

For a tutorial about Collapsibles, read our Bootstrap Collapse Tutorial.


The Collapse Plugin Classes

Class Description Example
.collapse Hides the content Try it
.collapse in Shows the content Try it
.collapsing Added when the transition starts, and removed when it finishes Try it

Via data-* Attributes

Just add data-toggle="collapse" and a data-target to element to automatically assign control of a collapsible element. 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 in.

Example

<button class="btn" data-toggle="collapse" data-target="#demo">Collapsible</button>

<div id="demo" class="collapse">
Some text..
</div>
Try it Yourself »

Tip: To add accordion-like group management to a collapsible control, add the data attribute data-parent="#selector".


Via JavaScript

Enable manually with:

$('.collapse').collapse()


Collapse Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-parent="".

Name Type Default Description
parent selector false All collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this is dependent on the panel class) - See example below
toggle boolean true Toggles the collapsible element on invocation

Collapse Methods

The following table lists all available collapse methods.

Method Description Try it
.collapse(options) Activates the collapsible element with an option. See options above for valid values
.collapse("toggle") Toggles the collapsible element Try it
.collapse("show") Shows the collapsible element Try it
.collapse("hide") Hides the collapsible element Try it

Collapse Events

The following table lists all available collapse events.

Event Description Try it
show.bs.collapse Occurs when the collapsible element is about to be shown Try it
shown.bs.collapse Occurs when the collapsible element is fully shown (after CSS transitions have completed) Try it
hide.bs.collapse Occurs when the collapsible element is about to be hidden Try it
hidden.bs.collapse Occurs when the collapsible element is fully hidden (after CSS transitions have completed) Try it

More Examples

Simple Collapsible

The following example makes a button toggle the expanding and collapsing content of another element:

Example

<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo">
  Simple collapsible
</button>

<div id="demo" class="collapse in">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit,
  sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
  nisi ut aliquip ex ea commodo consequat.
</div>
Try it Yourself »

Collapsible Panel

The following example shows a collapsible panel:

Example

<div class="panel-group">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" href="#collapse1">Collapsible panel</a>
      </h4>
    </div>
    <div id="collapse1" class="panel-collapse collapse">
      <div class="panel-body">Panel Body</div>
      <div class="panel-footer">Panel Footer</div>
    </div>
  </div>
</div>
Try it Yourself »

Collapsible List Group

The following shows a collapsible panel with a list group inside:

Example

<div class="panel-group">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" href="#collapse1">Collapsible list group</a>
      </h4>
    </div>
    <div id="collapse1" class="panel-collapse collapse">
      <ul class="list-group">
        <li class="list-group-item">One</li>
        <li class="list-group-item">Two</li>
        <li class="list-group-item">Three</li>
      </ul>
      <div class="panel-footer">Footer</div>
    </div>
  </div>
</div>
Try it Yourself »

Accordion

The following example shows a simple accordion by extending the panel component:

Note: The data-parent attribute makes sure that all collapsible elements under the specified parent will be closed when one of the collapsible item is shown.

Example

<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapse1">
        Collapsible Group 1</a>
      </h4>
    </div>
    <div id="collapse1" class="panel-collapse collapse in">
      <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
      sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
      minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
      commodo consequat.</div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapse2">
        Collapsible Group 2</a>
      </h4>
    </div>
    <div id="collapse2" class="panel-collapse collapse">
      <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
      sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
      minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
      commodo consequat.</div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapse3">
        Collapsible Group 3</a>
      </h4>
    </div>
    <div id="collapse3" class="panel-collapse collapse">
      <div class="panel-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit,
      sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
      minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
      commodo consequat.</div>
    </div>
  </div>
</div>
Try it Yourself »

Expand and Collapse Toggle Icon & Text

The following example changes the open/close text and icon when opening and closing the collapsible content:

Example

$(document).ready(function(){
  $("#demo").on("hide.bs.collapse", function(){
    $(".btn").html('<span class="glyphicon glyphicon-collapse-down"></span> Open');
  });
  $("#demo").on("show.bs.collapse", function(){
    $(".btn").html('<span class="glyphicon glyphicon-collapse-up"></span> Close');
  });
});
Try it Yourself »

Or you can use CSS:

Example

/* Icon when the collapsible content is shown */
.btn:after {
  font-family: "Glyphicons Halflings";
  content: "\e114";
}

/* Icon when the collapsible content is hidden */
.btn.collapsed:after {
  content: "\e080";
}
Try it Yourself »