Bootstrap 4 Collapse

Bootstrap 4 collapse is the collapsible panel that shows and hides the visibility of content. It is required to use few Bootstrap classes to trigger the toggle effect on content. The collapse effect is useful when you want to hide the large size data and display it only on the button or anchor link click.

Let’s find out how to create collapsible content with the example given below.

Basic Bootstrap 4 Collapse Using Button Element

To create a simple collapsible element on button click, you have to use a <button> element with attributes data-toggle="collapse" and data-target="#id" in which id is the id of the <div> element that contains the content part as given below:

Test it Live

Output

Basic Bootstrap 4 Collapse Using Button Element

Explanation: The above example contains the <button > element and the <div> element. The <div> element contains the attribute id="collapseExample" that gets added to the <button> element inside the attribute data-target="#collapseExample". The combination of these elements creates a collapsible element using Bootstrap 4.

Bonus: download a Free Bootstrap CHEAT SHEET that will show you 20+ most important examples to learn in Bootstrap.

Simple Collapse Using Anchor Link Element

Similarly, you can also use the anchor (<a>) link element to create a collapsible element. The only difference is that you have to add the id of the <div> element to the href attribute of <a> element as given below:

Test it Live

Output

Simple Collapse Using Anchor Link Element

The above example is a Bootstrap 4 collapse element using the <a> element. You can click the button ‘Test it Live’ button to test the above example live.

Let’s create an accordion using Bootstrap 4 collapse.

Accordion in Bootstrap 4

An accordion is an element that contains multiple links that display collapsible content on link clicks. You can create an accordion in Bootstrap 4 using a card header and body part. The card header contains the <button> element and the card body contains the content.

You need to add the attributes data-toggle="collapse" and data-target="#id" (where id is the id of the content <div>) to the <button> element. It also requires wrapping everything inside the class .accordion as given below:

Test it Live

Output

Accordion in Bootstrap 4

The above example shows the multiple collapse elements that create an accordion using Bootstrap 4. You can click the panels to open the collapsible content and test the above example live.

Note: Add the attribute data-parent="#id" to the content parent <div> element. Here, id is the id of the <div> element that contains the class .accordion)

Bootstrap 4 Accordion with Plus Minus Icons

To create a Bootstrap 4 accordion with plus/minus icons, you have to use the example given above. After that, add the font awesome icons for plus and minus and use the script given below:

Test it Live

Output

Accordion in Bootstrap 4

At the start, there is only one content item is open with a minus icon and other items are closed with a plus icon. When you click the other collapsible items, it gets opened and the plus icons change into the minus icon.

DOWNLOAD Free Bootstrap CHEAT SHEET

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.