当您将鼠标悬停在CSS中的其他对象上时,有没有办法让某个对象发生?

时间:2018-04-13 00:01:48

标签: html css

我想让下拉菜单保持相同的文字颜色,同时将鼠标悬停在链接上并将鼠标悬停在下拉列表中(当您将鼠标悬停在下拉菜单上时,会在下拉菜单中将鼠标悬停在链接上方时会改变颜色)。

以下是我目前的情况:

/* 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>
  <a href="products.html">Products</a>
  <a href="contact.html">Contact</a>
  <a href="about.html">About Us</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
        <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

从.topnav a:hover选择器中删除颜色:黑色:

.topnav a:hover {
  background-color: #ddd;
 /* color: black; */
}

这会将所有锚元素保留为颜色#f2f2f2,但活动元素除外。

编辑:

抱歉,我误解了你的问题。改变这个:

.dropdown .dropbtn:hover {
  color: black;
}

对此:

.dropdown:hover .dropbtn {
  color: black;
}

因为链接和.dropbtn都包含在.dropdown中,所以你仍然在它上面盘旋。