
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.
<button class="navbar-toggler p-0 border-0" type="button" data-bs-toggle="offcanvas" aria-label="Toggle navigation" >
2. Add the offcanvas-collapse class to the nav list.
<div class="navbar-collapse offcanvas-collapse" id="navbarsExampleDefault" > <ul class="navbar-nav me-auto mb-2 mb-lg-0"> ... </div>
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;
}
.offcanvas-collapse.open {
visibility: visible;
transform: translateX(-100%);
}
}4. The main script to enable the Off-canvas Navigation.
(function mainScript() {
"use strict";
const offcanvasToggle = document.querySelector(
'[data-bs-toggle="offcanvas"]',
);
const offcanvasCollapse = document.querySelector(".offcanvas-collapse");
offcanvasToggle.addEventListener("click", function () {
offcanvasCollapse.classList.toggle("open");
});
})();







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