Open In App

Bootstrap 5 Collapse Usage

Bootstrap 5 Collapse is a component used to create collapsible content panels. It allows you to hide and show content on demand. Collapse is useful when you have a lot of content on a page and need to display it in a more organized manner.

You can refer here for further details about Bootstrap 5 Collapse.



Collapse plugin uses few classes to handle the heavy lifting.

?

Collapse Usage:

 



Example 1: In this example, we will demonstrate bootstrap 5 collapses.




<!DOCTYPE html>
<html>
  
<head>
    <title>Bootstrap 5 Collapse Usage</title>
    <link href=
        rel="stylesheet">
    <script src=
    </script>
</head>
  
<body>
    <div class="container text-center">
        <h1 class="text-success">
            GeeksforGeeks
        </h1>
          
        <strong>Bootstrap5 Collapse Usage</strong>
          
        <button type="button" 
            class="btn btn-primary" 
            data-bs-toggle="collapse" 
            data-bs-target="#collapse_id">
            Click me to show data
        </button>
  
        <div id="collapse_id" class="collapse">
            GeeksforGeeks is a Computer Science 
            portal for geeks. It contains well 
            written, well thought and well
            explained computer science and 
            programming articles
        </div>
    </div>
</body>
  
</html>

Output:

 

Example 2: In this example, we will demonstrate the bootstrap 5 collapse option data-bs-parent.




<!DOCTYPE html>
<html>
  
<head>
    <title>Bootstrap 5 Collapse Usage</title>
    <link href=
        rel="stylesheet">
    <script src=
    </script>
</head>
  
<body>
    <div class="container">
        <center>
            <h1 class="text-success">
                GeeksforGeeks
            </h1>
            <strong>
                Bootstrap5 Collapse Usage
            </strong>
            <div id="accordion">
                <button class="btn btn-primary" 
                    data-bs-toggle="collapse" 
                    href="#collapse1">
                    Collapse element 1
                </button>
                <div id="collapse1" 
                    class="collapse show" 
                    data-bs-parent="#accordion">
                    <div>
                        GeeksforGeeks is a Computer 
                        Science portal for geeks.
                    </div>
                </div>
                <br>
                <button class="btn btn-primary" 
                    data-bs-toggle="collapse" 
                    href="#collapse2">
                    Collapse element 2
                </button>
                <div id="collapse2" class="collapse" 
                    data-bs-parent="#accordion">
                    <div>
                        GeeksforGeeks is a Computer 
                        Science portal for geeks.
                    </div>
                </div>
                <br>
                <button class="btn btn-primary" 
                    data-bs-toggle="collapse" 
                    href="#collapse3">
                    Collapse element 3
                </button>
                <div id="collapse3" class="collapse" 
                    data-bs-parent="#accordion">
                    <div>
                        GeeksforGeeks is a Computer 
                        Science portal for geeks.
                    </div>
                </div>
            </div>
        </center>
    </div>
</body>
  
</html>

Output:

 

Reference: https://getbootstrap.com/docs/5.0/components/collapse/#usage


Article Tags :