📖 Scenario: You are creating a simple webpage with Bootstrap where clicking one button will show or hide multiple content sections at the same time. This is useful for menus or FAQs where you want to control several parts with one click.
🎯 Goal: Build a webpage with a button that toggles the visibility of two separate content boxes simultaneously using Bootstrap's multi-target collapse feature.
📋 What You'll Learn
Use Bootstrap 5 collapse component
Create a button that toggles two different collapsible content sections at once
Each collapsible section should have some visible text inside
Use semantic HTML and include necessary Bootstrap classes and attributes
Ensure the button has accessible labels
💡 Why This Matters
🌍 Real World
Multi-target collapse is useful in websites where you want one control to show or hide multiple content areas, like FAQs, menus, or dashboards.
💼 Career
Understanding Bootstrap's collapse component and accessibility best practices is valuable for front-end web developers building interactive and user-friendly interfaces.
Progress0 / 4 steps
1
Create the HTML skeleton with two collapsible content sections
Write the basic HTML structure with doctype, html with lang="en", head including meta charset="UTF-8" and viewport meta tag. Inside the body, create two div elements with id attributes collapseOne and collapseTwo. Each div should have the Bootstrap class collapse and contain some text: "Content for section one." and "Content for section two." respectively.
Bootsrap
Hint
Remember to add two div elements with id and collapse class inside the body. Each should have the exact text content.
2
Add a button to toggle both collapsible sections
Add a button element above the collapsible divs. Give it the Bootstrap classes btn and btn-primary. Add the attribute data-bs-toggle="collapse" and set data-bs-target="#collapseOne,#collapseTwo" to target both collapsible sections. The button text should be "Toggle Sections".
Bootsrap
Hint
Make sure the button has the correct Bootstrap classes and the data-bs-toggle and data-bs-target attributes targeting both #collapseOne and #collapseTwo.
3
Include Bootstrap JavaScript for collapse functionality
Add the Bootstrap JavaScript bundle by including a script tag before the closing </body> tag. Use the CDN link https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js. This enables the collapse toggle to work.
Bootsrap
Hint
Include the Bootstrap JavaScript bundle just before the closing </body> tag to enable interactive components.
4
Add accessibility attributes to the toggle button
Enhance the toggle button by adding aria-expanded="false" and aria-controls="collapseOne collapseTwo" attributes. This helps screen readers understand the button controls the two collapsible sections.
Bootsrap
Hint
Use aria-expanded="false" and aria-controls="collapseOne collapseTwo" on the button to improve accessibility.
Practice
(1/5)
1. What does the data-bs-target attribute do in a Bootstrap multi-target collapse button?
easy
A. It specifies multiple elements to show or hide when the button is clicked.
B. It changes the button color when clicked.
C. It adds animation to the button itself.
D. It disables the button after one click.
Solution
Step 1: Understand the role of data-bs-target
This attribute tells Bootstrap which elements to toggle visibility for when the button is clicked.
Step 2: Recognize multi-target usage
When multiple IDs are separated by commas in data-bs-target, all those elements collapse or expand together.
Final Answer:
It specifies multiple elements to show or hide when the button is clicked. -> Option A
Quick Check:
Multi-target collapse = toggle multiple elements [OK]
Hint: Remember: data-bs-target controls which sections toggle [OK]
Common Mistakes:
Thinking it changes button style
Confusing it with data-bs-toggle
Assuming it disables the button
2. Which of the following is the correct syntax to target two collapsible sections with IDs #section1 and #section2 in one button using Bootstrap?
easy
A. Toggle
B. Toggle
C. Toggle
D. Toggle
Solution
Step 1: Identify correct separator for multiple targets
Bootstrap requires commas to separate multiple IDs in data-bs-target.
Step 2: Check each option's syntax
<button data-bs-toggle="collapse" data-bs-target="#section1,#section2">Toggle</button> uses commas correctly: #section1,#section2. Others use spaces, semicolons, or spaces with dot selectors which are invalid here.
Final Answer:
<button data-bs-toggle="collapse" data-bs-target="#section1,#section2">Toggle</button> -> Option D
Quick Check:
Multiple IDs separated by commas = correct syntax [OK]
Hint: Use commas to separate multiple IDs in data-bs-target [OK]
Common Mistakes:
Using spaces instead of commas
Using semicolons or other separators
Mixing class selectors with IDs
3. Given this HTML snippet, what happens when the button is clicked?
A. The IDs in data-bs-target should be separated by commas, not spaces.
B. The button is missing the class "btn".
C. The data-bs-toggle attribute should be "dropdown" instead of "collapse".
D. The IDs should not have the # symbol in data-bs-target.
Solution
Step 1: Check the separator between multiple targets
Bootstrap requires commas to separate multiple IDs in data-bs-target, not spaces.
Step 2: Verify other attributes
Class "btn" is optional for functionality, "collapse" is correct toggle type, and IDs must include # in selector.
Final Answer:
The IDs in data-bs-target should be separated by commas, not spaces. -> Option A
Quick Check:
Use commas, not spaces, between IDs [OK]
Hint: Separate multiple IDs with commas, not spaces [OK]
Common Mistakes:
Using spaces instead of commas
Confusing toggle types
Removing # from IDs
5. You want a button to toggle three collapsible sections: #info, #details, and #summary. Which of these is the best complete HTML snippet to achieve this with Bootstrap multi-target collapse?
hard
A. Toggle All
Info content
Details content
Summary content
B. Toggle All
Info content
Details content
Summary content
C. Toggle All
Info content
Details content
Summary content
D. Toggle All
Info content
Details content
Summary content
Solution
Step 1: Check data-bs-target syntax
<button class="btn btn-info" data-bs-toggle="collapse" data-bs-target="#info,#details,#summary">Toggle All</button>
<div id="info" class="collapse">Info content</div>
<div id="details" class="collapse">Details content</div>
<div id="summary" class="collapse">Summary content</div> correctly uses commas and includes # before each ID, which is required.
Step 2: Verify collapsible sections and initial states
<button class="btn btn-info" data-bs-toggle="collapse" data-bs-target="#info,#details,#summary">Toggle All</button>
<div id="info" class="collapse">Info content</div>
<div id="details" class="collapse">Details content</div>
<div id="summary" class="collapse">Summary content</div> sets all sections with class "collapse" (hidden initially), which is typical for toggling. <button class="btn btn-info" data-bs-toggle="collapse" data-bs-target="#info,#details,#summary">Toggle All</button>
<div id="info" class="collapse show">Info content</div>
<div id="details" class="collapse show">Details content</div>
<div id="summary" class="collapse show">Summary content</div> shows all sections visible initially, which is valid but not best for toggle demonstration.