Reputation: 13
Dropdown menu disappears when hovering away from the "Company" link and towards the menu itself.
I'm trying to accomplish a hoverable dropdown menu that is accessible without clicking on the link
I've searched and tried various solutions posted on stack but they haven't worked.
.nav-dropdown {
position: absolute;
margin-top: 10px;
width: 200px;
height: 200px;
display: flex;
justify-content: space-around;
align-items: center;
flex-direction: column;
list-style: none;
border-radius: 0 20px 20px 20px;
opacity: 0;
pointer-events: none;
transform: translateY(-10px);
transition: all 0.6s ease;
}
.nav-link:hover + .nav-dropdown {
background-color: white;
opacity: 1;
pointer-events: all;
transform: translate(0px);
box-shadow: 0 5px 5px #999;
color: #cf20fb;
}
.nav-dropdown-link a {
text-decoration: none;
font-size: 18px;
color: #567ff4;
}
.nav-dropdown li {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.nav-dropdown li:hover, a:hover {
color: #cf20fb;
}
<header>
<div class="logo-container">
<img src="images/euro_trans.png" alt="logo" width="300" height="75">
</div>
<nav>
<ul class="nav-links">
<li><a class="nav-link" href="index.html">Home</a></li>
<li><a class="nav-link" href="#">Company<i class="arrow-down"></i></a>
<ul class="nav-dropdown">
<li class="nav-dropdown-link"><a href="#">About</a></li>
<li class="nav-dropdown-link"><a href="#">Transport</a></li>
<li class="nav-dropdown-link"><a href="#">Key Figures</a></li>
</ul>
</li>
<li><a class="nav-link" href="#">Business</a></li>
<li><a class="nav-link" href="#">Career</a></li>
<li><a class="nav-link" href="#">Contact</a></li>
</ul>
</nav>
</header>
Upvotes: 1
Views: 38
Reputation: 80140
There was a gap between your link and your dropdown. I used a pseudoelement on the .nav-link
to fill it so the user has a bit of buffer when mousing down to the dropdown.
Then your dropdown wasn't set to stay visible when it was being hovered over, so I just added .nav-dropdown:hover
to that set of styles so it stays open as long as the mouse is within its bounds.
.nav-dropdown {
position: absolute;
margin-top: 10px;
width: 200px;
height: 200px;
display: flex;
justify-content: space-around;
align-items: center;
flex-direction: column;
list-style: none;
border-radius: 0 20px 20px 20px;
opacity: 0;
pointer-events: none;
transform: translateY(-10px);
transition: all 0.6s ease;
z-index: 2;
}
.nav-link.has-dropdown {
position: relative;
}
.nav-link.has-dropdown:hover:after {
content: '';
position: absolute;
display: block;
top: 0;
left: 0;
right: -30px;
bottom: -30px;
}
.nav-link:hover + .nav-dropdown,
.nav-dropdown:hover {
background-color: white;
opacity: 1;
pointer-events: all;
transform: translate(0px);
box-shadow: 0 5px 5px #999;
color: #cf20fb;
}
.nav-dropdown-link a {
text-decoration: none;
font-size: 18px;
color: #567ff4;
}
.nav-dropdown li {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.nav-dropdown li:hover, a:hover {
color: #cf20fb;
}
<header>
<div class="logo-container">
<img src="images/euro_trans.png" alt="logo" width="300" height="75">
</div>
<nav>
<ul class="nav-links">
<li><a class="nav-link" href="index.html">Home</a></li>
<li><a class="nav-link has-dropdown" href="#">Company<i class="arrow-down"></i></a>
<ul class="nav-dropdown">
<li class="nav-dropdown-link"><a href="#">About</a></li>
<li class="nav-dropdown-link"><a href="#">Transport</a></li>
<li class="nav-dropdown-link"><a href="#">Key Figures</a></li>
</ul>
</li>
<li><a class="nav-link" href="#">Business</a></li>
<li><a class="nav-link" href="#">Career</a></li>
<li><a class="nav-link" href="#">Contact</a></li>
</ul>
</nav>
</header>
Upvotes: 1