Off-canvas Navigation For Bootstrap 5

Category: Javascript , Menu & Navigation | January 5, 2021
Views Total:13,875 views
Official Page:Go to website
Last Update:January 5, 2021


Off-canvas Navigation For Bootstrap 5


By default, Bootstrap 5 collapses nav items behind a toggle button, which enables mobile users to reveal the navbar component in a dropdown manner.

This project helps you generate a responsive off-canvas navigation from Bootstrap 5’s navbar component that enables the navbar panel to slide out from the side of the screen instead.

How to use it:

1. Add the data-bs-toggle="offcanvas" attribute to the Bootstrap 5 navbar component.

  class="navbar-toggler p-0 border-0"
  aria-label="Toggle navigation"

2. Add the offcanvas-collapse class to the nav list.

  class="navbar-collapse offcanvas-collapse"
  <ul class="navbar-nav me-auto mb-2 mb-lg-0">

3. The necessary CSS styles for the off-canvas navigation.

@media (max-width: 991.98px) {
  .offcanvas-collapse {
    position: fixed;
    top: 56px; /* Height of navbar */
    bottom: 0;
    left: 100%;
    width: 100%;
    padding-right: 1rem;
    padding-left: 1rem;
    overflow-y: auto;
    visibility: hidden;
    background-color: #343a40;
    transition: transform 0.3s ease-in-out, visibility 0.3s ease-in-out;
  } {
    visibility: visible;
    transform: translateX(-100%);

4. The main script to enable the Off-canvas Navigation.

(function mainScript() {
  "use strict";
  const offcanvasToggle = document.querySelector(
  const offcanvasCollapse = document.querySelector(".offcanvas-collapse");
  offcanvasToggle.addEventListener("click", function () {

You Might Be Interested In:

One thought on “Off-canvas Navigation For Bootstrap 5

  1. adminyourplace

    Hi, can’t get this to work properly, missing the data-bs-target property?


Leave a Reply