I am currently using Bootstrap 5 to design a website and I'm facing an issue with creating a navbar dropdown. On desktop, I want the dropdown to open on hover and redirect the user to a new page when clicked. However, on mobile devices, I only want the dropdown to be clickable for opening and closing without navigating to a different page. The current setup opens the dropdown on hover and leads to another page on desktop, but on mobile it's not clickable and redirects the user to another page. I understand that achieving this functionality requires JavaScript, which I am unfamiliar with. Can someone please assist me in implementing this?
Desired Dropdown Functionality
Desktop/Laptop Dropdown Functionality
- Opens/closes on hover
- Redirects to another page when clicked
Mobile/Tablet Dropdown Functionality
- Open/closes by clicking
- No redirection to another page
#navbar-color {
background-color: hsl(210, 45%, 30%);
}
.navbar-light .navbar-toggler-icon {
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%28255, 255, 255, 1%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");
}
.navbar-light .navbar-toggler {
border: 1px solid hsl(0, 0%, 100%);
outline: none;
box-shadow: none;
}
#nav_object {
position: relative;
}
.dropdown:hover .dropdown-menu {
display: block;
background-color: hsl(210, 45%, 30%);
}
.dropdown-item:hover {
background-color: hsl(210, 55%, 41%);
}
#nav_object {
position: relative;
}
@media (min-width: 576px) {
#nav_object::after {
content: '';
opacity: 0;
transition: all 0.2s;
height: 2px;
width: 100%;
background: hsl(18, 100%, 62%);
position: absolute;
bottom: 0;
left: 0;
}
#nav_object:hover::after {
opacity: 1;
}
}
.dropdown-menu{
--bs-dropdown-link-hover-bg: hsl(210, 55%, 41%) !important;
}
@media screen and (max-width: 576px) {
.dropdown:hover > .dropdown-menu {
display: block;
margin-top: 0;
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Home | </title>
<link rel="stylesheet" href="/_bridgetown/static/css/main.c7d4dd3f1984a290e9be.css" />
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e4c41415a5d5a5c4f5e6e1b001d001e">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="./css/index.css" />
<link
rel="stylesheet"
data-purpose="Layout StyleSheet"
title="Default"
disabled
href="/css/app-937c1ff7d52fd6f78dd9322599e2b5d4.css?vsn=d"
>
<link
rel="stylesheet"
data-purpose="Layout StyleSheet"
title="Web Awesome"
href="/css/app-wa-8d95b745961f6b33ab3aa1b98a45291a.css?vsn=d"
>
<link
rel="stylesheet"
href="https://site-assets.fontawesome.com/releases/v6.4.0/css/all.css"
>
<link
rel="stylesheet"
href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-solid.css"
>
<link
rel="stylesheet"
href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-regular.css"
>
<link
rel="stylesheet"
href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-light.css"
>
<link rel="icon" type="image/x-icon" href="/Bootstrap-/images/favicon-32x32.png">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfuniqo.udnoubecrumtelen"":""vPProuEqrdgctrdrfoootdarecepnoemDe>-pTlyten<">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</body>
<!-- NAVIGATION BAR START-->
<nav class="navbar navbar-expand-lg navbar-light py-3 sticky-top" id="navbar-color">
<div class="container">
<a href="/Bootstrap-/index.html">
<img src="./src/img/logo-topnav.png" height="45" width="225" class="img-fluid"/>
</a>
<button
type="button"
class="navbar-toggler"
data-bs-toggle="collapse"
data-bs-target="#navmenu"
>
<span class="navbar-toggler-icon">
</button>
<div class="collapse navbar-collapse" id="navmenu">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a href="/Bootstrap-/index.html" class="nav-link text-white" id="nav_object">Home</a></li>
<li class="nav-item"><a href="/Bootstrap-/about.html" class="nav-link text-white" id="nav_object">About</a></li>
</ul>
</div>
</div>
</nav>
<!--NAVIGATION BAR CLOSE-->
</body>
</html>