如何在Bootstrap 4中覆盖nav-pills下拉样式?

时间:2018-07-08 15:14:35

标签: html css bootstrap-4

我正在尝试更改菜单链接的颜色,以修改我的CSS。选择菜单nav-pills作为菜单。这是我的代码。

HTML

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </head>
  <body>
    <nav class="navbar navbar-expand-lg">
      <div class="container">
        <ul class="mr-auto nav nav-pills">
          <li class="nav-item dropdown">
            <a class="nav-link" href="#" data-toggle="dropdown">My dropdown</a>
            <div class="dropdown-menu">
              <a class="dropdown-item" href="#">l1</a>
              <a class="dropdown-item" href="#">l2</a>
              <a class="dropdown-item" href="#">l3</a>
            </div>
          </li>
        </ul>
      </div>
    </nav>
  </body>
</html>

CSS:

.nav-pills > li > a {
  background-color: #888;
  color: white;
}

.nav-pills > li > a:hover,
.nav-pills > li > a:focus,
.nav-pills > li > a:active {
  background-color: #123 !important;
  color: #085;
}

直到我单击它,菜单药看起来都很好。悬停时,它也表现良好。但是当我单击它时,文本颜色变为白色。当我将光标放在下拉菜单中的链接上时,药丸会变为标准蓝色,而不是深蓝色。我该如何解决?

我正在使用Bootstrap 4。

1 个答案:

答案 0 :(得分:1)

看看CSS:active选择器。

https://www.w3schools.com/cssref/sel_active.asp

.nav-pills > li > a:hover,
.nav-pills > li > a:focus,
.nav-pills > li > a:active {
  background-color: #123 !important;
  color: #085;
}

在特定情况下,您可以通过添加以下内容来解决问题:

.show > .nav-link {
  color: #085 !important;
}

.nav-pills > li.show > a:focus,
.nav-pills > li.show > a:not(:focus) {
  background-color: #123 !important;
  color: #085;
}

.dropdown-item:active,
.nav-link:active {
  background-color: #123 !important;
}

这是更新后的fiddle