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 …

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Introduces 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

  1. Exercise 1: Create an accordion with three items. Each item should have a unique title and content.
  2. Exercise 2: Modify the accordion from exercise 1 so that only one item can be open at a time.
  3. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Favicon Generator

Create favicons from images.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help