Bootstrap Accordion Example on CodePen

Ruth Jul 09, 2026

Bootstrap, a popular CSS framework, offers a wide range of components to simplify and speed up web development. One of these components is the accordion, which allows you to create collapsible sections of content. In this article, we'll explore an example of a Bootstrap accordion using CodePen, a web development playground that allows you to experiment with HTML, CSS, and JavaScript.

Bootstrap Accordion Color Change
Bootstrap Accordion Color Change

Before we dive into the CodePen example, let's briefly understand what an accordion is and why it's useful. An accordion is a vertical list of items where only one item can be expanded at a time. It's particularly useful for saving space and organizing content, especially when you have a lot of information to display. Now, let's see how to create an accordion using Bootstrap and CodePen.

33 Accordions CSS
33 Accordions CSS

Setting Up the Bootstrap Accordion on CodePen

To start, you'll need to include the Bootstrap CSS and JavaScript files in your CodePen pen. You can do this by adding the following lines in the 'Settings' panel under 'External CSS' and 'External JS' respectively:

20 Best Bootstrap Accordion Examples 2026 - Colorlib
20 Best Bootstrap Accordion Examples 2026 - Colorlib

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js"></script>

Creating the Accordion Structure

Stylish Bootstrap 5 Accordion with Dark Theme and Custom Icons
Stylish Bootstrap 5 Accordion with Dark Theme and Custom Icons

Now, let's create the basic structure of the accordion. We'll use the `

` container and add multiple `
` elements, each representing a collapsible item.

Here's a simple example of what the HTML structure looks like:

<div class="accordion" id="accordionExample">
  <div class="card">
     <div class="card-header" id="headingOne">
     <h2 class="mb-0">
       <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
         Collapsible Group Item #1
       </button>
     </h2>
   </div>
   <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
     <div class="card-body">
       This is some text within first collapse body.
     </div>
   </div>
  </div>
</div>

Accordion Group for Framer
Accordion Group for Framer

Adding More Items to the Accordion

You can add more collapsible items by duplicating the `

` structure and modifying the `id` and `data-target` attributes accordingly. Here's an example of adding a second item:

<div class="card">
   <div class="card-header" id="headingTwo">
     <h2 class="mb-0">
       <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
         Collapsible Group Item #2
       </button>
     </h2>
   </div>
   <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
     <div class="card-body">
       This is some text within second collapse body.
     </div>
   </div>
  </div>

Create Nested Accordion with jQuery BeefUp Plugin — CodeHim
Create Nested Accordion with jQuery BeefUp Plugin — CodeHim

Customizing the Bootstrap Accordion

Bootstrap's accordion comes with default styles, but you can easily customize it to match your project's design. You can change the colors, fonts, and other styles by targeting the appropriate CSS classes.

16 Beautiful Looking CSS Accordion Menu OnAirCode
16 Beautiful Looking CSS Accordion Menu OnAirCode
How to Create and Style a Squarespace Accordion Block — Applet Studio
How to Create and Style a Squarespace Accordion Block — Applet Studio
How to make Divi accordion all closed (2026 guide)
How to make Divi accordion all closed (2026 guide)
Design better accordions
Design better accordions
Accordion Menu Website Design
Accordion Menu Website Design
26+ Accordion Elementary Design Collection
26+ Accordion Elementary Design Collection
Stylish HTML and CSS Accordion - Code Snippet
Stylish HTML and CSS Accordion - Code Snippet
How to Make an Accordion Using HTML And CSS
How to Make an Accordion Using HTML And CSS
CSS sliding multi-level accordion
CSS sliding multi-level accordion
How to Create CSS-Based Content Accordion
How to Create CSS-Based Content Accordion
the wordpress website for accordion with csss
the wordpress website for accordion with csss
CSS3 Accordions
CSS3 Accordions
Best Practices for Accordion Menu in Web Design
Best Practices for Accordion Menu in Web Design
29 Accordion Tabs Design Inspiration - HTML & CSS Snippets Ξ ℂ𝕠𝕕𝕖𝕄𝕪𝕌𝕀
29 Accordion Tabs Design Inspiration - HTML & CSS Snippets Ξ ℂ𝕠𝕕𝕖𝕄𝕪𝕌𝕀
Flexbox Accordion
Flexbox Accordion
How to Make Vertical Accordion Menu with CSS - OnAirCode
How to Make Vertical Accordion Menu with CSS - OnAirCode
Smooth Nested Accordion Web Component - Vanilla-Accordions | CSS Script
Smooth Nested Accordion Web Component - Vanilla-Accordions | CSS Script
CSS Libraries, Code Snippets & Tutorials
CSS Libraries, Code Snippets & Tutorials
How to customise your accordion blocks in Squarespace 7.1 | Finding Design
How to customise your accordion blocks in Squarespace 7.1 | Finding Design
Codepen Team Member List - Even/Odd Styles
Codepen Team Member List - Even/Odd Styles

Changing the Accordion's Background Color

For example, let's say you want to change the background color of the accordion's header. You can do this by adding the following CSS to your CodePen pen:

.accordion .card-header {
   background-color: #f8f9fa !important;
}

Customizing the Collapsed and Expanded States

You can also customize the appearance of the collapsed and expanded states. Here's an example of changing the border color and adding a transition effect:

.accordion .card {
   border-top: 1px solid rgba(0,0,0,.125);
   transition: border-color .3s ease-in-out;
}
.accordion .show {
   border-color: #17a2b8;
}

With these customizations, you can create an accordion that fits seamlessly into your project's design. The possibilities are endless, so don't be afraid to experiment with different styles and effects.

In conclusion, creating a Bootstrap accordion using CodePen is a straightforward process that involves setting up the structure, adding items, and customizing the styles. Whether you're building a website or a web application, an accordion can be a valuable tool for organizing and displaying content. So, go ahead and give it a try – you might be surprised at how much time and effort it can save you.