Accordion

Build vertically collapsing accordions in combination with our Collapse JavaScript plugin.

How it works

The accordion uses collapse internally to make it collapsible.

The animation effect of this component is dependent on the prefers-reduced-motion media query. See the reduced motion section of our accessibility documentation.

Example

Click the accordions below to expand/collapse the accordion content.

To render an accordion that’s expanded by default:

  • add the .show class on the .accordion-collapse element.
  • drop the .collapsed class from the .accordion-button element and set its aria-expanded attribute to true.

Accordion - 图1

html

  1. <div class="accordion" id="accordionExample">
  2. <div class="accordion-item">
  3. <h2 class="accordion-header">
  4. <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
  5. Accordion Item #1
  6. </button>
  7. </h2>
  8. <div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample">
  9. <div class="accordion-body">
  10. <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
  11. </div>
  12. </div>
  13. </div>
  14. <div class="accordion-item">
  15. <h2 class="accordion-header">
  16. <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
  17. Accordion Item #2
  18. </button>
  19. </h2>
  20. <div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
  21. <div class="accordion-body">
  22. <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
  23. </div>
  24. </div>
  25. </div>
  26. <div class="accordion-item">
  27. <h2 class="accordion-header">
  28. <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
  29. Accordion Item #3
  30. </button>
  31. </h2>
  32. <div id="collapseThree" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
  33. <div class="accordion-body">
  34. <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
  35. </div>
  36. </div>
  37. </div>
  38. </div>

Flush

Add .accordion-flush to remove some borders and rounded corners to render accordions edge-to-edge with their parent container.

Accordion - 图2

html

  1. <div class="accordion accordion-flush" id="accordionFlushExample">
  2. <div class="accordion-item">
  3. <h2 class="accordion-header">
  4. <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseOne" aria-expanded="false" aria-controls="flush-collapseOne">
  5. Accordion Item #1
  6. </button>
  7. </h2>
  8. <div id="flush-collapseOne" class="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
  9. <div class="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> class. This is the first item's accordion body.</div>
  10. </div>
  11. </div>
  12. <div class="accordion-item">
  13. <h2 class="accordion-header">
  14. <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwo" aria-expanded="false" aria-controls="flush-collapseTwo">
  15. Accordion Item #2
  16. </button>
  17. </h2>
  18. <div id="flush-collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
  19. <div class="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> class. This is the second item's accordion body. Let's imagine this being filled with some actual content.</div>
  20. </div>
  21. </div>
  22. <div class="accordion-item">
  23. <h2 class="accordion-header">
  24. <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseThree" aria-expanded="false" aria-controls="flush-collapseThree">
  25. Accordion Item #3
  26. </button>
  27. </h2>
  28. <div id="flush-collapseThree" class="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">
  29. <div class="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> class. This is the third item's accordion body. Nothing more exciting happening here in terms of content, but just filling up the space to make it look, at least at first glance, a bit more representative of how this would look in a real-world application.</div>
  30. </div>
  31. </div>
  32. </div>

Always open

Omit the data-bs-parent attribute on each .accordion-collapse to make accordion items stay open when another item is opened.

Accordion - 图3

html

  1. <div class="accordion" id="accordionPanelsStayOpenExample">
  2. <div class="accordion-item">
  3. <h2 class="accordion-header">
  4. <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseOne" aria-expanded="true" aria-controls="panelsStayOpen-collapseOne">
  5. Accordion Item #1
  6. </button>
  7. </h2>
  8. <div id="panelsStayOpen-collapseOne" class="accordion-collapse collapse show">
  9. <div class="accordion-body">
  10. <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
  11. </div>
  12. </div>
  13. </div>
  14. <div class="accordion-item">
  15. <h2 class="accordion-header">
  16. <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseTwo" aria-expanded="false" aria-controls="panelsStayOpen-collapseTwo">
  17. Accordion Item #2
  18. </button>
  19. </h2>
  20. <div id="panelsStayOpen-collapseTwo" class="accordion-collapse collapse">
  21. <div class="accordion-body">
  22. <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
  23. </div>
  24. </div>
  25. </div>
  26. <div class="accordion-item">
  27. <h2 class="accordion-header">
  28. <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseThree" aria-expanded="false" aria-controls="panelsStayOpen-collapseThree">
  29. Accordion Item #3
  30. </button>
  31. </h2>
  32. <div id="panelsStayOpen-collapseThree" class="accordion-collapse collapse">
  33. <div class="accordion-body">
  34. <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
  35. </div>
  36. </div>
  37. </div>
  38. </div>

Accessibility

Please read the collapse accessibility section for more information.

CSS

Variables

Added in v5.2.0

As part of Bootstrap’s evolving CSS variables approach, accordions now use local CSS variables on .accordion for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

scss/_accordion.scss

  1. --#{$prefix}accordion-color: #{$accordion-color};
  2. --#{$prefix}accordion-bg: #{$accordion-bg};
  3. --#{$prefix}accordion-transition: #{$accordion-transition};
  4. --#{$prefix}accordion-border-color: #{$accordion-border-color};
  5. --#{$prefix}accordion-border-width: #{$accordion-border-width};
  6. --#{$prefix}accordion-border-radius: #{$accordion-border-radius};
  7. --#{$prefix}accordion-inner-border-radius: #{$accordion-inner-border-radius};
  8. --#{$prefix}accordion-btn-padding-x: #{$accordion-button-padding-x};
  9. --#{$prefix}accordion-btn-padding-y: #{$accordion-button-padding-y};
  10. --#{$prefix}accordion-btn-color: #{$accordion-button-color};
  11. --#{$prefix}accordion-btn-bg: #{$accordion-button-bg};
  12. --#{$prefix}accordion-btn-icon: #{escape-svg($accordion-button-icon)};
  13. --#{$prefix}accordion-btn-icon-width: #{$accordion-icon-width};
  14. --#{$prefix}accordion-btn-icon-transform: #{$accordion-icon-transform};
  15. --#{$prefix}accordion-btn-icon-transition: #{$accordion-icon-transition};
  16. --#{$prefix}accordion-btn-active-icon: #{escape-svg($accordion-button-active-icon)};
  17. --#{$prefix}accordion-btn-focus-box-shadow: #{$accordion-button-focus-box-shadow};
  18. --#{$prefix}accordion-body-padding-x: #{$accordion-body-padding-x};
  19. --#{$prefix}accordion-body-padding-y: #{$accordion-body-padding-y};
  20. --#{$prefix}accordion-active-color: #{$accordion-button-active-color};
  21. --#{$prefix}accordion-active-bg: #{$accordion-button-active-bg};

Sass variables

scss/_variables.scss

  1. $accordion-padding-y: 1rem;
  2. $accordion-padding-x: 1.25rem;
  3. $accordion-color: var(--#{$prefix}body-color);
  4. $accordion-bg: var(--#{$prefix}body-bg);
  5. $accordion-border-width: var(--#{$prefix}border-width);
  6. $accordion-border-color: var(--#{$prefix}border-color);
  7. $accordion-border-radius: var(--#{$prefix}border-radius);
  8. $accordion-inner-border-radius: subtract($accordion-border-radius, $accordion-border-width);
  9. $accordion-body-padding-y: $accordion-padding-y;
  10. $accordion-body-padding-x: $accordion-padding-x;
  11. $accordion-button-padding-y: $accordion-padding-y;
  12. $accordion-button-padding-x: $accordion-padding-x;
  13. $accordion-button-color: var(--#{$prefix}body-color);
  14. $accordion-button-bg: var(--#{$prefix}accordion-bg);
  15. $accordion-transition: $btn-transition, border-radius .15s ease;
  16. $accordion-button-active-bg: var(--#{$prefix}primary-bg-subtle);
  17. $accordion-button-active-color: var(--#{$prefix}primary-text-emphasis);
  18. // fusv-disable
  19. $accordion-button-focus-border-color: $input-focus-border-color; // Deprecated in v5.3.3
  20. // fusv-enable
  21. $accordion-button-focus-box-shadow: $btn-focus-box-shadow;
  22. $accordion-icon-width: 1.25rem;
  23. $accordion-icon-color: $body-color;
  24. $accordion-icon-active-color: $primary-text-emphasis;
  25. $accordion-icon-transition: transform .2s ease-in-out;
  26. $accordion-icon-transform: rotate(-180deg);
  27. $accordion-button-icon: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='#{$accordion-icon-color}' stroke-linecap='round' stroke-linejoin='round'><path d='M2 5L8 11L14 5'/></svg>");
  28. $accordion-button-active-icon: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='#{$accordion-icon-active-color}' stroke-linecap='round' stroke-linejoin='round'><path d='M2 5L8 11L14 5'/></svg>");