You're correct! If both the navbar and the offcanvas menu use the same nav-items, it will cause them to appear twice on wider screens, which is not ideal.
To solve this issue, you can use the offcanvas menu only for mobile views while keeping the main navigation for desktop. You'll want to use one set of nav-items, displayed in the main navbar for desktop and in the offcanvas for mobile without duplicating them.
Here’s the updated code with only one definition of the nav-items, in which the items are used in both the main navbar and the offcanvas. On wider screens, the navbar is displayed normally, while on mobile screens, it will toggle the offcanvas menu:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Bootstrap Template</title>
<style>
footer {
background-color: #343a40; /* Dark grey color */
color: white; /* Text color */
}
</style>
</head>
<body>
<!-- Navbar with Offcanvas for Mobile -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasNavbar" aria-controls="offcanvasNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ms-auto"> <!-- This pushes the nav to the right -->
<li class="nav-item"><a class="nav-link" href="#services">Services</a></li>
<li class="nav-item"><a class="nav-link" href="#portfolio">Portfolio</a></li>
<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
</ul>
</div>
<!-- Offcanvas menu is defined here -->
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasNavbar" aria-labelledby="offcanvasNavbarLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasNavbarLabel">Menu</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" href="#services">Services</a></li>
<li class="nav-item"><a class="nav-link" href="#portfolio">Portfolio</a></li>
<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
</ul>
</div>
</div>
</div>
</nav>
<!-- Header Section with Full Width -->
<header class="container-fluid">
<div class="row py-5">
<div class="col-lg-9">
<img src="https://via.placeholder.com/800x400" class="img-fluid" alt="Header Image">
<div class="caption">This is a caption for the image.</div>
</div>
<div class="col-lg-3 d-none d-md-block">
<h1>Heading 1</h1>
<h1>Heading 2</h1>
<h1>Heading 3</h1>
</div>
</div>
</header>
<main class="container">
<!-- Services Section -->
<section id="services" class="py-5">
<h2>Services</h2>
<p>Details about the services offered.</p>
</section>
<!-- Portfolio Section -->
<section id="portfolio" class="py-5 bg-light">
<h2>Portfolio</h2>
<p>Showcasing the portfolio here.</p>
</section>
<!-- Contact Form Section -->
<section id="contact" class="py-5">
<h2>Contact Us</h2>
<form>
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" required>
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea class="form-control" id="message" rows="3" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</section>
</main>
<!-- Footer -->
<footer class="py-4">
<div class="container text-center">
<p>© 2023 Your Company Name. All rights reserved.</p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.min.js"></script>
</body>
</html>
Explanation:
-
Single Definition of Navigation Items: The nav-items for your navigation are defined only once in the navbar, within the navbar-nav list element. This will display them in the desktop view.
-
Offcanvas Menu: Under the offcanvas section, the same items are again listed to display in the offcanvas menu when the mobile menu icon is clicked. However, it won't conflict since the main navbar will be displayed for larger screen sizes.
Note:
Using the same list for both the desktop and mobile view ensures that the navigation items only show once in the main navbar while still giving you functionality in the mobile view through the offcanvas. If you need even more advanced control or wish to avoid duplication entirely, you could consider using JavaScript to dynamically add or remove elements, but for most use cases, this approach works effectively and is simple to maintain.