Reputation: 55
I have the following code
.navbar {
overflow: hidden;
background-color: #2A3D5A;
position: fixed;
top: 0;
width: 100%;
}
.navbar a {
float: left;
display: block;
color: #69788C;
text-align: center;
padding: 10px;
text-decoration: none;
font-size: 14px;
width: 20%;
}
.navbar a:hover {
background: #4F76B1;
color: white;
}
.navbar a.active {
background: #4F76B1;
color: white;
}
<div class="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
</div>
How to add a dropdown list that contains link 1, link 2 and link 3 when hovering the News tab.
I found many examples on the internet, but I couldn't just add it to this code instead I had to rewrite this code. Can you suggest a simple enhancement to this existing code to do the extra function.
Upvotes: 0
Views: 31
Reputation: 5909
Is this what you're looking for? It should be fairly self-explanatory but drop me a comment if not and I'll explain.
.navbar {
background-color: #2A3D5A;
position: fixed;
top: 0;
width: 100%;
}
.navbar a {
float: left;
display: block;
color: #69788C;
text-align: center;
padding: 10px;
text-decoration: none;
font-size: 14px;
width: 20%;
}
.navbar a:hover {
background: #4F76B1;
color: white;
}
.navbar a.active {
background: #4F76B1;
color: white;
}
#news {
position: relative;
}
.menu {
position: absolute;
display: none;
top: 2rem;
background-color: green;
width: 100%;
padding: 0.25rem;
}
#news:hover .menu {
display: block;
}
.menuitem {
padding-block: 0.125rem;
}
<div class="navbar">
<a href="#home">Home</a>
<a href="#news" id='news'>News
<div class='menu'>
<div class='menuitem'>Link1</div>
<div class='menuitem'>Link2</div>
<div class='menuitem'>Link3</div>
</div>
</a>
<a href="#contact">Contact</a>
</div>
Upvotes: 1