📖 Scenario: You are building a simple webpage where users can click a button to show or hide some content horizontally. This is useful for menus or side panels that slide open and closed.
🎯 Goal: Create a horizontal collapse section using Bootstrap. When the user clicks the button, the content area should expand or collapse horizontally.
📋 What You'll Learn
Use Bootstrap 5 classes and components
Create a button that toggles horizontal collapse
Create a collapsible content area that expands horizontally
Ensure the collapse works smoothly on button click
Use semantic HTML and accessible attributes
💡 Why This Matters
🌍 Real World
Horizontal collapsible sections are common in navigation menus, sidebars, and interactive panels on websites and web apps.
💼 Career
Knowing how to implement Bootstrap's collapse component helps you build responsive, user-friendly interfaces quickly, a valuable skill for frontend web development jobs.
Progress0 / 4 steps
1
Set up the basic HTML structure with Bootstrap
Create a basic HTML5 page with lang="en", meta charset="UTF-8", and viewport meta tag. Include Bootstrap 5 CSS from CDN inside the <head>. Inside the <body>, add a <div> with class container and inside it add a <button> with id="toggleButton" and text Toggle Horizontal Collapse.
Bootsrap
Hint
Remember to include Bootstrap CSS from CDN inside the <head>. Add a button inside a container with the exact id="toggleButton" and text.
2
Add the collapsible content container
Below the <button id="toggleButton">, add a <div> with id="collapseContent" and classes collapse collapse-horizontal border bg-light p-3 mt-3. Inside this div, add the text Here is the horizontally collapsible content!.
Bootsrap
Hint
Use the Bootstrap classes collapse and collapse-horizontal on the content div. Add some border and padding for better visibility.
3
Add the button attributes to toggle the collapse
Add the attributes data-bs-toggle="collapse" and data-bs-target="#collapseContent" to the <button id="toggleButton"> so it toggles the horizontal collapse on click.
Bootsrap
Hint
Add data-bs-toggle="collapse" and data-bs-target="#collapseContent" to the button. Also add accessibility attributes aria-expanded="false" and aria-controls="collapseContent".
4
Include Bootstrap JavaScript for collapse functionality
Before the closing </body> tag, add the Bootstrap 5 JavaScript bundle CDN script tag: <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>. This enables the collapse toggle to work.
Bootsrap
Hint
Include the Bootstrap JavaScript bundle before the closing </body> tag to enable interactive components like collapse.
Practice
(1/5)
1. What does the collapse-horizontal class do in Bootstrap?
easy
A. It makes the collapsible content slide left or right instead of up or down.
B. It changes the color of the collapsible content.
C. It disables the collapse functionality.
D. It makes the collapsible content slide up or down.
Solution
Step 1: Understand the role of collapse-horizontal
This class changes the collapse animation direction from vertical (up/down) to horizontal (left/right).
Step 2: Compare with default collapse behavior
By default, Bootstrap collapses content vertically. Adding collapse-horizontal changes this to horizontal sliding.
Final Answer:
It makes the collapsible content slide left or right instead of up or down. -> Option A
Quick Check:
collapse-horizontal = horizontal slide [OK]
Hint: Remember: horizontal means left-right slide, not up-down [OK]
Common Mistakes:
Thinking it changes color or disables collapse
Confusing horizontal with vertical collapse
Assuming it works without Bootstrap JS
2. Which of the following is the correct way to start a horizontal collapse in Bootstrap?
easy
A. <div class='collapse-vertical' id='myCollapse'></div>
B. <div class='horizontal-collapse' id='myCollapse'></div>
C. <div class='collapse-horizontal'></div>
D. <div class='collapse collapse-horizontal' id='myCollapse'></div>
Solution
Step 1: Identify correct Bootstrap classes for horizontal collapse
The correct classes are collapse and collapse-horizontal together on the element.
Step 2: Check the syntax of options
<div class='collapse collapse-horizontal' id='myCollapse'></div> uses both classes and includes an id for toggling, which is required for Bootstrap collapse to work.
Final Answer:
<div class='collapse collapse-horizontal' id='myCollapse'></div> -> Option D
Quick Check:
Use both collapse and collapse-horizontal classes [OK]
Hint: Always combine collapse with collapse-horizontal for horizontal effect [OK]
Common Mistakes:
Using only collapse-horizontal without collapse
Using wrong class names like collapse-vertical
Missing the id attribute for toggling
3. Given this code snippet, what will happen when the button is clicked?
C. The id attribute should be on the button, not the div.
D. The collapse-horizontal class should be on the button.
Solution
Step 1: Check required classes for collapse
Bootstrap requires the collapse class on the collapsible element for functionality.
Step 2: Identify missing class in code
The div only has collapse-horizontal but lacks collapse, so it won't collapse properly.
Final Answer:
Missing the main collapse class on the div. -> Option A
Quick Check:
collapse class is mandatory for collapse [OK]
Hint: Always include collapse class with collapse-horizontal [OK]
Common Mistakes:
Forgetting the collapse class
Misplacing id attribute
Assuming button needs collapse-horizontal
5. You want to create a horizontal collapse panel that starts collapsed and expands to 300px wide when toggled. Which code snippet correctly achieves this with accessibility in mind?