How to build an accordion or collapse component in Bootstrap?
To create a Bootstrap fold or accordion component, you need to first introduce the CSS and JS files of Bootstrap, and then implement it using data-bs-toggle="collapse" and the correct structure. 1. Include Bootstrap CSS and JS: Introduce style sheets and JavaScript bundle files with Popper via CDN. 2. Create a basic collapse: Use data-bs-toggle="collapse" and data-bs-target to point to the target element ID to achieve button control of the visible and hidden content. 3. Build a complete accordion: wrap multiple collapsed items in a container with class="accordion", each item uses the accordion-item class, set data-bs-toggle="collapse" and data-bs-target on the button, add the accordion-collapse collapse class and point to the parent container ID with data-bs-parent to ensure that only one item is expanded, and set the default expansion item by adding the show class. Key points include ensuring that the ID is unique, using data-bs-parent correctly to prevent multiple openings, and avoiding version confusion. Functions can be achieved without customizing JavaScript. The structure is correct and can run normally. The final effect is to expand the corresponding panel when clicking and the other panels will automatically collapse, completing an interactive accordion component.

Building an according or collapse component in Bootstrap is straightforward — you don't need to write any custom JavaScript because Bootstrap provides built-in functionality using data-bs-toggle="collapse" and proper markup. Here's how to do it step by step.

✅ 1. Include Bootstrap CSS and JS
Make sure you're including Bootstrap's CSS and JavaScript in your HTML. You can use the CDN:
<!-- CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"> <!-- JS (including Popper and Bootstrap JS) --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" defer></script>
The bundle version includes Popper, which is required for toolstips and some components.

? 2. Create a Basic Collapse (Single Toggle)
If you just want a simple show/hide element (not an according), use this:
<!-- Button to toggle -->
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample">
Toggle Content
</button>
<!-- Collapsible content -->
<div class="collapse" id="collapseExample">
<div class="card card-body mt-2">
This content appears and disappears when you click the button.
</div>
</div>-
data-bs-toggle="collapse"tells Bootstrap this button controls a collapse. -
data-bs-target="#collapseExample"points to the ID of the element to toggle.
? 3. Build a Full Accordion (Multiple Collapsible Items)
An accordingion lets you expand one item at a time (others automatically close). Use Bootstrap's accordion class:

<div class="accordion" id="myAccordion">
<!-- Item 1 -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne">
Section 1
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#myAccordion">
<div class="accordion-body">
Content for section 1. This is visible by default because of the 'show' class.
</div>
</div>
</div>
<!-- Item 2 -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo">
Section 2
</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#myAccordion">
<div class="accordion-body">
Content for section 2. Starts collapsed.
</div>
</div>
</div>
<!-- Item 3 -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree">
Section 3
</button>
</h2>
<div id="collapseThree" class="accordion-collapse collapse" data-bs-parent="#myAccordion">
<div class="accordion-body">
Content for section 3.
</div>
</div>
</div>
</div>? Key Points:
- Wrap all items in a container with
class="accordion"and give it an ID (eg,myAccordion). - Each item must have
class="accordion-item". - Use
data-bs-parent="#myAccordion"so only one panel opens at a time. - Add
showclass to make a panel open by default. - Buttons get
collapsedwhen closed — Bootstrap updates this automatically via JS.
?️ Optional: Customize Appearance
You can style the according with custom CSS. For example, remove borders or change background:
.accordion-button {
background-color: #f8f9fa;
border-bottom: 1px solid #ddd;
}
.accordion-button:not(.collapsed) {
background-color: #e9ecef;
color: #000;
}❗ Common Pitfalls
- Forgot
data-bs-parent→ all panels stay open. - Missing Bootstrap JS → collapse won't work.
- Using wrong Bootstrap version (eg, v4 vs v5 syntax differences slightly).
- Duplicate IDs in
data-bs-targetandid.
Basically, just follow the structure and classes Bootstrap expects, and it works out of the box. No extra JavaScript needed.
The above is the detailed content of How to build an accordion or collapse component in Bootstrap?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20519
7
13632
4
How to animate Bootstrap components for a more dynamic feel? (Advanced CSS)
Mar 13, 2026 am 01:13 AM
The fade and show classes of Bootstrap5 are only status markers and do not contain animation logic. You need to manually add @keyframes or transitions to achieve fade-in and fade-out; custom components need to configure additional animation rules, and when JS controls the display, the show class must be added with delay to ensure redrawing.
How to ensure your Bootstrap site is fully accessible (WCAG)? (Accessibility)
Mar 08, 2026 am 01:25 AM
The Bootstrap component does not automatically add the aria attributes required for complete WCAG. You need to manually complete aria-expanded, role="dialog", aria-labelledby, etc.; the form must be equipped with an explicit label; :focus needs to use :focus-visible to ensure that the keyboard focus is visible; the color contrast must reach 4.5:1.
How to create a professional-looking landing page with Bootstrap? (Project-Based)
Mar 10, 2026 am 12:22 AM
Bootstrap’sdefaultstylingfeelsgenericduetouncustomizedspacing,typography,andcolor—fixableviaCSSvariablesorSass,notdirectedits;avoidfixedwidths,misuseofgridclasses,andrender-blockingassetstoensureresponsivenessandfastLCP.
How to integrate Bootstrap with a JavaScript framework like React or Vue? (Integration)
Mar 12, 2026 am 01:22 AM
Only BootstrapCSS should be introduced in React/Vue instead of JS to avoid global style pollution and jQuery dependency; SCSS modules should be imported on demand to reduce the size; interactive functions must be imported with encapsulation libraries (such as react-bootstrap) or ESM; grid classes need to adapt to JSX/Vue syntax, and explicit and implicit classes should be used with SSR with caution.
How to properly install and set up Bootstrap 5 in your web project? (Getting Started)
Mar 16, 2026 am 12:40 AM
To correctly introduce Bootstrap5, you need to load bootstrap.min.css first, then load bootstrap.bundle.min.js (including Popper), and ensure that the viewport tag exists; after npm installation, you need to manually introduce compiled files in the dist directory. SCSS customization must be preceded by variable declarations, and JS components must be explicitly initialized to avoid jQuery contamination.
How to implement a carousel or image slider with Bootstrap? (Content Display)
Mar 07, 2026 am 01:01 AM
Need not. Bootstrap5's carousel is automatically initialized by default. You need to manually call newbootstrap.Carousel() only when data-bs-ride is modified or the element is dynamically inserted.
How to use Bootstrap utility classes to speed up your workflow? (Efficiency)
Mar 11, 2026 am 12:18 AM
Use mt-3 to represent the top margin, mb-3 to represent the bottom margin, and the suffixes t/b/s/e correspond to top/bottom/start/end respectively; ms-auto implements right alignment in the flex container, me-2 replaces the abandoned mr-2, and responsive classes such as mt-md-4 need to have breakpoint prefixes.
How to use Bootstrap variables to maintain a consistent color scheme? (Branding)
Mar 14, 2026 am 12:59 AM
The safest way is to redefine Bootstrap's Sass variables (such as $primary, $primary-rgb, $primary-text-emphasis) in advance and introduce them before @import "bootstrap/scss/variables" to ensure that all dependent variables are updated synchronously to avoid compilation failure or style exceptions.





