Bootstrap 5 Collapse is useful when you want to toggle (hide or show) the visibility of content across your project. You can use buttons or anchors tag 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.
The collapse required JavaScript plugin to hide and show the contents.
Example 1: Click the buttons below to show and hide another element.
<p> <a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample1"> Link with href </a> <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample1"> Button with data-bs-target </button> </p> <div class="collapse" id="collapseExample1"> <div class="card card-body"> Some placeholder content for the collapse component. This element is hidden by default but revealed when the user activates the relevant trigger. </div> </div>
A <button> or <a> element can show and hide multiple elements by referencing them with different target in data-bs-target or href attribute.
Example 2: Click buttons to show Multiple toggles and targets
<p> <a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample2" >Toggle first element</a> <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample3">Toggle second element</button> <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target=".multi-collapse">Toggle both elements</button> </p> <div class="row"> <div class="col"> <div class="collapse multi-collapse" id="collapseExample2"> <div class="card card-body"> Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger. </div> </div> </div> <div class="col"> <div class="collapse multi-collapse" id="collapseExample3"> <div class="card card-body"> Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger. </div> </div> </div> </div>