Bootstrap - Understanding Collapse and Accordion in Bootstrap
Introduction
Bootstrap provides a powerful and easy way to create collapsible content areas — commonly known as Collapse and Accordion components. These are widely used in websites to hide or show content dynamically, making web pages cleaner, more organized, and user-friendly.
1. What Is Bootstrap Collapse?
The Collapse component in Bootstrap allows you to toggle the visibility of content. It’s often used for showing and hiding sections of text, FAQs, or panels without navigating to another page.
Key Features
-
Allows you to show or hide content smoothly with a sliding animation.
-
Can be triggered using a button, link, or any clickable element.
-
Works using data attributes or JavaScript methods.
Basic Example
<!-- Button to toggle collapse -->
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#demoCollapse">
Toggle Content
</button>
<!-- Collapsible content -->
<div class="collapse" id="demoCollapse">
<div class="card card-body">
This is the collapsible content section.
</div>
</div>
How It Works
-
The button has
data-bs-toggle="collapse"
anddata-bs-target="#demoCollapse"
. -
The target div has the class
.collapse
and a matchingid="demoCollapse"
. -
Clicking the button toggles the visibility of that div with a sliding effect.
2. What Is an Accordion in Bootstrap?
An Accordion is a group of collapsible items where only one section can be open at a time. It’s ideal for FAQs, documentation pages, or lists with expandable details.
Key Features
-
Built on the Collapse component.
-
Automatically closes other open items when one expands.
-
Ensures a tidy and compact layout.
Basic Accordion Example
<div class="accordion" id="exampleAccordion">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne">
Accordion Item #1
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#exampleAccordion">
<div class="accordion-body">
This is the first accordion item.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingTwo">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo">
Accordion Item #2
</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#exampleAccordion">
<div class="accordion-body">
This is the second accordion item.
</div>
</div>
</div>
</div>
How It Works
-
The
.accordion
class wraps all items. -
Each section has a header (the clickable button) and a collapse area (the hidden content).
-
The
data-bs-parent="#exampleAccordion"
ensures only one item stays open at a time.
3. Bootstrap Collapse vs. Accordion: The Difference
Feature | Collapse | Accordion |
---|---|---|
Purpose | Show/hide a single content area | Manage multiple collapsible sections |
Open Items | Multiple can be open | Only one can be open |
Use Case | Toggles, menus, hidden sections | FAQs, grouped content |
Required Structure | Simple divs | .accordion container with multiple .accordion-item s |
4. Using JavaScript with Collapse
Bootstrap also lets you control collapse behavior using JavaScript.
Example:
// Show a collapse
var myCollapse = new bootstrap.Collapse(document.getElementById('demoCollapse'), {
toggle: true
});
You can use methods like:
-
.show()
– to open the collapse -
.hide()
– to close it -
.toggle()
– to switch between open/closed states
5. Advantages of Using Collapse and Accordion
-
Improves readability by organizing large content.
-
Enhances user experience with smooth animations.
-
Mobile-friendly and fully responsive.
-
Lightweight—requires no additional libraries beyond Bootstrap.
Conclusion
The Collapse and Accordion components in Bootstrap are essential tools for creating interactive, space-saving web designs. Whether you want to hide extra details under a “Read More” link or organize FAQs in a clean accordion format, these features help improve both usability and visual appeal with minimal code.