Loading...
Skip to main content

Collapse

Toggle content visibility by pairing Collapse with a connected CollapseToggle.

How it works

Collapse shows and hides content using its open or defaultOpen prop. Pair it with CollapseToggle and set both components' target props to connect the trigger and content. CSS animates the size from zero to the content size; because of how the transition works, put padding on a child rather than directly on the Collapse component.

The animation needs interpolate-size. Browsers without it open and close the element at once. See Transitions to change the timing or to remove it.

The animation effect depends on the prefers-reduced-motion media query. See the reduced motion accessibility guidance.

Example

Click the buttons below to show and hide another element via class changes:

  • Collapse hides content when closed and reveals it when open is true

Generally, we recommend a button trigger: use CollapseToggle with a target selector. While not recommended from a semantic point of view, passing href also renders an anchor trigger. The component manages the collapse behavior and accessibility attributes.

Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
<>
<p className="d-inline-flex flex-wrap gap-1">
<CollapseToggle
aria-controls="collapseExample"
aria-expanded="false"
href="#collapseExample"
role="button"
target="#collapseExample"
theme="primary"
variant="solid"
>
{' '}Link with href{' '}
</CollapseToggle>
<CollapseToggle
aria-controls="collapseExample"
aria-expanded="false"
target="#collapseExample"
theme="primary"
type="button"
variant="solid"
>
{' '}Button with data-bs-target{' '}
</CollapseToggle>
</p>
<Collapse id="collapseExample">
<Card body>
{' '}Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.{' '}
</Card>
</Collapse>
</>

Horizontal

The component also supports horizontal collapsing. Set horizontal to transition the inline size instead of the block size, and set a width on the immediate child through style or className.

Please note that while the example below has a min-height set to avoid excessive repaints in our docs, this is not explicitly required. Only the width on the child element is required.

This is some placeholder content for a horizontal collapse. It’s hidden by default and shown when triggered.
<>
<p>
<CollapseToggle
aria-controls="collapseWidthExample"
aria-expanded="false"
target="#collapseWidthExample"
theme="primary"
type="button"
variant="solid"
>
{' '}Toggle width collapse{' '}
</CollapseToggle>
</p>
<div
className="overflow-auto"
style={{
minHeight: '140px'
}}
>
<Collapse
horizontal
id="collapseWidthExample"
>
<Card
body
style={{
width: '300px'
}}
>
{' '}This is some placeholder content for a horizontal collapse. It’s hidden by default and shown when triggered.{' '}
</Card>
</Collapse>
</div>
</>

Multiple toggles and targets

A CollapseToggle can show and hide multiple elements by referencing them with a selector in its target prop. Conversely, multiple triggers can show and hide the same element if they each reference it with their target prop.

Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
<>
<p className="d-inline-flex flex-wrap gap-1">
<CollapseToggle
aria-controls="multiCollapseExample1"
aria-expanded="false"
href="#multiCollapseExample1"
role="button"
target="#multiCollapseExample1"
theme="primary"
variant="solid"
>
Toggle first element
</CollapseToggle>
<CollapseToggle
aria-controls="multiCollapseExample2"
aria-expanded="false"
target="#multiCollapseExample2"
theme="primary"
type="button"
variant="solid"
>
Toggle second element
</CollapseToggle>
<CollapseToggle
aria-controls="multiCollapseExample1 multiCollapseExample2"
aria-expanded="false"
target=".multi-collapse"
theme="primary"
type="button"
variant="solid"
>
Toggle both elements
</CollapseToggle>
</p>
<Row>
<Col>
<Collapse
className="multi-collapse"
id="multiCollapseExample1"
>
<Card body>
{' '}Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.{' '}
</Card>
</Collapse>
</Col>
<Col>
<Collapse
className="multi-collapse"
id="multiCollapseExample2"
>
<Card body>
{' '}Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.{' '}
</Card>
</Collapse>
</Col>
</Row>
</>

Accessibility

CollapseToggle keeps aria-expanded and aria-controls synchronized with the targeted Collapse. Use its default button element for the toggle, or set as to another element and provide the appropriate interactive semantics.

CollapseToggle sets aria-controls from its target prop, which identifies the Collapse by selector.

CollapseToggle does not implement the optional keyboard interactions described in the ARIA Authoring Practices Guide accordion pattern; add those interactions when building a full accordion pattern.

CSS

Variables

The collapse transition is controlled by CSS custom properties:

.collapse {
--bs-transition-collapse-property: block-size, display;
--bs-transition-collapse-duration: 0.35s;
--bs-transition-collapse-timing: ease;
}

Styling

Collapse shares its transition styles with Accordion. The horizontal prop changes the animated axis to inline size; the component applies its visibility state automatically.

Usage

The open and defaultOpen props control visibility; CSS handles the transition. Initialization and cleanup are automatic.

Options

NameTypeDefaultDescription
defaultOpenbooleanfalseInitial visibility of an uncontrolled collapse.
openboolean—Controlled visibility.
horizontalbooleanfalseAnimate inline size rather than block size.
parentstring—Parent selector for accordion-like behavior: opening one collapse closes its siblings.

Set parent on the collapsible area, not on its trigger. For native details-based disclosure groups, see Accordion.

Events

Use onOpenChange(open) to respond to visibility changes, or to update the state backing a controlled open prop.

Documentation navigation
On this page
On this page