I am utilizing the bootstrap 5 off-canvas feature for collapsing, while also having an announcement bar that appears as a dismissible alert on top of my navbar.
The issue I'm facing is that the top size of my off-canvas is not adjusting properly when I click on the close button of the dismissible alert.
How can I resolve this? I have created a live example on codepen.Io
Note: try resizing the window and closing the alert to observe the issue with my off-canvas.
HTML / CSS / JS :
(function () {
'use strict'
// for Off-Canvas Menu
document.querySelector('[data-bs-toggle="offcanvas"]').addEventListener('click', function () {
document.querySelector('.offcanvas-collapse').classList.toggle('open')
})
})()
@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%);
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 5 off-canvas</title>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f99b96968d8a8d8b9889b9ccd7c9d7c9d49b9c8d98c8">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Alert -->
<div class="alert alert-info alert-dismissible fade show px-0 mb-0 text-start text-lg-center" role="alert">
<div class="container-lg">
<strong>Note!</strong> Lorem Ipsum is simply dummy text of the printing and typesetting industry.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</div>
<!-- Second Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse offcanvas-collapse">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">About</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-light" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<main class="container py-5">
<h1>What is Lorem Ipsum?</h1>
<p class="lead">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
... (repeating content on Lorem Ipsum)
</main>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="583a37372c2b2c2a3928186d76687668753a3d2c3969">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>