The Collapse component lets you hide or show content smoothly on a webpage. It helps keep pages clean and organized by showing only what the user wants to see.
Collapse component in Bootsrap
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample"> Toggle content </button> <div class="collapse" id="collapseExample"> <div class="card card-body"> Collapsible content here. </div> </div>
The data-bs-toggle="collapse" attribute tells Bootstrap this button controls a collapse.
The data-bs-target attribute points to the ID of the content to show or hide.
<button class="btn btn-secondary" type="button" data-bs-toggle="collapse" data-bs-target="#info" aria-expanded="false" aria-controls="info"> Show Info </button> <div class="collapse" id="info"> <div class="card card-body"> Here is some extra information. </div> </div>
<a class="btn btn-link" data-bs-toggle="collapse" href="#details" role="button" aria-expanded="false" aria-controls="details"> More details </a> <div class="collapse" id="details"> <div class="card card-body"> Detailed content shown or hidden. </div> </div>
<div class="accordion" id="accordionExample"> <div class="accordion-item"> <h2 class="accordion-header" id="headingOne"> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> Accordion Item #1 </button> </h2> <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> <div class="accordion-body"> Content for first accordion item. </div> </div> </div> </div>
This page shows a button that toggles a hidden content area. When you click the button, the content smoothly appears or disappears.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Collapse Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="container py-4"> <h1>Collapse Component Demo</h1> <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseContent" aria-expanded="false" aria-controls="collapseContent"> Toggle Details </button> <div class="collapse mt-3" id="collapseContent"> <div class="card card-body"> This content is hidden by default and shown when you click the button. </div> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Always include aria-expanded and aria-controls for accessibility so screen readers understand the toggle state.
Use Bootstrap's JavaScript bundle to enable the collapse functionality.
Collapse works well with buttons or links but avoid using elements that are not interactive by default.
The Collapse component hides or shows content smoothly on user action.
Use data-bs-toggle="collapse" and data-bs-target to connect controls and content.
It improves page organization and user experience by controlling visible content.
Practice
Collapse component?Solution
Step 1: Understand the Collapse component function
The Collapse component is designed to toggle visibility of content smoothly, usually triggered by user actions like clicks.Step 2: Compare options with the component's purpose
Only To hide or show content smoothly when a user clicks a control describes hiding or showing content smoothly, which matches the Collapse component's role.Final Answer:
To hide or show content smoothly when a user clicks a control -> Option AQuick Check:
Collapse toggles content visibility = B [OK]
- Confusing Collapse with navigation or animation components
- Thinking Collapse validates forms
- Assuming Collapse is for styling images
Solution
Step 1: Identify the correct Bootstrap 5 attribute for toggling Collapse
Bootstrap 5 uses the prefixdata-bs-for its JavaScript plugins, so the toggle attribute isdata-bs-toggle="collapse".Step 2: Eliminate outdated or incorrect attributes
Options A and B use syntax withoutbsprefix (Bootstrap 4 or earlier). data-bs-target="collapse"'sdata-bs-target="collapse"specifies the target element, not the toggle action.Final Answer:
data-bs-toggle="collapse" -> Option CQuick Check:
Toggle attribute in Bootstrap 5 = data-bs-toggle="collapse" [OK]
- Using data-toggle instead of data-bs-toggle
- Confusing data-bs-target with toggle attribute
- Omitting the data-bs prefix
<button class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#info">Toggle Info</button> <div id="info" class="collapse">Hidden content here</div>
Solution
Step 1: Analyze the button attributes
The button hasdata-bs-toggle="collapse"anddata-bs-target="#info", linking it to the div with id "info".Step 2: Understand the effect on the target div
The div has class "collapse", so clicking the button toggles its visibility with a smooth animation.Final Answer:
The div with id "info" will smoothly appear or disappear -> Option BQuick Check:
Button toggles collapse div visibility = A [OK]
- Thinking button needs JavaScript event handlers manually
- Assuming button changes style only
- Believing div is removed instead of toggled
<button class="btn btn-secondary" data-bs-toggle="collapse" data-bs-target="#panel">Show</button> <div id="panel" class="collaps">Content</div>
Solution
Step 1: Check the button attributes
The button correctly usesdata-bs-toggle="collapse"and targets#panel.Step 2: Inspect the div class and id
The div has id "panel" matching the target, but the class is "collaps" instead of "collapse". This typo prevents the collapse behavior.Final Answer:
The div has a typo in the class name; it should be "collapse" -> Option AQuick Check:
Class must be "collapse" for Bootstrap to work [OK]
- Misspelling the collapse class
- Confusing data-bs-toggle with data-toggle
- Mismatching id and data-bs-target
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">Section 1</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample">
<div class="accordion-body">Content 1</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="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">Section 2</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
<div class="accordion-body">Content 2</div>
</div>
</div>
</div>Solution
Step 1: Check for accordion structure and attributes
The code usesdata-bs-parent="#accordionExample"on collapse divs, which ensures only one section is open at a time.Step 2: Verify buttons and collapse classes
Buttons havedata-bs-toggle="collapse"and target correct divs. Collapse divs have the correct classes and IDs.Final Answer:
This code correctly uses data-bs-parent to allow only one open section -> Option DQuick Check:
data-bs-parent controls accordion behavior = C [OK]
- Omitting data-bs-parent causes multiple sections open
- Forgetting collapse class on content divs
- Misusing aria-expanded attribute
Ready to prove it?
Pass the test and this pattern moves unknown
