在CSS中在悬停时更改Nav元素的颜色

时间:2018-10-28 03:44:22

标签: css

当我将鼠标悬停在导航栏本身上时,如何获得“品牌标志”和导航栏右侧的所有li变为黑色。导航已在悬停上更改为#FFF:

https://codepen.io/thomaschsu/pen/qJvVWm

HTML:

<nav id="nav">
  <div class="nav-wrapper">
    <span id="logo-container" class="brand-logo">Example</span>
    <div>
      <ul class="right">
        <li class="active"><a href="/">About</a></li>
        <li><a href="/contact">Contact</a></li>
        <li><a href="/portfolio">Portfolio</a></li>
        <li><a href="/resume">Resume</a></li>
        <li><a class="waves-effect email-btn" href="mailto:example@gmail.com">email me</a></li>
      </ul>
    </div>
  </div>
</nav>

CSS:

body {
  background-color: #222;
}

nav:hover {
  background: #fff;
  -webkit-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
  color: #222;
}

nav .nav-wrapper {
  position: relative;
  height: 100%;
}

nav {
  background: transparent;
  -webkit-box-shadow: none;
  box-shadow: none;
  color: #fff;
  width: 100%;
  height: 90px;
  line-height: 56px;
  display: block;
}

nav .brand-logo {
  position: absolute;
  color: #fff;
  display: inline-block;
  font-size: 2.1rem;
  padding: 0;
  white-space: nowrap;
}

nav ul li.active {
  background-color: rgba(0, 0, 0, 0.1);
}

nav ul a:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

nav ul li {
  transition: background-color 0.3s;
  float: left;
  padding: 0;
}
nav ul a {
  transition: background-color 0.3s;
  font-size: 1rem;
  color: #fff;
  display: block;
  padding: 0 15px;
  cursor: pointer;
}

.right {
  float: right;
}
ul:not(.browser-default) {
  padding-left: 0;
  list-style-type: none;
}
.email-btn {
  background: transparent;
  border: 1px solid #fff;
  border-radius: 20px;
}
.waves-effect {
  position: relative;
  cursor: pointer;
  display: inline-block;
  overflow: hidden;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  vertical-align: middle;
  z-index: 1;
  transition: 0.3s ease-out;
}

我已经尝试在nav:hover上添加“ color:#222”,这对导航中的任何文本都没有影响。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用此:

nav:hover #logo-container, nav:hover ul li a {
  color: #000;  
}

这意味着:当用户将鼠标悬停在导航栏上时,“内部” #logo容器和链接的文本颜色必须更改为黑色。