Collapse in Bootstrap 5
Bootstrap Collapse
Bootstrap provides a Collapse component that allows you to show and hide content with a toggle button or link.
The content can be anything such as text, images, videos, or other HTML elements.
As an example:
Editor
In this example:
- A button with the class
btn btn-primaryis used as the toggle button. - Thedata-bs-toggleattribute is set to collapse and thedata-bs-targetattribute is set to the ID of the content that will be shown or hidden. In this case, the ID iscollapseExample. - The content that will be shown or hidden is contained within a
divelement with the classescollapseandcard card-body. - The id attribute of this div element matches the value of the
data-bs-targetattribute in the toggle button. - When the toggle button is clicked, the content with the matching
idattribute will be shown or hidden.
tip
You can also add multiple collapse components to a page by using unique IDs for each one
Accordion
An Accordion component that allows you to display collapsible content in a vertically stacked list of items.
Each item in the list contains a toggle button or link that can be used to show or hide the corresponding content.
As an example:
Editor
In this example:
- A
divelement with the classaccordionis used to contain the accordion items. - Each accordion item is contained within a
divelement with the class accordion-item. Each accordion item contains an h2 element with the classaccordion-headerand a toggle button or link that is used to show or hide the corresponding content. - The content for each accordion item is contained within a
divelement with the classaccordion-collapseand the correspondingidattribute. - The
data-bs-parentattribute is used to specify the parent element of the accordion items.