How do I get the words in my nav bar to be centered? I want the Home, News and Dropdown to be centered.
/* Navbar container */
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial;
}
/* Links inside the navbar */
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<h1>Simple Pure CSS Drop Down Menu</h1>
<div class="navbar">
Home
News
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
You can use flexbox as mentioned in another answer. Another option is removing float properties on the navbar items, and add display: inline-block. Then center everything by adding text-align: center to the navbar
/* Navbar container */
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial;
text-align: center;
}
/* Links inside the navbar */
.navbar a {
display: inline-block;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* The dropdown container */
.dropdown {
display: inline-block;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<h1>Simple Pure CSS Drop Down Menu</h1>
<div class="navbar">
Home
News
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
You could add the following to .navbar:
display: flex;
justify-content: center;
Related
When hovering the dropdown menu, it will disappear sporadically when hovering over it. I would like to have my dropdown menu able to constantly hover over it without disappear. I could not find a way to fix this issue. I have tried to increase the z-index to 9999 on the class .mydropdown-content but it does not work. Finally, I work around it by setting below:
h1 {
margin-top: 0.5rem;
}
I am looking for a fix rather than a workaround. Thanks.
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #7e57c2;
color: white;
}
/* Dropdown container - needed to position the dropdown content */
.mydropdown {
float: left;
overflow: hidden;
}
/* Style the mydropdown button to fit inside the topnav */
.mydropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
/* Style the mydropdown content (hidden by default) */
.mydropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 99999;
}
/* Style the links inside the mydropdown */
.mydropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a dark background on topnav links and the mydropdown button on hover */
.topnav a:hover, .mydropdown:hover .dropbtn {
background-color: purple;
color: white;
}
/* Add a grey background to mydropdown links on hover */
.mydropdown-content a:hover {
background-color: #CE93D8;
color: black;
}
/* Show the mydropdown menu when the user moves the mouse over the mydropdown button */
.mydropdown:hover .mydropdown-content {
display: block;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="topnav" id="myTopnav">
Home
<div class="mydropdown">
<button class="dropbtn">Menu1
<i class="fa fa-caret-down"></i>
</button>
<div class="mydropdown-content">
Submenu1
Submenu2
</div>
</div>
<div class="mydropdown">
<button class="dropbtn">Menu2
<i class="fa fa-caret-down"></i>
</button>
<div class="mydropdown-content">
Submenu1
Submenu2
Submenu3
</div>
</div>
Menu3
</div>
<h1>My Long Header</h1>
You should add relative position to .topnav element. I removed useless styles, you can check the example below.
.topnav {
background-color: #333;
display: flex;
position: relative;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #7e57c2;
color: white;
}
/* Style the mydropdown button to fit inside the topnav */
.mydropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
/* Style the mydropdown content (hidden by default) */
.mydropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
/* Style the links inside the mydropdown */
.mydropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a dark background on topnav links and the mydropdown button on hover */
.topnav a:hover,
.mydropdown:hover .dropbtn {
background-color: purple;
color: white;
}
/* Add a grey background to mydropdown links on hover */
.mydropdown-content a:hover {
background-color: #ce93d8;
color: black;
}
/* Show the mydropdown menu when the user moves the mouse over the mydropdown button */
.mydropdown:hover .mydropdown-content {
display: block;
}
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
/>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
<div class="topnav" id="myTopnav">
Home
<div class="mydropdown">
<button class="dropbtn">
Menu1
<i class="fa fa-caret-down"></i>
</button>
<div class="mydropdown-content">
Submenu1
Submenu2
</div>
</div>
<div class="mydropdown">
<button class="dropbtn">
Menu2
<i class="fa fa-caret-down"></i>
</button>
<div class="mydropdown-content">
Submenu1
Submenu2
Submenu3
</div>
</div>
Menu3
</div>
<h1>My Long Header</h1>
So I made this navigation bar and there is 1 title which is a dropdown menu. This title has an other font then the others. If you hover over this dropdown menu, the links do have the right font. Can someone help me with this?
/* Navbar */
.topnav {
list-style-type: none;
overflow: hidden;
background-color: #363636;
box-shadow: 0px 6px 13px -7px rgba(0,0,0,0.20);
transition: all .2s ease-in;
font-weight: 700;
text-transform: uppercase;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 24px 26px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
color: #bdbdbd;
}
.topnav .icon {
display: none;
}
/* Dropdown container */
.dropdown {
float: left;
overflow: hidden;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size:16px;
border: none;
outline: none;
color: white;
padding: 24px 26px;
background-color: inherit;
font-family:'PT Sans', sans-serif;
font-style: inherit;
margin: 0;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black ;
padding: 24px 26px;
text-decoration: none;
display: block;
text-align: left;
}
/* Background dropdown links */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<!-- Navbar -->
<div class="topnav" id="myTopnav">
<img src="img/logo.png" alt="logo" />
<div class="max-width">
<section class="gray">
Home
About
Gamemodes
Vote
<div class="dropdown">
<button class="dropbtn">Application </button>
<div class="dropdown-content">
Staff
Donators
</div>
</div>
STORE
☰
</div>
Navbar:
https://i.stack.imgur.com/CLEeO.png
Navbar with dropdown:
https://i.stack.imgur.com/kzEa7.png
The reason is because the rest of your nav links are a elements, but your rogue nav link is a button element.
If the drop-down button isn't meant to look different you could just remove the font styling from it:
/* Dropdown button */
.dropdown .dropbtn {
// font-size:16px;
border: none;
outline: none;
// color: white;
padding: 24px 26px;
background-color: inherit;
// font-family:'PT Sans', sans-serif;
font-style: inherit;
margin: 0;
}
And wrap an a element around the rogue nav link.
I would like to resize my dropdown menu to match the link width. Here is a picture of what I currently have:
Here are the relevant parts of my HTML page:
<body>
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>
Here are the most relevant parts of my CSS stylesheet:
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
overflow: hidden;
background-color: #272424;
font-family: Arial;
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 25%;
}
/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
width: 25%;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
I tried adding width: 100% to .dropdown-content but the width was too large and extended beyond the writing link to the right.
EDIT: As an extra feature, I would like my links to be appear as "boxes" that are connected without any spaces between them instead of having them appear as on one continuous band of black. How can I do that?
I have make some modification related to the css.
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
background-color: #272424;
font-family: Arial;
height:50px
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 25%;
}
/* The dropdown container */
.dropdown {
float: left;
width: 25%;
position: relative;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: 100%;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: -webkit-fill-available;
}
Setting width to -webkit-fill-available will do the change.
If you want to appear as seperate boxes. You need to use flex feature.
Check out my code :I solved that .dropdown-content issue and Also added extra feature as per u r requirement i.e (spaces between them instead of having them appear as on one continuous band of black)
/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
overflow: hidden;
background-color: transparent;
font-family: Arial;
}
/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
border-radius:4px;
margin:6.2px;
background-color: black;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 23.5%;
}
/* The dropdown container */
.dropdown {
float: left;
margin:6px;
border-radius:4px;
overflow: hidden;
background-color: black;
width: 24%;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}
/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
width:23.5%;
position: absolute;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
background-color:white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
#media only screen and (max-width: 900px){
.navbar a {
width:22%;
}
.dropdown{
width:22%;
}
.dropdown-content{
width:22%;
}
}
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
Why Write?
Dollops
Longforms
Technical/ Science
Quotes
Words
Notes
</div>
</div>
</nav>
</div>
I write navbar component in my Angular 6 app.
<div class="navbar">
<ul>
<li>tekst</li>
<li>tekst</li>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</ul>
</div>
and .css file:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #e8d625;
position: fixed;
top: 0;
width: 100%
}
li {
float: left;
}
li a {
display: block;
color: #090909;
text-align: center;
font-weight: bold;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #f7e525;
}
.active {
background-color: #f7e525;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: #77ffb7;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #f7e525;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #80f987;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #80f987;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
but it's not working.
When I use the mouse, dropdown does not display anything.. It's difficult to say but I use https://www.w3schools.com/css/css_dropdowns.asp .
Do not I have any libraries?
And other question. Will I be able to use in dropdown later? Beacuse I use RoutingModule.
Very thanks for all answers.
EDIT:
add left navbar
<ul>
<li>Calendar</li>
<li>info1</li>
<li>info2</li>
<li>info3</li>
<li>info4</li>
</ul>
ul {
list-style-type: none;
margin-top: 45px;
padding: 0;
width: 200px;
background-color: #f1f1f1;
height: 100%;
position: fixed;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
li a:hover {
background-color: #555;
color: white;
}
You need to remove the overflow: hidden rule from ul, otherwise the dropdown won't be visible.
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #e8d625;
position: fixed;
top: 0;
width: 100%;
z-index: 5;
}
li {
float: left;
}
li a {
display: block;
color: #090909;
text-align: center;
font-weight: bold;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #f7e525;
}
.active {
background-color: #f7e525;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: #77ffb7;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #f7e525;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #80f987;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
position: relative;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #80f987;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
ul.side {
list-style-type: none;
margin-top: 45px;
padding: 0;
width: 200px;
background-color: #f1f1f1;
height: 100%;
position: fixed;
z-index: 1;
}
.side li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
.side li a:hover {
background-color: #555;
color: white;
}
<div class="navbar">
<ul>
<li>tekst</li>
<li>tekst</li>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</ul>
</div>
<ul class="side">
<li>Calendar</li>
<li>info1</li>
<li>info2</li>
<li>info3</li>
<li>info4</li>
</ul>
This question already has answers here:
How to affect other elements when one element is hovered
(9 answers)
Closed 4 years ago.
I want to make the dropdown menu stay the same text color while hovering over the links and hovering over dropdown (it changes color when you go from hovering over dropdown to hovering over the links) in the dropdown menu.
Here is what I currently have:
/* Add a black background color to the top navigation */
.topnav {
background-color: #333;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color: white;
}
/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
}
/* Dropdown button */
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
/* Important for vertical align on mobile phones */
margin: 0;
/* Important for vertical align on mobile phones */
}
.dropdown .dropbtn:hover {
color: black;
}
/* Links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover {
background-color: #ddd;
color: black;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
<div class="topnav">
<a class="active" href="index.html">Home</a>
Products
Contact
About Us
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
Remove color:black from the .topnav a:hover selector:
.topnav a:hover {
background-color: #ddd;
/* color: black; */
}
This leaves all anchor elements as color #f2f2f2 except the active one.
EDIT:
Sorry, I misread your question. Change this:
.dropdown .dropbtn:hover {
color: black;
}
To this:
.dropdown:hover .dropbtn {
color: black;
}
Because both the links and .dropbtn are contained within .dropdown, you're still hovering over it.