Accordion
Build vertically collapsing accordions in combination with our Collapse JavaScript plugin.
On this page
How it worksLink to this section: 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.
ExampleLink to this section: 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 itsaria-expanded
attribute totrue
.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample">
<div class="accordion-body">
<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.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Accordion Item #2
</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
<div class="accordion-body">
<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.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Accordion Item #3
</button>
</h2>
<div id="collapseThree" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
<div class="accordion-body">
<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.
</div>
</div>
</div>
</div>
SizesLink to this section: Sizes
Add .accordion-lg
or .accordion-sm
for additional sizes.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
<div class="row">
<div class="col-md-6 col-lg-5">
<div class="accordion accordion-sm" id="accordionExampleSmall">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOneSmall" aria-expanded="true" aria-controls="collapseOneSmall">
Accordion Item #1
</button>
</h2>
<div id="collapseOneSmall" class="accordion-collapse collapse show" data-bs-parent="#accordionExampleSmall">
<div class="accordion-body">
<strong>This is the first 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.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwoSmall" aria-expanded="false" aria-controls="collapseTwoSmall">
Accordion Item #2
</button>
</h2>
<div id="collapseTwoSmall" class="accordion-collapse collapse" data-bs-parent="#accordionExampleSmall">
<div class="accordion-body">
<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.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThreeSmall" aria-expanded="false" aria-controls="collapseThreeSmall">
Accordion Item #3
</button>
</h2>
<div id="collapseThreeSmall" class="accordion-collapse collapse" data-bs-parent="#accordionExampleSmall">
<div class="accordion-body">
<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.
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-lg-7">
<div class="accordion accordion-lg" id="accordionExampleLarge">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOneLarge" aria-expanded="true" aria-controls="collapseOneLarge">
Accordion Item #1
</button>
</h2>
<div id="collapseOneLarge" class="accordion-collapse collapse show" data-bs-parent="#accordionExampleLarge">
<div class="accordion-body">
<strong>This is the first 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.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwoLarge" aria-expanded="false" aria-controls="collapseTwoLarge">
Accordion Item #2
</button>
</h2>
<div id="collapseTwoLarge" class="accordion-collapse collapse" data-bs-parent="#accordionExampleLarge">
<div class="accordion-body">
<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.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThreeLarge" aria-expanded="false" aria-controls="collapseThreeLarge">
Accordion Item #3
</button>
</h2>
<div id="collapseThreeLarge" class="accordion-collapse collapse" data-bs-parent="#accordionExampleLarge">
<div class="accordion-body">
<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.
</div>
</div>
</div>
</div>
</div>
</div>
Always openLink to this section: Always open
Omit the data-bs-parent
attribute on each .accordion-collapse
to make accordion items stay open when another item is opened.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
.accordion-body
, though the transition does limit overflow.
<div class="accordion" id="accordionPanelsStayOpenExample">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseOne" aria-expanded="true" aria-controls="panelsStayOpen-collapseOne">
Accordion Item #1
</button>
</h2>
<div id="panelsStayOpen-collapseOne" class="accordion-collapse collapse show">
<div class="accordion-body">
<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.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseTwo" aria-expanded="false" aria-controls="panelsStayOpen-collapseTwo">
Accordion Item #2
</button>
</h2>
<div id="panelsStayOpen-collapseTwo" class="accordion-collapse collapse">
<div class="accordion-body">
<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.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapseThree" aria-expanded="false" aria-controls="panelsStayOpen-collapseThree">
Accordion Item #3
</button>
</h2>
<div id="panelsStayOpen-collapseThree" class="accordion-collapse collapse">
<div class="accordion-body">
<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.
</div>
</div>
</div>
</div>
Dark variantLink to this section: Dark variant
Deprecated in v5.3.3
Heads up! Dark variants for components are deprecated in Boosted v5.3.3. They are replaced by our contextual
dark mode.
Add data-bs-theme="dark"
to the .accordion
or any ancestor element
to enable a component-specific color mode. Learn more about our color modes.
AccessibilityLink to this section: Accessibility
Please read the collapse accessibility section for more information.
CSSLink to this section: CSS
VariablesLink to this section: Variables
Added in v5.2.0
As part of Boosted’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.
--#{$prefix}accordion-color: #{$accordion-color};
--#{$prefix}accordion-bg: #{$accordion-bg};
--#{$prefix}accordion-transition: #{$accordion-transition};
--#{$prefix}accordion-border-color: #{$accordion-border-color};
--#{$prefix}accordion-border-width: #{$accordion-border-width};
--#{$prefix}accordion-border-radius: #{$accordion-border-radius};
--#{$prefix}accordion-inner-border-radius: #{$accordion-inner-border-radius};
--#{$prefix}accordion-btn-padding-x: #{$accordion-button-padding-x};
--#{$prefix}accordion-btn-padding-y: #{$accordion-button-padding-y};
--#{$prefix}accordion-btn-font-size: #{$accordion-button-font-size}; // Boosted mod
--#{$prefix}accordion-btn-font-weight: #{$accordion-button-font-weight}; // Boosted mod
--#{$prefix}accordion-btn-line-height: #{$accordion-button-line-height}; // Boosted mod
--#{$prefix}accordion-btn-letter-spacing: #{$accordion-button-letter-spacing}; // Boosted mod
--#{$prefix}accordion-btn-color: #{$accordion-button-color};
--#{$prefix}accordion-btn-bg: #{$accordion-button-bg};
// Boosted mod: no --#{$prefix}accordion-btn-icon
// Boosted mod: no --#{$prefix}accordion-btn-icon-width
--#{$prefix}accordion-btn-icon-transform: #{$accordion-icon-transform};
// Boosted mod: no --#{$prefix}accordion-btn-icon-transition
// Boosted mod: no --#{$prefix}accordion-btn-active-icon
// Boosted mod: no --#{$prefix}accordion-btn-focus-box-shadow
--#{$prefix}accordion-body-padding-top: #{$accordion-body-padding-top}; // Boosted mod
--#{$prefix}accordion-body-padding-end: #{$accordion-body-padding-end}; // Boosted mod
--#{$prefix}accordion-body-padding-bottom: #{$accordion-body-padding-bottom}; // Boosted mod
--#{$prefix}accordion-body-padding-start: #{$accordion-body-padding-start}; // Boosted mod
--#{$prefix}accordion-btn-hover-bg: #{$accordion-button-hover-bg}; // Boosted mod
--#{$prefix}accordion-active-color: #{$accordion-button-active-color};
--#{$prefix}accordion-active-bg: #{$accordion-button-active-bg};
Small and large accordion modifier classes are used to update the value of these CSS variables as needed:
--#{$prefix}accordion-btn-font-size: #{$accordion-button-font-size-sm};
--#{$prefix}accordion-btn-line-height: #{$accordion-button-line-height-sm};
--#{$prefix}accordion-btn-letter-spacing: #{$accordion-button-letter-spacing-sm};
--#{$prefix}accordion-btn-font-size: #{$accordion-button-font-size-lg};
--#{$prefix}accordion-btn-line-height: #{$accordion-button-line-height-lg};
--#{$prefix}accordion-btn-letter-spacing: #{$accordion-button-letter-spacing-lg};
Sass variablesLink to this section: Sass variables
$accordion-padding-y: $spacer * .5; // Boosted mod
$accordion-padding-x: 0; // Boosted mod
$accordion-color: null; // Boosted mod: instead of `var(--#{$prefix}body-color)`
$accordion-bg: transparent; // Boosted mod: instead of `var(--#{$prefix}body-bg)`
// stylelint-disable-next-line function-disallowed-list
$accordion-border-width: calc(var(--#{$prefix}border-width) * .5); // Boosted mod
$accordion-border-color: var(--#{$prefix}border-color-subtle); // Boosted mod: instead of `var(--#{$prefix}border-color)`
$accordion-border-radius: var(--#{$prefix}border-radius);
$accordion-inner-border-radius: subtract($accordion-border-radius, #{$accordion-border-width});
$accordion-body-padding-top: $spacer; // Boosted mod
$accordion-body-padding-end: 0; // Boosted mod
$accordion-body-padding-bottom: $spacer * 1.5; // Boosted mod
$accordion-body-padding-start: 0; // Boosted mod
// fusv-disable
$accordion-body-padding-y: $spacer; // Deprecated in Boosted 5.2.3 to divide it in -padding<top | end | bottom |start>
$accordion-body-padding-x: $spacer; // Deprecated in Boosted 5.2.3 to divide it in -padding<top | end | bottom |start>
// fusv-enable
$accordion-button-padding-y: $accordion-padding-y;
$accordion-button-padding-x: $accordion-padding-x;
$accordion-button-color: null; // Boosted mod: instead of `var(--#{$prefix}body-color)`
$accordion-button-bg: var(--#{$prefix}accordion-bg);
$accordion-transition: $btn-transition, border-radius .15s ease;
$accordion-button-hover-bg: var(--#{$prefix}secondary-bg); // Boosted mod
$accordion-button-active-bg: null; // Boosted mod: instead of `var(--#{$prefix}primary-bg-subtle)`
$accordion-button-active-color: $accordion-button-color; // Boosted mod: instead of `var(--#{$prefix}primary-text-emphasis)`
// Boosted mod: no $accordion-button-focus-border-color
// Boosted mod: no $accordion-button-focus-box-shadow
// Boosted mod: no $accordion-icon-width
// Boosted mod: no $accordion-icon-color
// Boosted mod: no $accordion-icon-active-color
// Boosted mod: no $accordion-icon-transition
$accordion-icon-transform: scaleY(-1);
// Boosted mod: no $accordion-button-icon
// Boosted mod: no $accordion-button-active-icon
// Boosted mod: accordion sizes
$accordion-button-font-size: $h3-font-size;
$accordion-button-line-height: null;
$accordion-button-font-weight: $font-weight-bold;
$accordion-button-letter-spacing: $h3-spacing;
$accordion-button-font-size-sm: $h5-font-size;
$accordion-button-line-height-sm: $h5-line-height;
$accordion-button-letter-spacing-sm: $h5-spacing;
$accordion-button-font-size-lg: $h2-font-size;
$accordion-button-line-height-lg: calc(40 / 30); // stylelint-disable-line function-disallowed-list
$accordion-button-letter-spacing-lg: $h2-spacing;
// End mod
UsageLink to this section: Usage
The collapse plugin utilizes a few classes to handle the heavy lifting:
.collapse
hides the content.collapse.show
shows the content.collapsing
is added when the transition starts, and removed when it finishes
These classes can be found in _transitions.scss
.
Via data attributesLink to this section: Via data attributes
Just add data-bs-toggle="collapse"
and a data-bs-target
to the element to automatically assign control of one or more collapsible elements. The data-bs-target
attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse
to the collapsible element. If you’d like it to default open, add the additional class show
.
To add accordion group management to a collapsible area, add the data attribute data-bs-parent="#selector"
Via JavaScriptLink to this section: Via JavaScript
Enable manually with:
const accordionCollapseElementList = document.querySelectorAll('#myAccordion.collapse')
const accordionCollapseList = [...accordionCollapseElementList].map(accordionCollapseEl => new boosted.Collapse(accordionCollapseEl))
OptionsLink to this section: Options
As options can be passed via data attributes or JavaScript, you can append an option name to data-bs-
, as in data-bs-animation="{value}"
. Make sure to change the case type of the option name from “camelCase” to “kebab-case” when passing the options via data attributes. For example, use data-bs-custom-class="beautifier"
instead of data-bs-customClass="beautifier"
.
As of Boosted 5.2.0, all components support an experimental reserved data attribute data-bs-config
that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}'
and data-bs-title="456"
attributes, the final title
value will be 456
and the separate data attributes will override values given on data-bs-config
. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'
.
The final configuration object is the merged result of data-bs-config
, data-bs-
, and js object
where the latest given key-value overrides the others.
Name | Type | Default | Description |
---|---|---|---|
parent | selector, DOM element | null | If parent is provided, then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this is dependent on the card class). The attribute has to be set on the target collapsible area. |
toggle | boolean | true | Toggles the collapsible element on invocation. |
MethodsLink to this section: Methods
All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started, but before it ends. In addition, a method call on a transitioning component will be ignored. Learn more in our JavaScript docs.
Activates your content as a collapsible element. Accepts an optional options object
.
You can create a collapse instance with the constructor, for example:
const bsCollapse = new boosted.Collapse('#myCollapse', {
toggle: false
})
Method | Description |
---|---|
dispose | Destroys an element’s collapse. (Removes stored data on the DOM element) |
getInstance | Static method which allows you to get the collapse instance associated to a DOM element, you can use it like this: boosted.Collapse.getInstance(element) . |
getOrCreateInstance | Static method which returns a collapse instance associated to a DOM element or create a new one in case it wasn’t initialized. You can use it like this: boosted.Collapse.getOrCreateInstance(element) . |
hide | Hides a collapsible element. Returns to the caller before the collapsible element has actually been hidden (e.g., before the hidden.bs.collapse event occurs). |
show | Shows a collapsible element. Returns to the caller before the collapsible element has actually been shown (e.g., before the shown.bs.collapse event occurs). |
toggle | Toggles a collapsible element to shown or hidden. Returns to the caller before the collapsible element has actually been shown or hidden (i.e. before the shown.bs.collapse or hidden.bs.collapse event occurs). |
EventsLink to this section: Events
Boosted’s collapse class exposes a few events for hooking into collapse functionality.
Event type | Description |
---|---|
hide.bs.collapse | This event is fired immediately when the hide method has been called. |
hidden.bs.collapse | This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete). |
show.bs.collapse | This event fires immediately when the show instance method is called. |
shown.bs.collapse | This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete). |
const myCollapsible = document.getElementById('myCollapsible')
myCollapsible.addEventListener('hidden.bs.collapse', event => {
// do something...
})