📖 Scenario: You are creating a simple FAQ section for a website using Bootstrap's accordion component. This will help users easily find answers by expanding and collapsing questions.
🎯 Goal: Build a functional Bootstrap accordion with three collapsible items, each with a question as the header and an answer as the body.
📋 What You'll Learn
Use Bootstrap 5 accordion structure
Create exactly three accordion items
Each item must have a unique id for collapse control
Use semantic HTML with button elements for toggling
Ensure accessibility with aria-controls and aria-expanded attributes
Accordion items should collapse and expand independently
💡 Why This Matters
🌍 Real World
Accordions are common in FAQs, menus, and content sections to save space and improve user experience by showing only relevant information.
💼 Career
Knowing how to implement Bootstrap components is valuable for front-end developers working on responsive and accessible websites quickly.
Progress0 / 4 steps
1
Create the basic accordion container
Create a div with class="accordion" and id="faqAccordion" to hold the accordion items.
Bootsrap
Hint
The accordion container needs a class accordion and an id faqAccordion.
2
Add the first accordion item
Inside the div with id="faqAccordion", add the first accordion item with id="headingOne" for the header and id="collapseOne" for the collapsible body. The header button text should be "What is Bootstrap?" and the body text should be "Bootstrap is a CSS framework for building responsive websites.".
Bootsrap
Hint
Use accordion-item, accordion-header, and accordion-button classes. The button toggles the collapse with data-bs-toggle and data-bs-target.
3
Add the second and third accordion items
Add two more accordion items inside the div with id="faqAccordion". Use id="headingTwo" and id="collapseTwo" for the second item, and id="headingThree" and id="collapseThree" for the third. The second header text is "How do I include Bootstrap?" with body "You can include Bootstrap via CDN or by downloading the files.". The third header text is "Is Bootstrap free to use?" with body "Yes, Bootstrap is open source and free to use.". Ensure each button toggles its respective collapse area and uses correct aria attributes.
Bootsrap
Hint
Remember to add collapsed class to buttons that start closed and set aria-expanded="false".
4
Add Bootstrap CSS and JS links
Add the Bootstrap 5 CSS link inside the <head> and the Bootstrap 5 JavaScript bundle script before the closing </body> tag. Use the official Bootstrap CDN URLs for version 5.3. Make sure the accordion will work with these included.
Bootsrap
Hint
Use the official Bootstrap 5.3 CDN links for CSS and JS bundle to enable styling and accordion functionality.
Practice
(1/5)
1. What is the main purpose of the Accordion component in Bootstrap?
easy
A. To create a navigation bar with dropdown menus
B. To show and hide sections of content by expanding or collapsing them
C. To display images in a sliding carousel
D. To style buttons with different colors
Solution
Step 1: Understand Accordion functionality
The Accordion component is designed to toggle visibility of content sections by expanding or collapsing them.
Step 2: Compare with other Bootstrap components
Navigation bars, carousels, and buttons serve different purposes unrelated to content toggling.
Final Answer:
To show and hide sections of content by expanding or collapsing them -> Option B
Quick Check:
Accordion = toggle content visibility [OK]
Hint: Accordion toggles content sections open or closed [OK]
Common Mistakes:
Confusing Accordion with navigation menus
Thinking Accordion is for image sliders
Assuming Accordion styles buttons
2. Which of the following is the correct HTML structure to start an Accordion in Bootstrap 5?
easy
A. <div class='accordion' id='myAccordion'>
B. <accordion id='myAccordion'>
C. <div class='accordion-item'>
D. <section class='accordion-collapse'>
Solution
Step 1: Identify the container element for Accordion
The Accordion container uses a div with class accordion and an ID for reference.
Step 2: Check Bootstrap 5 syntax
Bootstrap 5 requires div.accordion as the wrapper, not accordion tag or other classes like accordion-item or accordion-collapse alone.
Final Answer:
<div class='accordion' id='myAccordion'> -> Option A
Quick Check:
Accordion container = div.accordion [OK]
Hint: Accordion always starts with div.accordion container [OK]
Common Mistakes:
Using a non-existent <accordion> tag
Starting with accordion-item instead of accordion container
Confusing collapse section with container
3. Given this Accordion item code snippet, what will happen when the button is clicked?
A. The accordion-collapse div is missing the collapse class
B. The button should not have aria-expanded attribute
C. The data-bs-parent attribute should be on the button, not the collapse div
D. The accordion container is missing an ID
Solution
Step 1: Check classes on collapsible content
The collapsible div must have class collapse to enable Bootstrap's collapse behavior.
Step 2: Verify other attributes
The aria-expanded attribute on the button is correct and data-bs-parent belongs on the collapse div. The container has an ID.
Final Answer:
The accordion-collapse div is missing the collapse class -> Option A
Quick Check:
collapse class missing = no toggle [OK]
Hint: Always add 'collapse' class to collapsible content div [OK]
Common Mistakes:
Forgetting the 'collapse' class on content div
Misplacing data-bs-parent attribute
Removing aria-expanded attribute
5. You want to create an Accordion where only one section can be open at a time, and the first section is open by default. Which combination of attributes and classes achieves this behavior correctly?
hard
A. Add data-bs-single='true' to buttons and expanded attribute on first button
B. Add data-bs-toggle='accordion' to buttons and active class to first button
C. Use data-bs-multiple='false' on accordion container and open class on first item
D. Add data-bs-parent to each collapse div and add show class to the first collapse div