1.

Bootstrap Collapse example?

Answer»

The Bootstrap collapse is used to show and hide content. The following are used for Collapsible:

.collapse: hides content .collapse.show: to show content

Bootstrap collapse with .collapse class

Let us see an example of Bootstrap collapse with .collapse class. It shows the content hidden by default:

<!DOCTYPE html> <html lang="en"> <head>   <title>Bootstrap Demo</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.1.3/css/bootstrap.min.css">   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> </head> <body> <div class="CONTAINER">   <h2>Bootstrap Collapsible</h2>   <p>Show or hide the content:</p>   <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#mydiv">Click me</button>   <div id="mydiv" class="collapse">     This is the content which is toggled on button click.   </div> </div> </body> </html>

The following is the output that shows toggled content (show or hide) on button click. FIRST, let us see the result on running:

After that, click the above button that toggles content. On clicking, the collapsed content will be visible:

Bootstrap collapse with .collapse show

Let us see an example of .collapse with .show class. It hides the content by default:

<!DOCTYPE html> <html lang="en"> <head>   <title>Bootstrap Demo</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.1.3/css/bootstrap.min.css">   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> </head> <body> <div class="container">   <h2>Bootstrap Collapsible</h2>   <p>Show or hide the content:</p>   <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#mydiv">Click me</button>   <div id="mydiv" class="collapse show">     This is the content which is toggled on button click.   </div> </div> </body> </html>

The following is the output that displays the content by default since we used the show class as well:



Discussion

No Comment Found