无法覆盖活动链接上的线性渐变

时间:2017-12-13 16:08:57

标签: css css-transitions gradient nav

我有一个引导程序导航,我已经添加了一个简单的CSS转换。当用户将鼠标悬停在链接上时,它会向右擦除并将背景颜色更改为蓝色。当用户在链接页面上.navactive添加到<li>类时。这应该使背景颜色为蓝色,以便用户知道它们在该页面上。线性渐变保持了这一点。

有没有办法

  • 覆盖过渡并使其变为实体
  • 禁用类
  • 中.navactive链接的转换

由于

.nav li {
  /* Old browsers */
  background: linear-gradient(to right, #0943A7 50%, #FFFFFF 50%);
  background-size: 200% 100%;
  background-position: right bottom;
  margin-left: 10px;
  transition: all 1s ease;
}

.nav li:hover{
  background-position: left bottom;
}

.navactive {
  background-color: #0943A7 !important;
  overflow: hidden;
  transform: skewX(-18deg);
  -webkit-transform: skewX(-18deg);
  -ms-transform: skewX(-18deg);
}

HTML:

<nav class="navbar navbar-expand-lg navbar-light bg-faded">
  <div class="container">
   <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="true" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse show" id="navbarResponsive">
      <ul class="nav justify-content-center mx-md-auto">

        <li class="<?php echo ($page == "home" ? "nav-item navactive" : "nav-item")?>">
          <a class="nav-link" href="index.php">Home</a>
        </li>

        <li class="<?php echo ($page == "page2" ? "nav-item navactive" : "nav-item")?>">
          <a class="nav-link" href="#">Page 2</a>
        </li>

      </ul>
    </div>
  </div>
</nav>

1 个答案:

答案 0 :(得分:0)

这里发生了两件事:

  • .navactive上的specificity低于.nav li
  • background:~~~background-image的简写,background-image覆盖background-color

.navactive规则更改为.nav li.navactive应该注意特殊性。

background-image:none;添加到.navactive规则将确保显示background-color

&#13;
&#13;
.nav li {
  /* Old browsers */
  background: linear-gradient(to right, #0943A7 50%, #FFFFFF 50%);
  background-size: 200% 100%;
  background-position: right bottom;
  margin-left: 10px;
  transition: all 1s ease;
}

.nav li:hover{
  background-position: left bottom;
}

.nav li.navactive {
  background-color: #0943A7 !important;
  background-image:none;
  overflow: hidden;
  transform: skewX(-18deg);
  -webkit-transform: skewX(-18deg);
  -ms-transform: skewX(-18deg);
}
&#13;
<nav class="navbar navbar-expand-lg navbar-light bg-faded">
  <div class="container">
   <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="true" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse show" id="navbarResponsive">
      <ul class="nav justify-content-center mx-md-auto">

        <li class="nav-item">
          <a class="nav-link" href="index.php">Home</a>
        </li>

        <li class="page2 nav-item navactive" >
          <a class="nav-link" href="#">Page 2</a>
        </li>

      </ul>
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;

在侧节点上,您可能希望使用font-style:italic而不是skewX。