I got the code below from the official bootstrap 5 demos and for the life of me, can't figure out how to move the off-canvas menu from Left-to-Right. The documentation code to place the offcanvas-start and the demo code is completely different.
I am creating a landing page in Bootstrap 5, where the default menu should be replaced by an off-canvas menu with a close icon.
<!doctype html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.rtl.min.css">
<title>Hello, world!</title>
</head>
<body>
<div >
<!-- Static navbar -->
<nav aria-label="Main navigation">
<div >
<a href="#">Offcanvas navbar</a>
<button type="button" id="navbarSideCollapse" aria-label="Toggle navigation">
<span ></span>
</button>
<div id="navbarsExampleDefault">
<ul >
<li >
<a aria-current="page" href="#">Dashboard</a>
</li>
<li >
<a href="#">Notifications</a>
</li>
<li >
<a href="#">Profile</a>
</li>
<li >
<a href="#">Switch account</a>
</li>
<li >
<a href="#" id="dropdown01" data-bs-toggle="dropdown" aria-expanded="false">Settings</a>
<ul aria-labelledby="dropdown01">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
</ul>
</li>
</ul>
<form >
<input type="search" placeholder="Search" aria-label="Search">
<button type="submit">Search</button>
</form>
</div>
</div>
</nav>
<!-- Main component for a primary marketing message or call to action -->
<div >
<h1>Offcanvas Navbar example</h1>
<p>This example demonstrates using the offcanvas plugin with the navbar.</p>
</div>
<p>
By default the navbar is show on the right side of the screen. You can show it on the left side instead by
adding <code>.navmenu-fixed-left</code> to the <code>.navbar-offcanvas</code>.
</p>
</div> <!-- /container -->
<script src="js/bootstrap.bundle.min.js"></script>
</body>
</html>
<!doctype html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div >
<!-- Static navbar -->
<nav aria-label="Main navigation">
<div >
<a href="#">Offcanvas navbar</a>
<button type="button" id="navbarSideCollapse" aria-label="Toggle navigation">
<span ></span>
</button>
<div id="navbarsExampleDefault">
<ul >
<li >
<a aria-current="page" href="#">Dashboard</a>
</li>
<li >
<a href="#">Notifications</a>
</li>
<li >
<a href="#">Profile</a>
</li>
<li >
<a href="#">Switch account</a>
</li>
<li >
<a href="#" id="dropdown01" data-bs-toggle="dropdown" aria-expanded="false">Settings</a>
<ul aria-labelledby="dropdown01">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
</ul>
</li>
</ul>
<form >
<input type="search" placeholder="Search" aria-label="Search">
<button type="submit">Search</button>
</form>
</div>
</div>
</nav>
<!-- Main component for a primary marketing message or call to action -->
<div >
<h1>Offcanvas Navbar example</h1>
<p>This example demonstrates using the offcanvas plugin with the navbar.</p>
</div>
<p>
By default the navbar is show on the right side of the screen. You can show it on the left side instead by adding <code>.navmenu-fixed-left</code> to the <code>.navbar-offcanvas</code>.
</p>
</div>
<!-- /container -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
CodePudding user response:
I made a guess on the source of your example. It has CSS specific to it and JavaScript specific to it.
Here I added the CSS in the snippet CSS block and the JavaScript in that block
Perhaps download instead of copy from the example site? FWIW I just used the browser to look into the actual source CSS in play here.
(function () {
'use strict'
document.querySelector('#navbarSideCollapse').addEventListener('click', function () {
document.querySelector('.offcanvas-collapse').classList.toggle('open')
})
})()
html,
body {
overflow-x: hidden; /* Prevent scroll on narrow devices */
}
body {
padding-top: 56px;
}
@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 .3s ease-in-out, visibility .3s ease-in-out;
}
.offcanvas-collapse.open {
visibility: visible;
transform: translateX(-100%);
}
}
.nav-scroller {
position: relative;
z-index: 2;
height: 2.75rem;
overflow-y: hidden;
}
.nav-scroller .nav {
display: flex;
flex-wrap: nowrap;
padding-bottom: 1rem;
margin-top: -1px;
overflow-x: auto;
color: rgba(255, 255, 255, .75);
text-align: center;
white-space: nowrap;
-webkit-overflow-scrolling: touch;
}
.nav-underline .nav-link {
padding-top: .75rem;
padding-bottom: .75rem;
font-size: .875rem;
color: #6c757d;
}
.nav-underline .nav-link:hover {
color: #007bff;
}
.nav-underline .active {
font-weight: 500;
color: #343a40;
}
.text-white-50 { color: rgba(255, 255, 255, .5); }
.bg-purple { background-color: #6f42c1; }
<!doctype html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div >
<!-- Static navbar -->
<nav aria-label="Main navigation">
<div >
<a href="#">Offcanvas navbar</a>
<button type="button" id="navbarSideCollapse" aria-label="Toggle navigation">
<span ></span>
</button>
<div id="navbarsExampleDefault">
<ul >
<li >
<a aria-current="page" href="#">Dashboard</a>
</li>
<li >
<a href="#">Notifications</a>
</li>
<li >
<a href="#">Profile</a>
</li>
<li >
<a href="#">Switch account</a>
</li>
<li >
<a href="#" id="dropdown01" data-bs-toggle="dropdown" aria-expanded="false">Settings</a>
<ul aria-labelledby="dropdown01">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
</ul>
</li>
</ul>
<form >
<input type="search" placeholder="Search" aria-label="Search">
<button type="submit">Search</button>
</form>
</div>
</div>
</nav>
<!-- Main component for a primary marketing message or call to action -->
<div >
<h1>Offcanvas Navbar example</h1>
<p>This example demonstrates using the offcanvas plugin with the navbar.</p>
</div>
<p>
By default the navbar is show on the right side of the screen. You can show it on the left side instead by adding <code>.navmenu-fixed-left</code> to the <code>.navbar-offcanvas</code>.
</p>
</div>
<!-- /container -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>