📖 Scenario: You want to add a section on your webpage that can show or hide extra information when a user clicks a button. This helps keep your page clean and organized.
🎯 Goal: Build a simple Bootstrap collapse component with a button that toggles the visibility of a content area.
📋 What You'll Learn
Use Bootstrap 5 classes and attributes
Create a button that toggles the collapse
Create a collapsible content section
Ensure the collapse works when the button is clicked
💡 Why This Matters
🌍 Real World
Collapsible sections are common on websites to hide extra details, FAQs, or menus, making pages cleaner and easier to navigate.
💼 Career
Knowing how to use Bootstrap components like collapse helps you quickly build interactive and responsive web pages, a valuable skill for front-end developers.
Progress0 / 4 steps
1
Set up the basic HTML structure with Bootstrap CSS link
Write the basic HTML5 skeleton with lang="en", charset="UTF-8", and viewport meta tag. Include the Bootstrap 5 CSS CDN link inside the <head> section. Add an empty <body> section.
Bootsrap
Hint
Remember to include the Bootstrap CSS link inside the <head> so you can use Bootstrap styles.
2
Add a button to toggle the collapse
Inside the <body>, add a <button> with class btn btn-primary. Give it type="button", data-bs-toggle="collapse", and data-bs-target="#collapseExample" attributes. The button text should be Toggle Content.
Bootsrap
Hint
The button needs data-bs-toggle="collapse" and data-bs-target="#collapseExample" to control the collapse.
3
Add the collapsible content section
Below the button, add a <div> with class="collapse" and id="collapseExample". Inside this div, add a <div> with class="card card-body" containing the text This is the collapsible content.
Bootsrap
Hint
The collapsible content must have class="collapse" and the same id as the button's data-bs-target.
4
Add Bootstrap JavaScript bundle for collapse functionality
Before the closing </body> tag, add a <script> tag that loads the Bootstrap 5 JavaScript bundle from the CDN: https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js. This enables the collapse toggle to work.
Bootsrap
Hint
The Bootstrap JavaScript bundle is needed for the collapse toggle to work. Add it just before the closing </body> tag.
Practice
(1/5)
1. What is the main purpose of the Bootstrap Collapse component?
easy
A. To hide or show content smoothly when a user clicks a control
B. To create a fixed navigation bar at the top of the page
C. To add animations to images on hover
D. To validate form inputs automatically
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 A
Quick Check:
Collapse toggles content visibility = B [OK]
Hint: Collapse means hide/show content smoothly on click [OK]
Common Mistakes:
Confusing Collapse with navigation or animation components
Thinking Collapse validates forms
Assuming Collapse is for styling images
2. Which attribute is required on a button to toggle a Bootstrap Collapse element?
easy
A. data-target="collapse"
B. data-toggle="collapse"
C. data-bs-toggle="collapse"
D. data-bs-target="collapse"
Solution
Step 1: Identify the correct Bootstrap 5 attribute for toggling Collapse
Bootstrap 5 uses the prefix data-bs- for its JavaScript plugins, so the toggle attribute is data-bs-toggle="collapse".
Step 2: Eliminate outdated or incorrect attributes
Options A and B use syntax without bs prefix (Bootstrap 4 or earlier). data-bs-target="collapse"'s data-bs-target="collapse" specifies the target element, not the toggle action.
Final Answer:
data-bs-toggle="collapse" -> Option C
Quick Check:
Toggle attribute in Bootstrap 5 = data-bs-toggle="collapse" [OK]
Hint: Use data-bs-toggle="collapse" for toggling in Bootstrap 5 [OK]
Common Mistakes:
Using data-toggle instead of data-bs-toggle
Confusing data-bs-target with toggle attribute
Omitting the data-bs prefix
3. What will happen when the button below is clicked?
A. The div has a typo in the class name; it should be "collapse"
B. The button is missing the data-bs-toggle attribute
C. The div id does not match the data-bs-target
D. The button should use data-toggle instead of data-bs-toggle
Solution
Step 1: Check the button attributes
The button correctly uses data-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 A
Quick Check:
Class must be "collapse" for Bootstrap to work [OK]
Hint: Check class spelling: must be "collapse" not "collaps" [OK]
Common Mistakes:
Misspelling the collapse class
Confusing data-bs-toggle with data-toggle
Mismatching id and data-bs-target
5. You want to create an accordion with two sections using Bootstrap Collapse. Which code snippet correctly ensures only one section is open at a time?