Bootstrap / Bootstrap Advanced Components
Creating Accordions and Collapsible Panels
In this tutorial, you will learn how to create interactive Accordions and Collapsible Panels using Bootstrap. These components can be used to manage large amounts of content in a …
Section overview
5 resourcesIntroduces advanced Bootstrap components such as accordions, scrollspy, and tooltips.
Introduction
This tutorial will guide you through creating interactive Accordions and Collapsible Panels using Bootstrap. Accordions and collapsible panels are very useful for managing large amounts of content in a user-friendly manner as they allow you to hide and show content as needed.
By the end of this tutorial, you will be able to:
- Understand what Accordions and Collapsible Panels are.
- Create and customize Accordions and Collapsible Panels using Bootstrap.
Prerequisites:
- Basic knowledge of HTML and CSS.
- Basic understanding of Bootstrap framework.
- Bootstrap CDN linked in your project.
Step-by-Step Guide
Bootstrap provides a component called 'Collapse' which we can use to create accordions and collapsible panels.
Accordions
An accordion is a list of collapsible items where each item can be expanded or collapsed independently.
Collapsible Panels
A collapsible panel is a single content area that can be shown or hidden on demand.
Code Examples
Creating an Accordion
Below is an example of a basic accordion in Bootstrap:
<div class="accordion" id="myAccordion">
<div class="card">
<div class="card-header" id="cardHeaderOne">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item 1
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="cardHeaderOne" data-parent="#myAccordion">
<div class="card-body">
Content for item 1.
</div>
</div>
</div>
<!-- Repeat for more items... -->
</div>
Creating a Collapsible Panel
Here is an example of a collapsible panel:
<div class="card">
<div class="card-header" id="panelHeader">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapsePanel" aria-expanded="true" aria-controls="collapsePanel">
Collapsible Panel
</button>
</h5>
</div>
<div id="collapsePanel" class="collapse show" aria-labelledby="panelHeader">
<div class="card-body">
Content for collapsible panel.
</div>
</div>
</div>
Summary
In this tutorial, we have learned how to create interactive Accordions and Collapsible Panels using Bootstrap. We have done this by using the 'collapse' component provided by Bootstrap.
Practice Exercises
- Exercise 1: Create an accordion with three items. Each item should have a unique title and content.
- Exercise 2: Modify the accordion from exercise 1 so that only one item can be open at a time.
- Exercise 3: Create a collapsible panel. When the panel is collapsed, the button should say 'Show more'. When the panel is expanded, the button should say 'Show less'.
Solutions:
1. This is similar to the accordion example provided above. Just repeat the card div for each item.
2. To ensure only one item can be open at a time, make sure each item's 'data-parent' attribute is set to the id of the accordion div.
3. This requires a bit of JavaScript to change the text of the button when the panel is shown or hidden. You can listen for the 'show.bs.collapse' and 'hide.bs.collapse' events on the panel.
Tips for further practice:
Try to create more complex accordions and collapsible panels. For example, you could create nested accordions, or panels with different types of content (like images or forms). You could also try using different Bootstrap styles to customize the look and feel of your accordions and panels.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article