CSS 悬停颜色更改保持不变

时间:2021-02-19 11:18:02

标签: css

如何使我的 :hover 颜色在滚动链接时保持不变?

这是我试过的:

.main-menu-ul {
    width: 1274px;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
}

nav.main-menu {
    position: fixed;
    z-index: 999;
    background-color: #ffffff;
    box-shadow: 0px 0px 100px rgba(0, 0, 0, 0.1);
    border-radius: 0px 0px 20px 20px;
    padding: 17px 0;
}

nav.main-menu li {
    list-style: none;
}

nav.main-menu a {
    font-family: poppins-regular-400;
    text-decoration: none;
    color: #000000;
    font-size: 14px;
    letter-spacing: 0.2px;
    transition: color 1s ease-in-out;
}

.main-menu-contact {
    width: 91px;
    text-align: center;
    background: #002366;
    box-shadow: 0px 2px 20px rgba(0, 35, 102, 0.25);
    border-radius: 4px;
    height: 32px;
    line-height: 32px;
}

.main-menu-contact a {
    color: #ffffff !important;
}

.menu-dropdown {
    position: relative;
    z-index: 1;
}

.menu-dropdown-container {
    position: absolute;
    visibility: hidden;
    opacity: 0;
    background-color: #ffffff;
    top: 70px;
    left: -30px;
    padding: 55px 10px 10px;
    border-radius: 10px;
    box-shadow: 0px 0px 100px rgba(0, 0, 0, 0.1);
    z-index: -1;
    width: 120px;
    transition: opacity 1s ease-in-out, top 1s ease-in-out, visibility 1s ease-in-out;
}

.menu-dropdown-container li {
    padding-bottom: 10px;
    position: relative;
    left: -150px;
    top: 0;
    transition: left 1s ease-in-out;
}

.custom-arrow {
    border: solid black;
    border-width: 0 1px 1px 0;
    display: inline-block;
    padding: 3px;
    transform: rotate(45deg);
    position: relative;
    bottom: 3px;
    left: 4px;
    transition: border 1s ease-in-out, transform 1s ease-in-out, bottom 1s ease-in-out;
  }

.menu-dropdown:hover .menu-dropdown-container {
    visibility: visible;
    opacity: 1;
    top: -20px;
}

.menu-dropdown:hover .menu-dropdown-container li {
    left: 0;
}

.menu-dropdown:hover .custom-arrow {
    transform: rotate(-135deg);
    border: solid #DF1F00;
    border-width: 0 1px 1px 0;
    display: inline-block;
    padding: 3px;
    bottom: 0px;
}

.menu-dropdown:hover .menu-dropdown a {
    color: #DF1F00;
}
<nav class="main-menu">
  <ul class="main-menu-ul">
    <li class="menu-dropdown">
    <a href="">SERVICES <i class="custom-arrow"></i></a>
      <ul class="menu-dropdown-container">
        <li>
          <a href="">Link 1</a>
        </li>
        <li>
          <a href="">Link 2</a>
        </li>
        <li>
          <a href="">Link 3</a>
        </li>
        <li>
          <a href="">Link 4</a>
        </li>
        <li>
          <a href="">Link 5</a>
        </li>
      </ul>
    </li>
    
    
        <li class="menu-dropdown">
    <a href="">ABOUT US <i class="custom-arrow"></i></a>
      <ul class="menu-dropdown-container">
        <li>
          <a href="">Link 1</a>
        </li>
        <li>
          <a href="">Link 2</a>
        </li>
        <li>
          <a href="">Link 3</a>
        </li>
        <li>
          <a href="">Link 4</a>
        </li>
        <li>
          <a href="">Link 5</a>
        </li>
      </ul>
    </li>
  </ul>
</nav>

如您所见,我已经为箭头完成了。但是为什么主链接“服务”或“关于我们”没有改变颜色?

2 个答案:

答案 0 :(得分:4)

.menu-dropdown:hover > a { 而不是 .menu-dropdown:hover .menu-dropdown a {

这就是原因,因为您在悬停时选择了菜单,而您似乎选择了另一个菜单,但该菜单不存在。

.main-menu-ul {
  width: 1274px;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
}

nav.main-menu {
  position: fixed;
  z-index: 999;
  background-color: #ffffff;
  box-shadow: 0px 0px 100px rgba(0, 0, 0, 0.1);
  border-radius: 0px 0px 20px 20px;
  padding: 17px 0;
}

nav.main-menu li {
  list-style: none;
}

nav.main-menu a {
  font-family: poppins-regular-400;
  text-decoration: none;
  color: #000000;
  font-size: 14px;
  letter-spacing: 0.2px;
  transition: color 1s ease-in-out;
}

.main-menu-contact {
  width: 91px;
  text-align: center;
  background: #002366;
  box-shadow: 0px 2px 20px rgba(0, 35, 102, 0.25);
  border-radius: 4px;
  height: 32px;
  line-height: 32px;
}

.main-menu-contact a {
  color: #ffffff !important;
}

.menu-dropdown {
  position: relative;
  z-index: 1;
}

.menu-dropdown-container {
  position: absolute;
  visibility: hidden;
  opacity: 0;
  background-color: #ffffff;
  top: 70px;
  left: -30px;
  padding: 55px 10px 10px;
  border-radius: 10px;
  box-shadow: 0px 0px 100px rgba(0, 0, 0, 0.1);
  z-index: -1;
  width: 120px;
  transition: opacity 1s ease-in-out, top 1s ease-in-out, visibility 1s ease-in-out;
}

.menu-dropdown-container li {
  padding-bottom: 10px;
  position: relative;
  left: -150px;
  top: 0;
  transition: left 1s ease-in-out;
}

.custom-arrow {
  border: solid black;
  border-width: 0 1px 1px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(45deg);
  position: relative;
  bottom: 3px;
  left: 4px;
  transition: border 1s ease-in-out, transform 1s ease-in-out, bottom 1s ease-in-out;
}

.menu-dropdown:hover .menu-dropdown-container {
  visibility: visible;
  opacity: 1;
  top: -20px;
}

.menu-dropdown:hover .menu-dropdown-container li {
  left: 0;
}

.menu-dropdown:hover .custom-arrow {
  transform: rotate(-135deg);
  border: solid #DF1F00;
  border-width: 0 1px 1px 0;
  display: inline-block;
  padding: 3px;
  bottom: 0px;
}

.menu-dropdown:hover>a {
  color: #DF1F00;
}
<nav class="main-menu">
  <ul class="main-menu-ul">
    <li class="menu-dropdown">
      <a href="">SERVICES <i class="custom-arrow"></i></a>
      <ul class="menu-dropdown-container">
        <li>
          <a href="">Link 1</a>
        </li>
        <li>
          <a href="">Link 2</a>
        </li>
        <li>
          <a href="">Link 3</a>
        </li>
        <li>
          <a href="">Link 4</a>
        </li>
        <li>
          <a href="">Link 5</a>
        </li>
      </ul>
    </li>


    <li class="menu-dropdown">
      <a href="">ABOUT US <i class="custom-arrow"></i></a>
      <ul class="menu-dropdown-container">
        <li>
          <a href="">Link 1</a>
        </li>
        <li>
          <a href="">Link 2</a>
        </li>
        <li>
          <a href="">Link 3</a>
        </li>
        <li>
          <a href="">Link 4</a>
        </li>
        <li>
          <a href="">Link 5</a>
        </li>
      </ul>
    </li>
  </ul>
</nav>

答案 1 :(得分:1)

您还需要在悬停时更改 .menu-dropdown > a 的颜色,因此请执行以下操作:

.main-menu-ul {
    width: 1274px;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
}

nav.main-menu {
    position: fixed;
    z-index: 999;
    background-color: #ffffff;
    box-shadow: 0px 0px 100px rgba(0, 0, 0, 0.1);
    border-radius: 0px 0px 20px 20px;
    padding: 17px 0;
}

nav.main-menu li {
    list-style: none;
}

nav.main-menu a {
    font-family: poppins-regular-400;
    text-decoration: none;
    color: #000000;
    font-size: 14px;
    letter-spacing: 0.2px;
    transition: color 1s ease-in-out;
}

.main-menu-contact {
    width: 91px;
    text-align: center;
    background: #002366;
    box-shadow: 0px 2px 20px rgba(0, 35, 102, 0.25);
    border-radius: 4px;
    height: 32px;
    line-height: 32px;
}

.main-menu-contact a {
    color: #ffffff !important;
}

.menu-dropdown {
    position: relative;
    z-index: 1;
}

.menu-dropdown-container {
    position: absolute;
    visibility: hidden;
    opacity: 0;
    background-color: #ffffff;
    top: 70px;
    left: -30px;
    padding: 55px 10px 10px;
    border-radius: 10px;
    box-shadow: 0px 0px 100px rgba(0, 0, 0, 0.1);
    z-index: -1;
    width: 120px;
    transition: opacity 1s ease-in-out, top 1s ease-in-out, visibility 1s ease-in-out;
}

.menu-dropdown-container li {
    padding-bottom: 10px;
    position: relative;
    left: -150px;
    top: 0;
    transition: left 1s ease-in-out;
}

.custom-arrow {
    border: solid black;
    border-width: 0 1px 1px 0;
    display: inline-block;
    padding: 3px;
    transform: rotate(45deg);
    position: relative;
    bottom: 3px;
    left: 4px;
    transition: border 1s ease-in-out, transform 1s ease-in-out, bottom 1s ease-in-out;
  }

.menu-dropdown:hover .menu-dropdown-container {
    visibility: visible;
    opacity: 1;
    top: -20px;
}

.menu-dropdown:hover .menu-dropdown-container li {
    left: 0;
}

.menu-dropdown:hover .custom-arrow {
    transform: rotate(-135deg);
    border: solid #DF1F00;
    border-width: 0 1px 1px 0;
    display: inline-block;
    padding: 3px;
    bottom: 0px;
}

.menu-dropdown:hover .menu-dropdown a, .menu-dropdown:hover > a {
    color: #DF1F00;
}
<nav class="main-menu">
  <ul class="main-menu-ul">
    <li class="menu-dropdown">
    <a href="">SERVICES <i class="custom-arrow"></i></a>
      <ul class="menu-dropdown-container">
        <li>
          <a href="">Link 1</a>
        </li>
        <li>
          <a href="">Link 2</a>
        </li>
        <li>
          <a href="">Link 3</a>
        </li>
        <li>
          <a href="">Link 4</a>
        </li>
        <li>
          <a href="">Link 5</a>
        </li>
      </ul>
    </li>
    
    
        <li class="menu-dropdown">
    <a href="">ABOUT US <i class="custom-arrow"></i></a>
      <ul class="menu-dropdown-container">
        <li>
          <a href="">Link 1</a>
        </li>
        <li>
          <a href="">Link 2</a>
        </li>
        <li>
          <a href="">Link 3</a>
        </li>
        <li>
          <a href="">Link 4</a>
        </li>
        <li>
          <a href="">Link 5</a>
        </li>
      </ul>
    </li>
  </ul>
</nav>