How to implement a collapsible/accordion component in Bootstrap

How to implement a collapsible/accordion component in Bootstrap

In web development, creating user-friendly and organized interfaces is paramount. One effective way to achieve this is by implementing a collapsible or accordion component. These components allow users to expand and collapse sections of content, presenting information in a neat and easily digestible format. In this blog post, we’ll explore how to seamlessly integrate a collapsible/accordion component using Bootstrap, providing a step-by-step guide to enhance your web projects.

Understanding the Collapsible/Accordion Component

A collapsible or accordion component consists of multiple sections, or “cards,” each containing content that can be toggled open or closed. When one card is opened, the others are automatically closed, creating a streamlined user experience. This component is commonly used for FAQs, product features, or any content that needs to be presented in a structured manner.

Steps to Implement a Collapsible/Accordion Component

Let’s dive into the implementation process of a collapsible/accordion component using Bootstrap:

Step 1: Set Up the HTML Structure

Start by structuring your HTML with the necessary classes and elements. Each card will have a title (the clickable header) and content. Wrap all cards within a container, often a <div> element with the class accordion.

<div class="accordion" id="accordionExample">
  <div class="card">
    <div class="card-header" id="headingOne">
      <h2 class="mb-0">
        <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne">
          Collapsible Card #1

    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
      <div class="card-body">
        Content for Card #1
  <!-- Repeat similar structure for other cards -->

Step 2: Include Bootstrap CSS and JavaScript

To utilize Bootstrap’s built-in functionality, make sure to include the Bootstrap CSS and JavaScript files in your project. You can either link to the files hosted on a content delivery network (CDN) or download them and link to local files.

Step 3: Configure Card Titles and Content

For each card, customize the title and content as needed. Replace the text within the card header (<h2>) and card body (<div class="card-body">) with your desired content.

Step 4: Add Data Attributes for Collapse Interaction

Bootstrap relies on data attributes to control the collapse interaction. The data-toggle attribute triggers the collapse, and the data-target attribute specifies the ID of the element to collapse.

Step 5: Organize with Parent ID

Using the data-parent attribute on the collapsed elements ensures that only one card is open at a time within the same container. The value should match the ID of the accordion container.

Step 6: Customize Styling

Bootstrap provides basic styling out of the box, but you can customize the appearance of the accordion to match your design. Utilize Bootstrap classes and your own CSS to achieve the desired look.

Benefits of Using the Collapsible/Accordion Component

Integrating a collapsible/accordion component offers several benefits for both developers and users:

  • Organized Content: Users can easily navigate and find relevant information in a structured layout.
  • Space Efficiency: The accordion design conserves space, making it ideal for mobile and limited-screen-size environments.
  • Enhanced User Experience: The intuitive expand and collapse functionality simplifies content consumption.
  • Consistency: Bootstrap’s standardized styling ensures a consistent appearance across different devices and browsers.


The collapsible/accordion component is a valuable addition to your web development toolkit, enabling you to create clean and user-friendly interfaces. By following the steps outlined in this guide, you can seamlessly integrate this component using Bootstrap. Whether you’re building a FAQ section, showcasing product features, or organizing content, the collapsible/accordion component is a versatile solution that enhances user experience and presentation. Elevate your web projects by implementing this interactive and efficient component, and provide your users with a more intuitive way to explore your content.