THE WORLD'S LARGEST WEB DEVELOPER SITE

Bootstrap 4 JS Collapse


Collapse CSS Classes

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

Class Description Example
.collapse Hides the content Try it
.collapse show Shows the collapsible content by default 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 "show".

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 Try it
parent selector false All collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior) Try it
toggle boolean true Toggles the collapsible element on invocation Try it

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("dispose") Destroys the collapsible element

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