Collapse

Angular Bootstrap 5 Collapse component

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

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

Add mdbCollapse to the element to enable the collapse functionality. Click the buttons below to show and hide another element:

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"
              (click)="basicCollapse.toggle()"
              role="button"
              [attr.aria-expanded]="!basicCollapse.collapsed"
              aria-controls="collapseExample"
            >
              Link with href
            </a>
            <button
              class="btn btn-primary mb-3"
              type="button"
              (click)="basicCollapse.toggle()"
              [attr.aria-expanded]="!basicCollapse.collapsed"
              aria-controls="collapseExample"
            >
              Button
            </button>
            <!--Collapsed content-->
            <div id="collapseExample" mdbCollapse #basicCollapse="mdbCollapse">
              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>
          
        
    

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"
              role="button"
              (click)="linkCollapse.toggle()"
              [attr.aria-expanded]="!linkCollapse.collapsed"
              aria-controls="multiCollapseExample1"
            >
              Toggle first element
            </a>
            <button
              class="btn btn-primary mb-3"
              type="button"
              (click)="buttonCollapse.toggle()"
              [attr.aria-expanded]="!buttonCollapse.collapsed"
              aria-controls="multiCollapseExample2"
            >
              Toggle second element
            </button>
            <button
              class="btn btn-primary mb-3"
              type="button"
              (click)="linkCollapse.toggle(); buttonCollapse.toggle()"
              [attr.aria-expanded]="!linkCollapse.collapsed && !buttonCollapse.collapsed"
              aria-controls="multiCollapseExample1 multiCollapseExample2"
            >
              Toggle both elements
            </button>
            <!-- Collapsed content -->
            <div class="row">
              <div class="col">
                <div
                  class="collapse multi-collapse"
                  id="multiCollapseExample1"
                  mdbCollapse
                  #linkCollapse="mdbCollapse"
                >
                  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"
                  id="multiCollapseExample2"
                  mdbCollapse
                  #buttonCollapse="mdbCollapse"
                >
                  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>
          
        
    

Accessibility

Be sure to add aria-expanded to the control element. Providing [attr.aria-expanded]="!collapseElement.collapsed" attribute will assure dynamic change to aria-expanded="false" when an element is collapsed, and aria-expanded="true" when it's expanded. 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 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

        
            
          import { MdbCollapseModule } from 'mdb-angular-ui-kit/collapse';
          …
          @NgModule ({
            ...
            imports: [MdbCollapseModule],
            ...
          })
        
        
    

Inputs

Name Type Default Description
collapsed Boolean true Changes default collapsed state

Outputs

Name Type Description
collapseShow EventEmitter<MdbCollapseDirective> Fires when show animation starts.
collapseShown EventEmitter<MdbCollapseDirective> Fires when show animation ends.
collapseHide EventEmitter<MdbCollapseDirective> Fires when hide animation starts.
collapseHidden EventEmitter<MdbCollapseDirective> Fires when hide animation ends.
        
            
            <button
            class="btn btn-primary"
            type="button"
            (click)="collapse.toggle()"
            [attr.aria-expanded]="!collapse.collapsed"
            aria-controls="collapseExample"
          >
            Button
          </button>
          <!--Collapsed content-->
          <div
            class="mt-3"
            id="collapseExample"
            mdbCollapse
            #collapse="mdbCollapse"
            (collapseShow)="onCollapseShow($event)"
          >
            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>                   
            
        
    
        
            
            import { Component } from '@angular/core';
            import { MdbCollapseDirective } from 'mdb-angular-ui-kit/collapse';
            
            @Component({
              selector: 'app-root',
              templateUrl: './app.component.html',
              styleUrls: ['./app.component.scss'],
            })
            export class AppComponent {
              onCollapseShow(event: MdbCollapseDirective) {
                console.log('Collapse show: ', event);
              }
            }                    
            
        
    

Methods

Name Description Example
hide Manually closes a collapse collapse.hide()
show Manually opens a collapse collapse.show()
toggle Manually toggle a collapse collapse.toggle()
        
            
            <button
            class="btn btn-primary"
            type="button"
            (click)="collapse.toggle()"
            [attr.aria-expanded]="!collapse.collapsed"
            aria-controls="collapseExample"
          >
            Button
          </button>
          <!--Collapsed content-->
          <div class="mt-3" id="collapseExample" mdbCollapse #collapse="mdbCollapse">
            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>                   
            
        
    
        
            
            import { AfterViewInit, Component, ViewChild } from '@angular/core';
            import { MdbCollapseDirective } from 'mdb-angular-ui-kit/collapse';
            
            @Component({
              selector: 'app-root',
              templateUrl: './app.component.html',
              styleUrls: ['./app.component.scss'],
            })
            export class AppComponent implements AfterViewInit {
              @ViewChild('collapse') collapse!: MdbCollapseDirective;
            
              ngAfterViewInit(): void {
                setTimeout(() => {
                  this.collapse.show();
                }, 2000);
              }
            }            
            
        
    

SCSS variables

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