Accordion & Collapse
Collapse: Show and Hide Content
Collapse lets a button show or hide a block of content, with a smooth animation.
html
<button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#more">
Show details
</button>
<div class="collapse mt-3" id="more">
<div class="card card-body">
This content appears when you click the button.
</div>
</div>
collapsehides the content at the startdata-bs-targetpoints to the id of the content- Add
showto the class if the content should be open at the start
Accordion
An accordion is a group of collapse items where opening one closes the others, which is perfect for FAQ pages.
html
<div class="accordion" id="faq">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#q1">
What is Bootstrap?
</button>
</h2>
<div id="q1" class="accordion-collapse collapse show" data-bs-parent="#faq">
<div class="accordion-body">
Bootstrap is a CSS framework for building responsive websites.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#q2">
Is Bootstrap free?
</button>
</h2>
<div id="q2" class="accordion-collapse collapse" data-bs-parent="#faq">
<div class="accordion-body">
Yes, Bootstrap is free and open source.
</div>
</div>
</div>
</div>
Result
Two questions. Opening one answer automatically closes the other.
The Important Attributes
| Attribute or Class | Meaning |
|---|---|
data-bs-parent="#faq" | Makes the accordion close other items when one opens |
collapsed on the button | Shows the closed arrow icon |
show on the panel | Opens it at page load |
accordion-flush | Removes borders and rounded corners for a clean look |
Always Open
Remove the data-bs-parent attribute and each item will open and close independently.
SEO and UX tip: Accordions are excellent for FAQs. Keep questions clear and answers short.
Conclusion
Collapse and accordion save space and keep long pages tidy. Always make sure each button target matches the right id.
Quick Summary: collapse toggles one block. accordion groups collapse items and data-bs-parent keeps only one open.

