How to Create an Accordion Using Bootstrap: Step-by-Step Tutorial


By Faraz -

Learn how to create a responsive accordion using Bootstrap. This tutorial will guide you through the steps of creating and customizing your accordion easily.

how to create an accordion using bootstrap fast and easily.jpg

Table of Contents

  1. Project Introduction
  2. HTML Code
  3. CSS Code
  4. Preview
  5. Conclusion

An accordion is a UI element commonly used in web design to display a large amount of content in a compact space. It allows users to toggle between different sections of content, displaying one section at a time, and hiding the others. The name "accordion" is derived from a musical instrument of the same name, which can be compressed or expanded to change the pitch of the sound. In the same way, an accordion UI element can be compressed or expanded to show or hide the content. Accordions are commonly used on mobile devices where screen real estate is limited, but they can also be used on desktop web pages to improve the user experience by providing an organized and streamlined way to display information. Accordions can also help improve the speed of a webpage by only loading the necessary content when it is needed, rather than loading all of the content at once.

Accordions are always a great thing to have on your website - they can help to provide clarity and organization with your content and to easily show off key points or collections of information. You can also create them in just a few minutes with Bootstrap, which makes it easy for you!

In this post, we will provide a step-by-step process for front-end developers and web developers to create an accordion using Bootstrap. Bootstrap is a popular framework that makes web development fast, easy, and efficient, allowing developers to create professional-looking web pages with minimal effort. By the end of this guide, you will have a thorough understanding of how to create an accordion using Bootstrap and how to customize it to your website's specific needs.

Let's start making an amazing accordion using bootstrap step by step.

Join My Telegram Channel to Download the Project: Click Here


Before starting this tutorial, you should have a basic understanding of HTML, and CSS. Additionally, you will need a code editor such as Visual Studio Code or Sublime Text to write and save your code.

Source Code

Step 1 (HTML Code):

To get started, we will first need to create a basic HTML file. In this file, we will include the main structure for our accordion.

Next, you will need to install the Bootstrap CDN. This is a network of servers that distribute CSS and JavaScript files. You can install the CDN by clicking on the Get Started button on the Bootstrap website and following the instructions.

This is a HTML file that uses Bootstrap to create an accordion, which is a graphical user interface element that allows users to expand and collapse sections of content.

The code imports the Bootstrap CSS stylesheet and JavaScript file from a content delivery network (CDN). It also imports a custom stylesheet called "styles.css".

The main content of the page is contained within a <div> element with the class "container". This <div> contains another <div> with the classes "row align-items-center justify-content-center vh-100". This <div> contains a <div> with the classes "col-md-5 col-sm-12 col-xs-12", which defines the width of the accordion.

The accordion itself is created using the <div> element with the class "accordion" and an ID of "accordionPlusMinus". Inside this <div>, there are four <div> elements with the class "accordion-item". Each <div> contains an <h2> element with the class "accordion-header" and a <button> element with the classes "accordion-button collapsed" and various data attributes. The <h2> element contains the title of each section, and the <button> element contains a collapsed icon and the text of the title.

Finally, each <div> element with the class "accordion-item" contains another <div> element with the classes "accordion-collapse collapse". This <div> contains another <div> element with the class "accordion-body", which contains the content of each section. When a user clicks on the title of a section, the corresponding <div> with the class "accordion-collapse" is expanded or collapsed to reveal or hide the content.

The JavaScript file imported from the CDN is responsible for the interactive behavior of the accordion.

After creating the files just paste the following codes into your file. Remember that you must save a file with the .html extension.

Step 2 (CSS Code):

Next, we will create our CSS file. In this file, we will add plus and minus icons for our accordion using SVG.

The first part of the code sets the background color of the webpage to a light blue-gray color.

The remaining lines of code target a specific element with the ID "accordionPlusMinus" and its child elements with the class "accordion-button". The code applies specific styles to these elements, such as changing the direction of the flexbox layout and adjusting margins.

When the "accordion-button" element is not collapsed, the color and background color of the element changes to black and white respectively. Additionally, a small arrow icon is added to the right of the element, which changes its orientation when the element is expanded or collapsed.

Create a CSS file with the name of styles.css and paste the given codes into your CSS file. Remember that you must create a file with the .css extension.

  background-color: #d5e2e4;
#accordionPlusMinus .accordion-button { 
  flex-direction: row-reverse; 
  start; margin-left: 0; 
  margin-right: 15px; 
#accordionPlusMinus .accordion-button:not(.collapsed) { 
  color: #000; 
  background-color: #fff; 
#accordionPlusMinus .accordion-button:not(.collapsed)::after { 
  margin-left: 0; 
  background-image: url("data:image/svg+xml,%3Csvg xmlns='' viewBox='0 0 16 2'%3E%3Cpath fill='none' stroke='%23D0364E' stroke-linecap='round' stroke-width='2' d='M8 1H1m7 0h7'/%3E%3C/svg%3E"); 
  transform-origin: 50% 25%; 
#accordionPlusMinus .accordion-button::after { 
  width: 1.25rem; 
  height: 1.25rem; 
  content: ""; 
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' fill='none' xmlns=''%3E%3Cpath d='M8 1v7m0 7V8m0 0H1m7 0h7' stroke='%233482B5' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E"); 
  justify-content: start; 
  margin-left: 0; 
  margin-right: 15px; 

Final Output:

how to create an accordion using bootstrap fast and easily.gif


In conclusion, creating an accordion using Bootstrap is a great way to enhance the user experience on your website by displaying large amounts of content in a compact format. By following the steps outlined in this tutorial, you can easily create a responsive and customizable accordion that matches the design of your website.

Bootstrap provides a variety of customization options, such as adding colors and styles, icons, and animations. With these customization options, you can create a unique accordion that fits the look and feel of your website.

By using Bootstrap to create an accordion, you also benefit from the framework's responsive design, making your accordion look great on all devices. In addition, Bootstrap offers built-in accessibility features that help ensure your accordion is accessible to all users.

In summary, creating an accordion using Bootstrap is a fast and efficient way to add an interactive element to your website. With this tutorial, you have learned the necessary steps to create a responsive and customizable accordion that enhances the user experience on your website. Good luck with your web development projects!

That’s a wrap!

I hope you enjoyed this post. Now, with these examples, you can create your own amazing page.

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox

Latest Post