Bootstrap5 Collapse
Bootstrap5 Collapse can easily show and hide content.
data-bs-toggle and data-bs-target are custom data attributes used to define component behavior and target elements.
1. data-bs-toggle attribute:
- Used to define the behavior of a component, indicating when it is triggered.
- The values that can be set depend on the specific component type, such as
"collapse"(collapse menu),"modal"(modal),"tab"(tab page), etc. data-bs-toggle="collapse"Used to trigger the expansion and collapse of a collapse menu.data-bs-toggle="modal"Used to trigger the display and hiding of a modal.data-bs-toggle="tab"Used to trigger the switching of tab pages.
2. data-bs-target attribute:
- Used to specify the target element or target selector of a component.
- The target element can be a CSS selector used to identify the specific element to operate on.
- It can also be a specified element ID, used together with the
#, such asdata-bs-target="#myModal"。 data-bs-targetThe attribute anddata-bs-toggleattribute to associate the component behavior with the target element.
In the following example, when the button is clicked, the data-bs-toggle="collapse" attribute triggers the collapse menu behavior, and the data-bs-target="#demo" attribute specifies that the target element of the collapse menu is the
.
Example
<button data-bs-toggle="collapse" data-bs-target="#demo" Collapse button>
<div id="demo" class="collapse">Here is some test content...
div
Try it »
Example Parsing
The .collapse class is used to specify a collapsible element (the
in the example); clicking the button toggles between hidden and shown.
To control the hiding and showing of content, you need to add the or