Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 58 ยท Components

Accordion and Collapse

Show and hide content with Bootstrap's collapse plugin, build accordions that stay open or close their siblings, and control several panels with data attributes.

Pages often hold more information than fits on a phone screen: FAQ answers, filter panels, long settings. Instead of showing everything at once, you can hide content behind a heading and let the visitor open it. Bootstrap does this with the collapse plugin, and an accordion is simply a set of collapsible panels that follow a fixed markup pattern.

This is the first lesson in the track that needs Bootstrap's JavaScript. The "bundle" script from Installing Bootstrap is already included in every example below. You do not write any JavaScript yourself: the plugin reads data-bs-* attributes from your HTML. A later lesson, Bootstrap JavaScript Plugins, shows the programming side.

How collapse works

A collapsible element has the class collapse, which hides it (display: none). A control, usually a button or a link, points at it and toggles it. Two attributes make that connection:

  • data-bs-toggle="collapse" tells Bootstrap that clicking this element should toggle a panel.
  • data-bs-target="#panel" is a CSS selector for the panel. On a link you can use href="#panel" instead.

While the panel animates, Bootstrap temporarily adds the class collapsing. When it ends open, the panel has collapse show. You should also set aria-expanded and aria-controls on the control, so assistive technology knows what the button does. Bootstrap updates aria-expanded for you after the first click, but you must write the starting value.

flowchart LR
  H["collapse<br/>(hidden)"] -->|click| O["collapsing<br/>(animating)"] --> S["collapse show<br/>(open)"]
  S -->|click| C["collapsing<br/>(animating)"] --> H

The classes on a panel while it opens and closes: collapse, collapsing, collapse show.

A single collapse

Notice that the content sits inside the collapse element, but the card styling is on an inner div. This is deliberate: the plugin animates the height of the collapse element, and padding, margins and borders on that same element make the animation jump. Put spacing on a child, not on the collapse itself. (A top margin like mt-3 is a small exception that works in practice, but wrapping is the safe habit.)

Starting open

To show a panel at page load, write collapse show and set aria-expanded="true" on the control. Plain collapse buttons need no extra class for this; only accordion buttons use collapsed (see below).

An <a> works just as well as a <button>, with href="#details" and role="button". Use a real <button> when the control does not navigate anywhere, because it is keyboard-friendly without extra work.

One button, many panels

A selector can match more than one element. Give several panels the same class and point data-bs-target at that class, and a single click toggles all of them. In the other direction, several buttons can control one panel.

When one control owns several panels, list the ids in aria-controls, separated by spaces.

Toggle several panels at once

Try opening A, then pressing "Toggle both". The plugin toggles each panel individually, so A (which was open) closes while B (which was closed) opens. That is how toggling a group works: each panel flips its own state.

Horizontal collapse

Add collapse-horizontal next to collapse and the panel animates its width instead of its height. Because width has no natural value for a block element, give the child a fixed width, for example style="width: 300px". This is useful for a side filter panel on a desktop layout.

Building an accordion

An accordion repeats one structure. Every item has a header with a button and a collapsible body:

HTML
<div class="accordion" id="faq">
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button" type="button"
              data-bs-toggle="collapse" data-bs-target="#q1"
              aria-expanded="true" aria-controls="q1">Question</button>
    </h2>
    <div id="q1" class="accordion-collapse collapse show" data-bs-parent="#faq">
      <div class="accordion-body">Answer</div>
    </div>
  </div>
</div>

The parts and what they do:

Class Role
accordion the outer wrapper that gets an id
accordion-item one question-and-answer pair with border and radius
accordion-header the heading element (h2, or the level that fits your page)
accordion-button the clickable button with an arrow icon
accordion-collapse + collapse the hidden region
accordion-body padding for the content

The key attribute is data-bs-parent="#faq" on each collapse region. It tells Bootstrap that these panels belong to one group where only one may be open. Opening a second panel closes the first. Items that start closed use collapse without show, and their buttons get the class collapsed and aria-expanded="false".

Choose the heading level to match the page outline. An accordion in a section that already has an h2 should use h3 in its headers; the accordion-header class works on any heading.

A FAQ accordion

Always-open and flush accordions

If you leave out data-bs-parent, each panel works on its own and several can be open together. Everything else stays the same. This "always open" style suits long reference content where visitors compare sections.

For a design without the outer border and rounded corners, add accordion-flush to the wrapper. Flush accordions blend into a card or a page section that already provides its own frame.

Flush, independent panels

Accordions and search. Content inside a collapsed panel is still in the HTML, so search engines can read it, and the browser's find-in-page will not open it. For critical content, consider whether hiding it is really the right choice.

Common mistakes

  • Forgetting the script. Without the bundle, buttons do nothing and closed panels stay hidden forever.
  • A wrong target. data-bs-target="#q1" must match an id exactly. A typo gives no error; the button simply does nothing.
  • Duplicate ids. Two accordions on one page that both use #q1 toggle the wrong panel. Use unique ids.
  • data-bs-parent pointing at the wrong element. It must be the id of the common ancestor, such as the .accordion wrapper.
  • Starting state mismatch. A panel with show but a button with aria-expanded="false" confuses screen reader users.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which pair of attributes makes a button toggle a panel with id="more"?

    data-bs-toggle selects the plugin and data-bs-target is a CSS selector for the panel. Bootstrap 5 uses the data-bs- prefix; the old data-toggle name is Bootstrap 4.
  2. 2. In an accordion, what makes opening one item close the others?

    data-bs-parent groups the panels so only one stays open. Leave it out and the panels act independently.
  3. 3. How do you let visitors keep several accordion items open at once?

    There is no special class. Without data-bs-parent each panel toggles on its own.
  4. 4. What does the accordion-flush class change?

    accordion-flush is purely visual: it drops the outer frame. Behavior stays the same.
  5. 5. Why should padding go on a child of a .collapse element instead of the element itself?

    The plugin animates the height of the collapse element. Padding, margin and borders on it cause a visible jump, so wrap the content in a child.

Summary

  • data-bs-toggle="collapse" plus data-bs-target (or href) toggles any element with the class collapse; show means open.
  • Set aria-expanded and aria-controls on the control, and keep the starting state consistent.
  • One selector can target many panels, and many buttons can control one panel.
  • An accordion uses accordion, accordion-item, accordion-header, accordion-button, accordion-collapse and accordion-body.
  • data-bs-parent makes it "one open at a time"; leave it out for always-open panels; accordion-flush removes the outer frame.
  • Keep spacing on an inner element so the height animation stays smooth.

Next lesson: Modals.