π Bootstrap Accordion & Collapse: The Magic Drawer Story
Imagine you have a magical dresser. Each drawer can open and close with just a tap. Sometimes only one drawer opens at a time, and sometimes many can be open together. Thatβs exactly what Accordion and Collapse do for your website!
π The Big Picture
Think of a Collapse as a single drawer that opens and closes.
Think of an Accordion as a smart dresser where opening one drawer automatically closes the others.
Simple, right? Letβs explore each magical drawer!
π¦ 1. Accordion - The Polite Dresser
What Is It?
An Accordion is like a stack of drawers where only one can be open at a time. When you open a new drawer, the previous one closes automatically. Very polite!
Why Use It?
- Shows a lot of information in a small space
- Keeps things organized and tidy
- Users focus on one thing at a time
The Simple Example
<div class="accordion" id="myAccordion">
<!-- First Item -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button"
type="button"
data-bs-toggle="collapse"
data-bs-target="#panelOne">
π Fruits
</button>
</h2>
<div id="panelOne"
class="accordion-collapse collapse show"
data-bs-parent="#myAccordion">
<div class="accordion-body">
Apples, bananas, and oranges!
</div>
</div>
</div>
<!-- Second Item -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed"
type="button"
data-bs-toggle="collapse"
data-bs-target="#panelTwo">
π₯ Vegetables
</button>
</h2>
<div id="panelTwo"
class="accordion-collapse collapse"
data-bs-parent="#myAccordion">
<div class="accordion-body">
Carrots, broccoli, and peas!
</div>
</div>
</div>
</div>
π Key Parts
| Part | What It Does |
|---|---|
accordion |
The magical dresser container |
accordion-item |
Each drawer |
accordion-header |
The drawer handle |
accordion-button |
The clickable part |
accordion-collapse |
The drawer content |
data-bs-parent |
Makes drawers close others! |
π‘ The Secret Sauce
The magic happens with data-bs-parent="#myAccordion". This tells Bootstrap: βHey, when I open, close my siblings!β
πͺ 2. Collapse - The Simple Drawer
What Is It?
A Collapse is just ONE drawer. It opens. It closes. Thatβs it! No rules about other drawers.
The Simple Example
<!-- The Button (Toggle) -->
<button class="btn btn-primary"
type="button"
data-bs-toggle="collapse"
data-bs-target="#myContent">
Open the Drawer! πͺ
</button>
<!-- The Hidden Content -->
<div class="collapse" id="myContent">
<div class="card card-body">
π Surprise! Here's the hidden treasure!
</div>
</div>
How It Works
graph TD A["User Clicks Button"] --> B{Is Drawer Open?} B -->|No| C["Drawer Opens π"] B -->|Yes| D["Drawer Closes π"] C --> E["Content Visible"] D --> F["Content Hidden"]
π― Two Ways to Toggle
Method 1: Using data-bs-target
<button data-bs-toggle="collapse"
data-bs-target="#example">
Click Me
</button>
Method 2: Using href (for links)
<a href="#example"
data-bs-toggle="collapse">
Click Me
</a>
βοΈ 3. Collapse Horizontal - The Sideways Drawer
What Is It?
Normal collapse goes up-down like an elevator. Horizontal collapse slides left-right like a sliding door!
The Simple Example
<button class="btn btn-info"
type="button"
data-bs-toggle="collapse"
data-bs-target="#slidePanel">
Slide It! βοΈ
</button>
<div class="collapse collapse-horizontal"
id="slidePanel">
<div class="card card-body"
style="width: 200px;">
I slide from the side! π’
</div>
</div>
π The Magic Word
Add collapse-horizontal class. Thatβs it!
<div class="collapse collapse-horizontal">
β οΈ Important Rule
For horizontal collapse, your content needs a fixed width. Otherwise, it wonβt know how much to slide!
<div style="width: 200px;">
Your content here
</div>
π― 4. Collapse Multiple Targets - The Group Toggle
What Is It?
One button that controls many drawers at once! Like a master switch that opens all the lights.
The Simple Example
<!-- One Button, Multiple Targets -->
<button class="btn btn-success"
type="button"
data-bs-toggle="collapse"
data-bs-target=".multi-collapse">
Toggle All! πͺ
</button>
<!-- First Drawer -->
<div class="collapse multi-collapse"
id="drawer1">
<div class="card card-body">
Drawer 1 Content π¦
</div>
</div>
<!-- Second Drawer -->
<div class="collapse multi-collapse"
id="drawer2">
<div class="card card-body">
Drawer 2 Content π¦
</div>
</div>
π The Secret
Use a class selector with a dot:
data-bs-target=".multi-collapse"
All elements with class multi-collapse will toggle together!
π‘ Mix and Match
You can also have individual controls plus a master switch:
<!-- Master Control -->
<button data-bs-target=".multi-collapse">
Toggle All
</button>
<!-- Individual Controls -->
<button data-bs-target="#drawer1">
Toggle Drawer 1
</button>
<button data-bs-target="#drawer2">
Toggle Drawer 2
</button>
β 5. Close Button - The Exit Door
What Is It?
A special button that looks like an X and dismisses things. Used in alerts, modals, and anywhere you need a βgo awayβ button.
The Simple Example
<!-- Basic Close Button -->
<button type="button"
class="btn-close"
aria-label="Close">
</button>
π¨ Close Button Variants
Default (Dark X)
<button class="btn-close"></button>
White X (for dark backgrounds)
<button class="btn-close btn-close-white"></button>
Disabled Close Button
<button class="btn-close" disabled></button>
π Close Button + Collapse
Want the X to close a collapse? Add the dismiss attribute:
<div class="alert alert-warning
alert-dismissible fade show">
<strong>Hey!</strong> I can be closed!
<button type="button"
class="btn-close"
data-bs-dismiss="alert">
</button>
</div>
Common Uses
| Use Case | Code |
|---|---|
| Dismiss Alert | data-bs-dismiss="alert" |
| Close Modal | data-bs-dismiss="modal" |
| Close Offcanvas | data-bs-dismiss="offcanvas" |
π§ Quick Memory Tricks
graph TD A["Need Collapsible Content?"] --> B{How Many Sections?} B -->|Just One| C["Use Collapse"] B -->|Multiple, One at a Time| D["Use Accordion"] B -->|Multiple, All Together| E["Use Multiple Targets"] C --> F{Which Direction?} F -->|Up/Down| G["Regular Collapse"] F -->|Left/Right| H["Collapse Horizontal"]
π― The Complete Cheat Code
Accordion Formula
Container (accordion + id)
βββ Items (accordion-item)
βββ Header (accordion-header)
β βββ Button (data-bs-target + data-bs-toggle)
βββ Panel (accordion-collapse + data-bs-parent)
βββ Body (accordion-body)
Collapse Formula
Toggle Button (data-bs-toggle="collapse" + data-bs-target)
βββ Target Content (collapse + id)
Multiple Targets Formula
Toggle Button (data-bs-target=".classname")
βββ Target 1 (collapse + classname)
βββ Target 2 (collapse + classname)
βββ Target N (collapse + classname)
π Youβre Ready!
Now you understand Bootstrapβs magical drawers:
| Feature | Use When |
|---|---|
| Accordion | One section open at a time |
| Collapse | Simple show/hide |
| Horizontal | Side-to-side sliding |
| Multiple Targets | Control many at once |
| Close Button | Dismiss/close something |
Go build something amazing! π
