Bootstrap accordion is a component that allows you to display collapsible content panels for presenting information in a compact and organized manner. Here is a step-by-step tutorial on how to create an accordion in Bootstrap :

  1. Include the necessary files:
    To use Bootstrap accordion, you need to include the Bootstrap CSS and JavaScript files in your HTML file.
<!-- Bootstrap  CSS  -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">

<!-- Bootstrap JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
  1. Create an HTML structure for the accordion:
    To create an accordion, you need to create a set of panels that will be collapsed and expanded. Each panel consists of a header and a content section. You can use the “accordion” class to create an accordion container, and the “card” class to create each panel.
<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" aria-expanded="true" aria-controls="collapseOne">
          Panel 1
        </button>
      </h2>
    </div>

    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
      <div class="card-body">
        Content 1
      </div>
    </div>
  </div>

  <div class="card">
    <div class="card-header" id="headingTwo">
      <h2 class="mb-0">
        <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Panel 2
        </button>
      </h2>
    </div>

    <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
      <div class="card-body">
        Content 2
      </div>
    </div>
  </div>
</div>
  1. Add the necessary attributes:
    You need to add the “data-toggle”, “data-target”, “aria-expanded”, and “aria-controls” attributes to the button element to control the collapse and expand behavior of the panel.
  2. Customize the accordion:
    You can customize the styling of the accordion using CSS. For example, you can change the color of the header and the font size of the content.
.accordion .card-header {
  background-color: #f5f5f5;
  font-weight: bold;
}

.accordion .card-body {
  font-size: 14px;
}

By following these steps, you can easily create an accordion in Bootstrap and customize its styling to match your website design

Leave a Reply