Open In App

Bootstrap 5 Offcanvas Usage

Bootstrap 5 Offcanvas is a sidebar component, which allows you to create a side menu that slides in from the edge of the screen when triggered. To create an off-canvas with Bootstrap 5, you can use the .off-canvas class.

Bootstrap 5 Offcanvas Usage:



var example = document.getElementById('...')
var ... = new bootstrap.Tooltip(example, value)

Syntax:

<tag class="offcanvas" ..>
     ....
</tag>

Example 1: In this example, we will demonstrate how to pass options through data attributes in bootstrap 5 Offcanvas.






<!DOCTYPE html>  
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1">
        
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href=
    <!-- Bootstrap JavaScript -->
    <script src=
    </script>
</head>
<body class="text-center m-3">
    <h1 class="text-success">GeeksforGeeks</h1>
    <h3>Bootstrap 5 Offcanvas usage</h3>
    <button class="btn btn-primary" 
            type="button" 
            data-bs-toggle="offcanvas" 
            data-bs-target="#offcanvasId">
        Click here toggle Offcanvas
    </button>
    <div class="offcanvas offcanvas-top" 
        id="offcanvasId">
        <div class="offcanvas-header">
            <h1 class="text-success">
                GeeksforGeeks</h1>
            <button type="button" 
                    class="text-reset btn-close" 
                    data-bs-dismiss="offcanvas">
            </button>
        </div>
        <div class="offcanvas-body">
                GeeksforGeeks is a Computer Science portal for geeks.
                It contains well written, well thought and well explained 
                computer science and programming articles, quizzes etc.
        </div>
    </div>
</body>
</html>

Output:

Bootstrap 5 Offcanvas Usage

Example 2: In this example, we will demonstrate how we can place the position of offcanvas using class.




<!DOCTYPE html>  
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1">
        
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href=
    <!-- Bootstrap JavaScript -->
    <script src=
    </script>
</head>
<body class="text-center m-3">
    <h1 class="text-success">GeeksforGeeks</h1>
    <h3>Bootstrap 5 Offcanvas usage</h3>
    <button class="btn btn-primary" 
        type="button" 
        data-bs-toggle="offcanvas" 
        data-bs-target="#offcanvasId">
            Click here toggle Offcanvas at left
    </button>
    <button class="btn btn-primary" 
        type="button" 
        data-bs-toggle="offcanvas" 
        data-bs-target="#offcanvasId2">
            Click here toggle Offcanvas at right
    </button>
    <div class="offcanvas offcanvas-start" 
        id="offcanvasId">
        <div class="offcanvas-header">
            <h1 class="text-success">GeeksforGeeks</h1>
            <button type="button" 
                class="text-reset btn-close" 
                data-bs-dismiss="offcanvas">
            </button>
        </div>
        <div class="offcanvas-body">
            GeeksforGeeks is a Computer Science portal for geeks.
            It contains well written, well thought and well explained 
            computer science and programming articles, quizzes etc.
        </div>
    </div>
    <div class="offcanvas offcanvas-end" 
         id="offcanvasId2">
        <div class="offcanvas-header">
            <h1 class="text-success ">GeeksforGeeks</h1>
            <button type="button"
               class="text-reset btn-close" 
               data-bs-dismiss="offcanvas">
            </button>
        </div>
        <div class="offcanvas-body">
            GeeksforGeeks is a Computer Science portal for geeks.
            It contains well written, well thought and well explained
            computer science and programming articles, quizzes etc.
        </div>
    </div>
 </body>
</html>

Output :

Bootstrap 5 Offcanvas Usage

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


Article Tags :