I've incorporated a drop-down menu into my Bootstrap 4 navbar, but I'm facing an issue with its positioning. Instead of having the drop-down menu appear below the navbar as intended, it shows up somewhere else due to the default top
attribute value being set to 100%
. To address this, I adjusted the top
attribute to a custom value to align the drop-down menu just beneath the navbar. If anyone has a more efficient solution to achieve this alignment without resorting to custom values, I'd appreciate your input.
The modification I made has caused another problem – whenever I try to move the cursor from the menu link to the drop-down menu, the menu disappears. This behavior occurs because the menu is triggered by hovering, not clicking. The images below illustrate this issue:
https://i.sstatic.net/JfxLF.png https://i.sstatic.net/ebBjB.png
This is my current navbar setup (to experience the issue, you need to view the desktop version):
.navbar-brand p {
width: auto;
max-width: 170px;
height: 47px;
}
.navbar {
padding: .25rem 1rem;
}
.nav-item {
padding: 0px 10px;
}
.navbar .nav-item:first-child {
padding-left: 0px;
}
.nav-item:last-child {
padding-right: 0px;
}
.burger-menu {
justify-content: right;
}
.dropdown-menu {
left: unset !important;
min-width: 300px !important;
top: 3.1rem !important;
}
.dropdown:hover > .dropdown-menu {
display: block;
}
.dropdown > .dropdown-toggle:active {
pointer-events: none;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b6a9b6b6a3b4e8acb586f7e8f7f0e8f6">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-white">
<div class="container">
<div class="d-flex flex-grow-1">
<span class="w-100 d-lg-none d-block"><!-- hidden spacer to center brand on mobile --></span>
<a class="navbar-brand" href="#">
<p>My Image</p>
</a>
<div class="w-100 d-flex burger-menu">
<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#myNavbar7" aria-expanded="false">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</div>
<div class="navbar-collapse flex-grow-1 collapse" id="myNavbar7" style="">
<ul class="navbar-nav ml-auto flex-nowrap">
<li class="nav-item">
<a href="#" class="nav-link">Link1</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Link2</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Link3</a>
</li>
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">Menu</a>
<div class="dropdown-menu">
<div class="row">
<div class="col-xl-12">
<a href="" class="nav-link">All Categories</a>
</div>
<div class="col-xl-6">
<a href="#" class="dropdown-item">link</a>
<a href="#" class="dropdown-item">link</a>
<a href="#" class="dropdown-item">link</a>
<a href="#" class="dropdown-item">link</a>
</div>
<div class="col-xl-6">
<a href="#" class="dropdown-item">link</a>
<a href="#" class="dropdown-item">link</a>
<a href="#" class="dropdown-item">link</a>
<a href="#" class="dropdown-item">link</a>
</div>
</div>
</div>
</li>
<li class="nav-item">
<form>
<div class="input-group">
<input type="text" class="form-control border border-right-0" placeholder="Search...">
<span class="input-group-append">
<button class="btn btn-outline-dark border border-left-0" type="button">
<i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
</li>
<li class="nav-item">
<a href="#" class="btn btn-secondary">Login</a>
</li>
<li class="nav-item">
<a href="#" class="btn btn-primary">Register</a>
</li>
</ul>
</div>
</div>
</nav>