Lesson 36 of 60 – Bootstrap Accordion
60%

Bootstrap Accordion

Bootstrap Accordion is a component used to display multiple sections of content that can be expanded and collapsed. It is useful when you want to organize a large amount of information in a small amount of space.

Note: Bootstrap Accordion uses the Collapse JavaScript component. It is commonly used for FAQs, course lessons, documentation, settings, and other expandable content.

1. What Is an Accordion?

An Accordion is a user interface component that displays content in expandable and collapsible sections.

For example, an FAQ section can contain questions that users can click to reveal their answers.

  • FAQ sections
  • Course topics
  • Documentation
  • Product information
  • Settings panels
  • Student information

2. Basic Accordion Structure

A Bootstrap Accordion normally uses the .accordion container along with .accordion-item, .accordion-header, and .accordion-collapse.

<div class="accordion" id="myAccordion">

    <div class="accordion-item">

        <h2 class="accordion-header">

            <button
            class="accordion-button"
            type="button">

            Section 1

            </button>

        </h2>

        <div class="accordion-collapse">

            <div class="accordion-body">

            Content

            </div>

        </div>

    </div>

</div>

3. The .accordion Class

The .accordion class creates the main Accordion container.

<div
class="accordion"
id="myAccordion">

    ...

</div>

The Accordion container normally has a unique id so its child sections can be connected correctly.

4. The .accordion-item Class

Each section of an Accordion is placed inside an .accordion-item.

<div class="accordion-item">

    ...

</div>

An Accordion can contain multiple Accordion items.

5. Accordion Header

The .accordion-header class is used for the heading section of an Accordion item.

<h2 class="accordion-header">

    ...

</h2>

The heading normally contains the button that opens or closes the Accordion content.

6. Accordion Button

The .accordion-button class styles the button used to expand or collapse an Accordion section.

<button
class="accordion-button"
type="button"
data-bs-toggle="collapse"
data-bs-target="#collapseOne">

First Section

</button>

The data-bs-target points to the content that should be shown or hidden.

7. Accordion Collapse

The .accordion-collapse class is used for the collapsible content area.

<div
id="collapseOne"
class="accordion-collapse collapse">

    ...

</div>

The collapse class provides the collapsible behavior.

8. Accordion Body

The .accordion-body class contains the actual content inside an Accordion section.

<div class="accordion-body">

    This is the Accordion content.

</div>

You can place text, links, lists, images, forms, tables, and other Bootstrap components inside the Accordion body.

9. First Accordion Example

The following example creates a simple Accordion with one expandable section.

<div class="accordion" id="accordionExample">

    <div class="accordion-item">

        <h2 class="accordion-header"
        id="headingOne">

            <button
            class="accordion-button"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#collapseOne">

            What is Bootstrap?

            </button>

        </h2>

        <div
        id="collapseOne"
        class="accordion-collapse collapse show"
        data-bs-parent="#accordionExample">

            <div class="accordion-body">

            Bootstrap is a popular front-end framework.

            </div>

        </div>

    </div>

</div>

Bootstrap is a popular front-end framework.

10. The collapse Show Class

Adding the show class to the collapsible section makes that section visible when the page loads.

<div
class="accordion-collapse collapse show">

    ...

</div>

If show is removed, the section starts collapsed.

11. data-bs-parent

The data-bs-parent attribute connects an Accordion item to the main Accordion container.

<div
class="accordion-collapse collapse"
data-bs-parent="#myAccordion">

    ...

</div>

When used in a standard Accordion, it allows the Accordion behavior where opening one section closes another section in the same parent.

12. Multiple Accordion Items

An Accordion can contain many sections.

<div class="accordion"
id="myAccordion">

    <div class="accordion-item">
        ...
    </div>

    <div class="accordion-item">
        ...
    </div>

    <div class="accordion-item">
        ...
    </div>

</div>

Each item should have its own unique IDs and matching data-bs-target and ARIA references.

13. Accordion with Three Sections

The following example demonstrates three Accordion sections.

HTML is used to structure web pages.

CSS is used to style web pages.

JavaScript is used to add interactivity to web pages.

14. Accordion with FAQ

Accordion is commonly used for Frequently Asked Questions.

Bootstrap is a front-end framework containing CSS utilities, components, and JavaScript-powered interactive components.

Yes. Bootstrap uses a mobile-first responsive system.

15. Accordion Without Parent

The data-bs-parent attribute is not mandatory for every Collapse component. Without it, multiple sections can remain open at the same time.

<div
class="accordion-collapse collapse"
id="sectionOne">

    ...

</div>

<div
class="accordion-collapse collapse"
id="sectionTwo">

    ...

</div>

This can be useful when you want independent expandable sections.

16. Accordion Button State

Bootstrap automatically changes the visual state of an .accordion-button based on whether the related section is expanded or collapsed.

<button
class="accordion-button collapsed"
type="button"
data-bs-toggle="collapse"
data-bs-target="#sectionOne"
aria-expanded="false">

Section

</button>

The aria-expanded attribute communicates the expanded state to assistive technologies.

17. aria-expanded Attribute

The aria-expanded attribute indicates whether the related Accordion section is currently expanded.

<button
class="accordion-button"
type="button"
data-bs-toggle="collapse"
data-bs-target="#sectionOne"
aria-expanded="true"
aria-controls="sectionOne">

Section One

</button>

Use true for an expanded section and false for a collapsed section.

18. aria-controls Attribute

The aria-controls attribute identifies the collapsible element controlled by the Accordion button.

<button
class="accordion-button"
data-bs-toggle="collapse"
data-bs-target="#courseContent"
aria-controls="courseContent">

Course Content

</button>

The value should match the id of the controlled element.

19. Accordion with Lists

You can place lists inside an Accordion body.

<div class="accordion-body">

    <ul>

        <li>HTML</li>
        <li>CSS</li>
        <li>JavaScript</li>

    </ul>

</div>

  • HTML
  • CSS
  • JavaScript
  • Bootstrap

20. Accordion with Tables

Tables can also be placed inside an Accordion body.

<div class="accordion-body">

    <table class="table">

        <tr>
            <th>Course</th>
            <th>Duration</th>
        </tr>

        <tr>
            <td>HTML</td>
            <td>1 Month</td>
        </tr>

    </table>

</div>

Course Duration
HTML 1 Month
CSS 1 Month
Bootstrap 2 Months

21. Accordion with Forms

Forms can be placed inside an Accordion when different sections of a form need to be organized.

<div class="accordion-body">

    <label
    class="form-label">

    Student Name

    </label>

    <input
    type="text"
    class="form-control">

</div>

This approach can be useful for long registration forms or profile forms.

22. Accordion for Course Content

Learning websites can use Accordion sections to organize course lessons.

  • Introduction to HTML
  • HTML Elements
  • HTML Attributes
  • HTML Forms

  • Introduction to CSS
  • Selectors
  • Colors
  • Layouts

23. Flush Accordion

Use .accordion-flush to remove the outer borders and rounded corners from the Accordion.

<div
class="accordion accordion-flush"
id="flushAccordion">

    ...

</div>

This is a flush Accordion without the normal outer rounded container appearance.

24. Accordion Accessibility

Accessibility should be considered when creating Accordion components.

  • Use proper heading elements for Accordion headers.
  • Use descriptive button text.
  • Use aria-expanded to communicate state.
  • Use aria-controls to identify controlled content.
  • Ensure buttons are keyboard accessible.
  • Do not hide essential information only inside collapsed content.
Tip: Accordion buttons should clearly describe the content that will appear when the user expands them.

25. Common Accordion Mistakes

Avoid these common Accordion mistakes:

  • Using duplicate IDs.
  • Using the wrong data-bs-target.
  • Forgetting the collapse class.
  • Forgetting the Bootstrap JavaScript bundle.
  • Using unclear Accordion titles.
  • Incorrectly matching aria-controls.
  • Using the same ID for multiple sections.
  • Putting important information only inside a collapsed section.

26. Accordion with JavaScript

Bootstrap Accordion is based on the Collapse component and can also be controlled with JavaScript.

const element =
document.getElementById('courseOne');

const collapse =
new bootstrap.Collapse(
element,
{
toggle: false
}
);

collapse.show();

The show() method opens the collapsible section.

27. Hiding an Accordion Section with JavaScript

The Collapse API provides methods for controlling Accordion sections.

const element =
document.getElementById('courseOne');

const collapse =
bootstrap.Collapse.getInstance(
element
);

collapse.hide();

The hide() method closes the section.

28. Accordion Events

Because Accordion sections use Bootstrap's Collapse component, you can listen for Collapse events.

Some useful events include:

  • show.bs.collapse
  • shown.bs.collapse
  • hide.bs.collapse
  • hidden.bs.collapse
const section =
document.getElementById('courseOne');

section.addEventListener(
'shown.bs.collapse',
function(){

    console.log(
    'Accordion section opened'
    );

});

29. Practical FAQ Accordion

The following example creates a practical FAQ Accordion.

<div
class="accordion"
id="faqProject">

    <div class="accordion-item">

        <h2
        class="accordion-header"
        id="faqHeadingOne">

            <button
            class="accordion-button"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#faqCollapseOne"
            aria-expanded="true"
            aria-controls="faqCollapseOne">

            How do I enroll?

            </button>

        </h2>

        <div
        id="faqCollapseOne"
        class="accordion-collapse collapse show"
        aria-labelledby="faqHeadingOne"
        data-bs-parent="#faqProject">

            <div class="accordion-body">

            Complete the admission form and submit your details.

            </div>

        </div>

    </div>

</div>

Complete the admission form and submit your details.

30. Complete Bootstrap Accordion Example

This complete example demonstrates multiple Accordion sections with headings, content, ARIA attributes, and the parent relationship.

<div
class="accordion"
id="completeAccordion">


    <div class="accordion-item">

        <h2
        class="accordion-header"
        id="completeHeadingOne">

            <button
            class="accordion-button"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#completeOne"
            aria-expanded="true"
            aria-controls="completeOne">

            HTML Course

            </button>

        </h2>


        <div
        id="completeOne"
        class="accordion-collapse collapse show"
        aria-labelledby="completeHeadingOne"
        data-bs-parent="#completeAccordion">

            <div class="accordion-body">

            Learn HTML elements, attributes, forms, tables,
            links, images, and semantic HTML.

            </div>

        </div>

    </div>


    <div class="accordion-item">

        <h2
        class="accordion-header"
        id="completeHeadingTwo">

            <button
            class="accordion-button collapsed"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#completeTwo"
            aria-expanded="false"
            aria-controls="completeTwo">

            CSS Course

            </button>

        </h2>


        <div
        id="completeTwo"
        class="accordion-collapse collapse"
        aria-labelledby="completeHeadingTwo"
        data-bs-parent="#completeAccordion">

            <div class="accordion-body">

            Learn selectors, colors, typography, layouts,
            Flexbox, Grid, and responsive design.

            </div>

        </div>

    </div>


    <div class="accordion-item">

        <h2
        class="accordion-header"
        id="completeHeadingThree">

            <button
            class="accordion-button collapsed"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#completeThree"
            aria-expanded="false"
            aria-controls="completeThree">

            Bootstrap Course

            </button>

        </h2>


        <div
        id="completeThree"
        class="accordion-collapse collapse"
        aria-labelledby="completeHeadingThree"
        data-bs-parent="#completeAccordion">

            <div class="accordion-body">

            Learn Bootstrap components, utilities,
            responsive layouts, forms, navigation, and projects.

            </div>

        </div>

    </div>


</div>

Learn HTML elements, attributes, forms, tables, links, images, and semantic HTML.

Learn selectors, colors, typography, layouts, Flexbox, Grid, and responsive design.

Learn Bootstrap components, utilities, responsive layouts, forms, navigation, and practical projects.

📌 Key Points

  • .accordion creates the main Accordion container.
  • .accordion-item creates each Accordion section.
  • .accordion-header contains the section heading.
  • .accordion-button creates the expandable button.
  • .accordion-collapse contains the collapsible area.
  • .accordion-body contains the section content.
  • data-bs-toggle="collapse" enables the Collapse behavior.
  • data-bs-target identifies the section to open or close.
  • data-bs-parent connects sections to an Accordion parent.
  • The show class makes a section initially visible.
  • aria-expanded communicates the expanded state.
  • aria-controls identifies the controlled section.
  • .accordion-flush creates a flush Accordion style.
  • Accordion bodies can contain text, lists, tables, forms, and other components.
  • Bootstrap Accordion is based on the Collapse JavaScript component.

🧠 Quick Quiz

Question: Which Bootstrap class is used to create the main Accordion container?