Bootstrap 4 Collapse

Bootstrap 4 Collapse

Collapse is a bootstrap component that is similar to the dropdown button. Collapse is used to hide or show the content such as paragraphs, sentences, etc.  When we click on a collapse button, a panel will open with a large amount of text.

How to create

The steps that are used to create collapse are given below:-

  • Add the .collapse class and id = “demo” to the <div> element to show/hide the content when we click on collapse button.
  • Add the CSS attribute data-toggle=”collapse” to the <a> (anchor) element or <button> element.
  • Then, add CSS attribute data-target= ”#id” to the <button> element for connecting the collapse button to the content which we want to show/hide.

Note

If you use a <a> element instead of <button> element then use href attribute instead of data-target attribute.

If you want to make your content visible instead of hidden (which is the default), then use .show class.

Example

This example shows how to create collapse using <a> element.

<!DOCTYPE html>
 <html lang="en">
 <head>
   <title>Bootstrap Collapse Example</title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
 </head> 
 <div class="container">
   <h2>Simple Collapse</h2>
   <a href="#demo" class="btn btn-primary" data-toggle="collapse">Collapse Button</a>
   <div id="demo" class="collapse">
     A collapse is a bootstrap component which is similar to the dropdown button. Collapse is used to hide or show the content such as paragraph, sentence etc. When we click on a collapse button, a panel will open with large amount of text.
   </div>
 </div>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
 </body>
 </html> 

Output

Bootstrap Collapse

Example

This example shows how to create a collapse using <button> element.

<!DOCTYPE html>
 <html lang="en">
 <head>
   <title>Bootstrap Collapse Example</title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> 
 </head>
 <div class="container">
   <h2>Simple Collapse</h2>
   <p>Click on the button to toggle between showing and hiding content.</p>
   <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">Simple collapse</button>
   <div id="demo" class="collapse">
     A collapse is a bootstrap component which is similar to the dropdown button. Collapse is used to hide or show the content such as paragraph, sentence etc.  When we click on a collapse button, a panel will open with large amount of text.
   </div>
 </div> 
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
 </body>
 </html> 

Output

Bootstrap Collapse

Accordion

An accordion is a card component. We can create the accordion by extending collapse and adding some classes and attributes.

In accordion, the data-parent attribute is used to ensure that only one collapsible element will be visible at a time.

 Example

<!DOCTYPE html>
 <html lang="en">
 <head>
   <title>Bootstrap Collapse Example</title>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
 </head>
 <div class="container">
   <h2>Accordion</h2>
   <div id="accordion">
     <div class="card"> 
       <div class="card-header">
         <a class="card-link" data-toggle="collapse" href="#collapseOne">
           Collapsible Group Item #1
         </a>
       </div>
       <div id="collapseOne" class="collapse show" data-parent="#accordion">
         <div class="card-body">
           A collapse is a bootstrap component which is similar to the dropdown button. Collapse is used to hide or show the content such as paragraph, sentence etc.  When we click on a collapse button, a panel will open with large amount of text.
         </div>
       </div> 
     </div>
     <div class="card">
       <div class="card-header">
         <a class="collapsed card-link" data-toggle="collapse" href="#collapseTwo">
         Collapsible Group Item #2
       </a>
       </div> 
       <div id="collapseTwo" class="collapse" data-parent="#accordion">
         <div class="card-body">
           A collapse is a bootstrap component which is similar to the dropdown button. Collapse is used to hide or show the content such as paragraph, sentence etc.  When we click on a collapse button, a panel will open with large amount of text.
         </div>
       </div>
     </div>
     <div class="card">
       <div class="card-header">
         <a class="collapsed card-link" data-toggle="collapse" href="#collapseThree">
           Collapsible Group Item #3
         </a>
       </div>
       <div id="collapseThree" class="collapse" data-parent="#accordion"> 
         <div class="card-body">
           A collapse is a bootstrap component which is similar to the dropdown button. Collapse is used to hide or show the content such as paragraph, sentence etc.  When we click on a collapse button, a panel will open with large amount of text.
         </div>
       </div>
     </div>
   </div>
 </div>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
 </body>
 </html> 

Output

Bootstrap Collapse

Related Topics

Bootstrap 4 Icons

Bootstrap 4 Icons Bootstrap 3 contains the default icon library. But in bootstrap 4, you have to include the external icon library by yourself. Many free icon libraries are available such...

2 minutes read.

Bootstrap 4 Scrollspy

Bootstrap 4 Scrollspy Bootstrap 4 offers a scrollspy component that is used to make the website more attractive and eye-catching. Basically, the scrollspy is used on the single-page website. The scrollspy highlights the nav...

5 minutes read.

Bootstrap 4 Colors

Bootstrap 4 Colors Bootstrap 4 consists of classes that are used to set colors according to the context of the element, and with the help of these classes, we can read...

2 minutes read.

Bootstrap Multiselect Dropdown

What is a multi-select Dropdown? Multi-select dropdown lists are used when we want to select multiple options for a particular record. Usually, dropdown lists provide the feature of choosing just one value...

4 minutes read.

Bootstrap 4 Content

Typography Typography is used for styling and formatting the content. It also helps in creating customized headings, sub-headings, lists, paragraphs, etc. Headings Bootstrap 4 offers HTML heading <h1> to <h6> that has bolder...

8 minutes read.

Bootstrap 4 Popover

Bootstrap 4 Popover Popover A popover is similar to the tooltip. The only difference between tooltip and Popover is that in the tooltip, the popup box appears when you move the cursor...

6 minutes read.

Bootstrap 4 Grid System

Bootstrap 4 Grid System The bootstrap 4 grid system is used to create a responsive website layout. The bootstrap 4 offers a mobile-first flexbox grid system that allows you to divide...

4 minutes read.

Bootstrap Input groups

Bootstrap 4 Input groups The input group is a component of Bootstrap 4, which is used to create interactive and stylish form controls. With the help of input groups, you can easily customize the input...

8 minutes read.

Bootstrap Toast Example

What are toasts? Toasts are similar to notifications, and they are lightweight and act as push notifications from mobile and desktop operating systems. They’re built using flexbox in Bootstrap. They can...

4 minutes read.

Dropdown & Dropup

Bootstrap Dropdowns The bootstrap dropdown menu is used to create a toggleable menu. It provides users to choose one value from a predefined list. Before creating dropdown menu, we have to know...

3 minutes read.

Bootstrap Glyphicons

Bootstrap Glyphicon is used to create different type of glyphicons It is used in the web project. It provides 260 Glyphicons from the Glyphivons Halflings set. There are various examples of...

1 minute read.

Bootstrap With CSS

← Prev Next → ...

1 minute read.

Bootstrap 4 Modal

Bootstrap 4 Modal The Modal is a component of Bootstrap 4, which is a dialog box or pop-up window. The modals are used to provide information or warning to the user, such as...

18 minutes read.

Bootstrap 4 Carousel

Bootstrap 4 Carousel Bootstrap 4 offers a carousel component, which is also known as a slider or slideshow. The carousel is used for the dynamic representation of content using images with...

7 minutes read.

Bootstrap 4 navbar

A navigation navbar or navbar uses in every website to make it more user-friendly. Navigating through the website is easier, and the user can instantly search for the topic of...

8 minutes read.

Bootstrap First Example

1) Add HTML5 doctype Before starting the example of Bootstrap, we should know about the some basic rules of Bootstrap. We have to add HTML5 doctype with lang element and CSS properties. <!DOCTYPE html>   <html lang="en">     <head>       <meta charset="utf-8">      </head>   </html> 2) Bootstrap...

2 minutes read.

Bootstrap 4 Tutorial

What is Bootstrap? Bootstrap is a CSS (Cascading Style Sheets) framework, which is used to develop a responsive website. It is an open-source and mobile-first front-end framework. It has design templates that are based...

4 minutes read.

Bootstrap List group

Bootstrap list group provides a group of a list of items. The most basic list is an unordered list. We can use <ul> element with .list-group class and <li> element with .lsit-group-item. Let us see...

3 minutes read.

Bootstrap Searchable Dropdown Menu

Searchable Dropdown Menu When we click on the dropdown button/arrow in this dropdown menu, an input field will pop up with all the dropdown items listing inside. If we type in...

5 minutes read.

Bootstrap Tabs and Pills

Bootstrap Menus Bootstrap menu is used to create different types of menu. It can created horizontal with its .list-inlineclass. <div class="container">       <h3>Example of Bootstrap Menu</h3>       <ul class="list-inline">         <li><a href="#">Home</a></li>         <li><a href="#">About</a></li>         <li><a href="#">Contact</a></li>         <li><a href="#">services</a></li>       </ul>     </div> Try Now Bootstrap Tabs Bootstrap tabl is look like is menu but there is some difference....

4 minutes read.