Collapse

Bootstrap 5 Collapse component

Responsive collapse built with Bootstrap 5. Collapse panel, vertical collapse, collapsing divs, data-toggle usage, collapse button & more.

Collapse is a vertical element used to show and hide content via class changes.

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

Note: Read the API tab to find all available options and advanced customization

Required ES init: Collapse *
* UMD autoinits are enabled by default. This means that you don't need to initialize the component manually. However if you are using MDBootstrap ES format then you should pass the required components to the initMDB method.

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.


Basic 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

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

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
        
            
            <!-- Buttons trigger collapse -->
            <a
              class="btn btn-primary mb-3"
              data-mdb-collapse-init
              data-mdb-ripple-init
              href="#collapseExample"
              role="button"
              aria-expanded="false"
              aria-controls="collapseExample"
            >
              Link with href
            </a>
            <button
              class="btn btn-primary mb-3"
              type="button"
              data-mdb-collapse-init
              data-mdb-ripple-init
              data-mdb-target="#collapseExample"
              aria-expanded="false"
              aria-controls="collapseExample"
            >
              Button with data-mdb-target
            </button>

            <!-- Collapsed content -->
            <div class="collapse" id="collapseExample">
              Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad
              squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
              sapiente ea proident.
            </div>
          
        
    
        
            
            // Initialization for ES Users
            import { Collapse, Ripple, initMDB } from 'mdb-ui-kit';

            initMDB({ Collapse, Ripple });
          
        
    

Horizontal

The collapse plugin also supports horizontal collapsing. Add the .collapse-horizontal modifier class to transition the width instead of height and set a width on the immediate child element. Feel free to write your own custom Sass, use inline styles, or use our width utilities.

Please note that while the example below has a min-height set to avoid excessive repaints in our docs, this is not explicitly required. Only the width on the child element is required.

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
        
            
              <p>
                <button 
                  class="btn btn-primary"
                  type="button"
                  data-mdb-collapse-init
                  data-mdb-ripple-init
                  data-mdb-target="#collapseWidthExample"
                  aria-expanded="false"
                  aria-controls="collapseWidthExample"
                >
                  Toggle width collapse
                </button>
              </p>
              <div style="min-height: 120px;">
                <div class="collapse collapse-horizontal" id="collapseWidthExample" >   
                  <div style="width: 300px;">
                    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad
                    squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
                    sapiente ea proident.          
                  </div>
                </div>
              </div>
            
        
    
        
            
              // Initialization for ES Users
              import { Collapse, Ripple, initMDB } from 'mdb-ui-kit';

              initMDB({ Collapse, Ripple });
            
        
    

Multiple targets

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

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
        
            
                <!-- Buttons trigger collapse -->
                <a
                  class="btn btn-primary mb-3"
                  data-mdb-collapse-init
                  data-mdb-ripple-init
                  href="#multiCollapseExample1"
                  role="button"
                  aria-expanded="false"
                  aria-controls="multiCollapseExample1"
                >
                  Toggle first element
                </a>
                <button
                  class="btn btn-primary mb-3"
                  type="button"
                  data-mdb-collapse-init
                  data-mdb-ripple-init
                  data-mdb-target="#multiCollapseExample2"
                  aria-expanded="false"
                  aria-controls="multiCollapseExample2"
                >
                  Toggle second element
                </button>
                <button
                  class="btn btn-primary mb-3"
                  type="button"
                  data-mdb-collapse-init
                  data-mdb-ripple-init
                  data-mdb-target=".multi-collapse"
                  aria-expanded="false"
                  aria-controls="multiCollapseExample1 multiCollapseExample2"
                >
                  Toggle both elements
                </button>

                <!-- Collapsed content -->
                <div class="row">
                  <div class="col">
                    <div class="collapse multi-collapse" id="multiCollapseExample1">
                      Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry
                      richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson
                      cred nesciunt sapiente ea proident.
                    </div>
                  </div>
                  <div class="col">
                    <div class="collapse multi-collapse mt-3" id="multiCollapseExample2">
                      Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry
                      richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson
                      cred nesciunt sapiente ea proident.
                    </div>
                  </div>
                </div>
              
        
    
        
            
                // Initialization for ES Users
                import { Collapse, Ripple, initMDB } from 'mdb-ui-kit';

                initMDB({ Collapse, Ripple });
              
        
    

With scroll

Add custom styles with specified height to see content with scrollbar.

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
        
            
                  <!-- Button triggers collapse -->
                  <a
                    class="btn btn-primary mb-3"
                    data-mdb-collapse-init
                    data-mdb-ripple-init
                    href="#collapseWithScrollbar"
                    role="button"
                    aria-expanded="false"
                    aria-controls="collapseExample"
                  >
                    Longer content
                  </a>

                  <!-- Collapsed content -->
                  <div class="collapse scroll-section" id="collapseWithScrollbar"  style="max-width: 500px">
                    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad
                    squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente
                    ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer
                    farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them
                    accusamus labore sustainable VHS. 3 wolf moon officia aute, non cupidatat skateboard dolor
                    brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua
                    put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
                  </div>
                
        
    
        
            
                  .scroll-section {
                    max-height: 100px;
                    overflow-y: auto;
                  }
                
        
    
        
            
                  // Initialization for ES Users
                  import { Collapse, Ripple, initMDB } from 'mdb-ui-kit';

                  initMDB({ Collapse, Ripple });
                
        
    

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-mdb-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 keyboard interactions described in the WAI-ARIA Authoring Practices 1.1 accordion pattern - you will need to include these yourself with custom JavaScript.


Collapse - API


Import

Importing components depends on how your application works. If you intend to use the MDBootstrap ES format, you must first import the component and then initialize it with the initMDB method. If you are going to use the UMD format, just import the mdb-ui-kit package.

        
            
          import { Collapse, initMDB } from "mdb-ui-kit";
          
          initMDB({ Collapse });
        
        
    
        
            
          import "mdb-ui-kit"
        
        
    

Usage

Via data attributes

Using the collapse component doesn't require any additional JavaScript code - simply add data-mdb-collapse-init attribute to collapse toggler and use other data attributes to set all options. For ES format, you must first import and call the initMDB method.

The data-mdb-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-mdb-parent="#selector". Refer to the accordion page for more information.

        
            
            <button
              class="btn btn-primary"
              type="button"
              data-mdb-collapse-init
              data-mdb-target="#collapseExample"
              aria-expanded="false"
              aria-controls="collapseExample"
            >
              Button with data-mdb-target
            </button>

            <!-- Collapsed content -->
            <div class="collapse mt-3" id="collapseExample">
              Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad
              squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt
              sapiente ea proident.
            </div>
          
        
    

Via JavaScript

        
            
              const element = document.querySelector('.collapse')
              const instance = new Collapse(element)
            
        
    
        
            
              const element = document.querySelector('.collapse')
              const instance = new mdb.Collapse(element)
          
        
    

Options

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

Name Type Default Description
parent selector | DOM element | null false If parent is provided, then 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 card class). The attribute has to be set on the target collapsible area.
toggle boolean true Toggles the collapsible element on invocation

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.

Method Description Example
toggle Toggles a collapsible element to shown or hidden. Returns to the caller before the collapsible element has actually been shown or hidden (i.e. before the shown.bs.collapse or hidden.bs.collapse event occurs). myCollapse.toggle()
show Shows a collapsible element. Returns to the caller before the collapsible element has actually been shown (e.g., before the shown.bs.collapse event occurs). myCollapse.show()
hide Hides a collapsible element. Returns to the caller before the collapsible element has actually been hidden (e.g., before the hidden.bs.collapse event occurs). myCollapse.hide()
dispose Destroys an element's collapse. myCollapse.dispose()
getInstance Static method which allows you to get the collapse instance associated with a DOM element. Collapse.getInstance()
getOrCreateInstance Static method which allows you to get the collapse instance associated with a DOM element or create a new one in case it wasn't initialized. Collapse.getOrCreateInstance()
        
            
            const myCollapse = document.getElementById('myCollapse')
            const collapse = new Collapse(myCollapse)
            collapse.show()
          
        
    
        
            
            const myCollapse = document.getElementById('myCollapse')
            const collapse = new mdb.Collapse(myCollapse)
            collapse.show()
        
        
    

Events

Collapse triggers a few events for hooking into collapse functionality.

Event type Description
show.mdb.collapse This event fires immediately when the show instance method is called.
shown.mdb.collapse This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete).
hide.mdb.collapse This event is fired immediately when the hide method has been called.
hidden.mdb.collapse This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete).
        
            
            const myCollapsible = document.getElementById('myCollapsible')
            myCollapsible.addEventListener('hidden.mdb.collapse', () => {
              // do something...
            })
          
        
    

SCSS variables

        
            
        $transition-collapse: height .35s ease;
        $transition-collapse-width: width .35s ease;