Table of Contents
1. Change Accordion Header and Body Styling
2. Remove Default Arrow and Add Custom Icons
Example using Bootstrap Icons:
3. Adjust Spacing and Borders
4. Customize Collapse Animation (Optional)
5. Make It Always Open One Panel (Default Behavior)
6. Optional: Allow Multiple Panels Open
Home Web Front-end Bootstrap Tutorial How to customize the Bootstrap accordion

How to customize the Bootstrap accordion

Aug 21, 2025 am 09:25 AM

Customize accordion header and body styles using CSS to modify background, color, borders, and typography. 2. Replace default arrows with custom icons like plus/minus by integrating Bootstrap Icons and toggling via CSS content. 3. Adjust spacing and borders by removing default styles and applying box-shadow, borderRadius, and margins for a card-like design. 4. Optionally customize collapse animation speed using transition properties or disable it with JavaScript. 5. Ensure only one panel opens at a time by using data-bs-parent to group accordion items. 6. Allow multiple panels to remain open by removing or modifying the data-bs-parent attribute in HTML or JavaScript. Customization is achieved through CSS overrides and minor HTML or JavaScript adjustments, enabling a fully branded and user-friendly accordion component.

How to customize the Bootstrap accordion

Customizing the Bootstrap accordion is a common task when you want to match it to your site’s design or improve usability. Whether you're using Bootstrap 4 or 5, the default accordion can be styled and modified with a mix of CSS overrides and optional JavaScript tweaks. Here's how to do it effectively.

How to customize the Bootstrap accordion

1. Change Accordion Header and Body Styling

The default accordion uses subtle styling. To make it stand out, start by customizing the header (button) and body (collapse content).

/* Customize accordion button */
.accordion-button {
  background-color: #f8f9fa;
  color: #212529;
  font-size: 1rem;
  border: 1px solid #dee2e6;
  border-radius: 8px;
  margin-bottom: 8px;
}

/* Remove default focus outline or style it */
.accordion-button:focus {
  box-shadow: none;
  border-color: #007bff;
}

/* Style active (open) state */
.accordion-button:not(.collapsed) {
  background-color: #e9ecef;
  color: #0056b3;
  box-shadow: none;
}

/* Customize accordion body */
.accordion-body {
  background-color: #ffffff;
  border-top: 1px solid #dee2e6;
  padding: 1rem;
  font-size: 0.95rem;
  color: #495057;
}

Note: The .not(.collapsed) class is automatically toggled by Bootstrap’s JavaScript to indicate open panels.

How to customize the Bootstrap accordion

2. Remove Default Arrow and Add Custom Icons

Bootstrap uses a background chevron that rotates on open/close. You can replace it with custom icons (e.g., plus/minus) using Font Awesome or Bootstrap Icons.

Example using Bootstrap Icons:

<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css" rel="stylesheet">

Update your accordion button:

How to customize the Bootstrap accordion
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne">
  Section 1 
  <i class="bi bi-plus-circle ms-auto"></i>
</button>

Then use CSS to swap icons based on state:

.accordion-button:not(.collapsed) .bi-plus-circle::before {
  content: "\f18c"; /* minus-circle icon */
}

.accordion-button.collapsed .bi-plus-circle::before {
  content: "\f18e"; /* plus-circle icon */
}

Alternatively, you can hide the default icon and manage visibility with two separate icons.

3. Adjust Spacing and Borders

Control the overall look by adjusting padding, margins, and borders.

.accordion-item {
  border: none;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  margin-bottom: 10px;
}

.accordion-header {
  border-bottom: none;
}

This gives a more modern, card-like appearance.

4. Customize Collapse Animation (Optional)

Bootstrap uses CSS transitions for collapsing. You can tweak the speed:

.accordion-collapse {
  transition: height 0.3s ease;
}

Or disable animation entirely by removing the collapse class and handling it via JavaScript.

5. Make It Always Open One Panel (Default Behavior)

This is already default in Bootstrap, but ensure you’re using data-bs-parent to link panels:

<div class="accordion" id="myAccordion">
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-controls="collapseOne" data-bs-parent="#myAccordion">
        Section 1
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse" data-bs-parent="#myAccordion">
      <div class="accordion-body">Content 1</div>
    </div>
  </div>
  <!-- More items -->
</div>

The data-bs-parent ensures only one panel opens at a time.

6. Optional: Allow Multiple Panels Open

Remove data-bs-parent or set it to a different container or null in JavaScript:

// Allow multiple open panels
document.querySelectorAll('.accordion-button').forEach(button => {
  button.addEventListener('click', () => {
    // Do not enforce parent grouping
  });
});

Or simply omit data-bs-parent from the HTML.


Customizing the Bootstrap accordion doesn’t require overriding JavaScript—just smart CSS and minor HTML tweaks. Focus on the .accordion-button, .accordion-body, and parent .accordion-item classes, and you can achieve a fully branded look. Basically, just override the styles you don’t like and enhance the interaction with icons or animations.

The above is the detailed content of How to customize the Bootstrap accordion. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1594
276
How to install and use Bootstrap Icons library? How to install and use Bootstrap Icons library? Jul 27, 2025 am 01:25 AM

There are three ways to install and use BootstrapIcons: 1. Use CDN and add links to the HTML head; 2. Install through npm, suitable for modern projects such as React and Vue. You need to run npminstallbootstrap-icons and import CSS; 3. Manually download SVG or font files and import them. When using it, you can add bi and icon name classes (such as bi-heart) to insert icons. You can also use other inline elements such as span. It is recommended to use SVG files for better performance and customization capabilities. You can adjust the size through bi-lg, bi-2x and other classes, and use Bootstrap text such as text-danger.

Does Bootstrap 5 require jQuery? Does Bootstrap 5 require jQuery? Aug 03, 2025 am 01:37 AM

Bootstrap5doesnotrequirejQuery,asithasbeencompletelyremovedtomaketheframeworklighterandmorecompatiblewithmodernJavaScriptpractices.Theremovalwaspossibleduetodroppedsupportforolderbrowsers,enablingtheuseofmodernES6 JavaScriptfeaturesforbetterperforman

How to change the height of the Bootstrap navbar? How to change the height of the Bootstrap navbar? Jul 28, 2025 am 12:50 AM

Adjusting the height of the Bootstrap navigation bar can be achieved by the following methods: 1. Use custom CSS to modify the padding-top and padding-bottom values of .navbar to directly control the height; 2. Adjust the font size and line height of .navbar-nav.nav-link indirectly change the height to enhance responsive adaptability; 3. Set styles for .navbar-brand and .nav-item separately, such as height, line-height or use flex layout to optimize vertical alignment; 4. Use Bootstrap's built-in spacing tools such as p-3, py-4, etc. to quickly adjust the inner margins to affect the overall height. Palm

How to make an image responsive in Bootstrap? How to make an image responsive in Bootstrap? Aug 03, 2025 am 04:11 AM

TomakeanimageresponsiveinBootstrap,addthe.img-fluidclasstothetag;thisappliesmax-width:100%andheight:auto,ensuringtheimagescalesproportionallywithinitscontainerwithoutoverflowing;1.Use;2.Worksinsidegrids,cards,oranycontainer;3.Avoidfixedwidthsthatcanb

How to use Bootstrap forms? How to use Bootstrap forms? Aug 05, 2025 am 08:34 AM

The key to using Bootstrap forms is to master its structure and use of classes. 1. The basic form structure uses form-control, form-label, form-text and form-check to style input, label, help text and check boxes; 2. Horizontal forms are displayed in line with labels and controls by combining grid systems (such as col-sm-*), and inline forms use practical classes such as d-flex to replace the removed form-inline in Bootstrap5; 3. Form verification uses is-valid or is-invalid classes to match valid-feedback and invalid-feedback to display inversely.

Bootstrap navbar dropdown cut off by screen edge Bootstrap navbar dropdown cut off by screen edge Jul 28, 2025 am 01:11 AM

When Dropdown is truncated, you should give priority to using Bootstrap's .dropdown-menu-end class to expand the menu to the left. 1. Adding this class to dropdown-menu can solve the problem of incomplete display on the right menu 2. If it is still invalid, check whether the parent container has overflow:hidden and adjust it 3. In complex layouts, position:absolute and inset can be set through custom CSS 4. JavaScript can be used to dynamically calculate the position but not required.

How to customize Bootstrap with Sass? How to customize Bootstrap with Sass? Jul 29, 2025 am 12:47 AM

InstallBootstrapvianpmanduseitsSassfilesinsteadofprecompiledCSS.2.Createacustom.scssfileandoverrideBootstrap’sdefaultvariableslike$primary,$font-family-sans-serif,or$enable-shadowsbeforeimportingBootstrap.3.CustomizecomplexcomponentsusingSassmapssuch

How to use the Bootstrap CDN link? How to use the Bootstrap CDN link? Aug 01, 2025 am 06:01 AM

Using BootstrapCDN, you can quickly introduce CSS and JS with just two links. 1. Add CSS link in HTML: 2. Add JS scripts before: 3. Optionally introduce icon library: and then use etc. This method does not require installation and is suitable for learning and prototyping, but the production environment recommends self-hosting for better control.

See all articles