更改导航栏的默认背景颜色和悬停背景颜色

时间:2017-07-28 21:03:12

标签: css twitter-bootstrap navbar

我有以下引导程序导航栏:JSBin



 <!DOCTYPE html>
    <html>
    <head>
      <script src="https://code.jquery.com/jquery.min.js"></script>
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
    </head>
    <body>
      <nav class="navbar navbar-default navbar-static-top" ng-controller="NavCtrl">
        <ul class="nav navbar-nav">
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdow <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="https://www.google.fr">New</a></li>
              <li><a href="https://www.google.fr">Old</a></li>
            </ul>
          </li>
          <li><a href="https://www.google.fr">Help</a></li>
        </ul>
      </nav>
    </body>
    </html>
&#13;
&#13;
&#13;

我想做以下事情:

  1. 将导航栏的默认背景颜色更改为#e1e1e1
  2. 当我们将鼠标悬停在dropdownhelp上时,该元素的背景颜色会变得更暗(例如,#a9a9a9
  3. 有谁知道如何实现这个目标?

3 个答案:

答案 0 :(得分:2)

使用这些规则对其进行定位,以覆盖默认的引导样式。

.navbar-default {
    background-color: #e1e1e1;
}
.navbar-default .navbar-nav>li>a:focus, 
.navbar-default .navbar-nav>li>a:hover,
.navbar-default .navbar-nav .open .dropdown-menu>li>a:focus, 
.navbar-default .navbar-nav .open .dropdown-menu>li>a:hover  {
  background: #a9a9a9;
}

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  <style>
  .navbar-default {
    background-color: #e1e1e1;
}
nav.navbar-default .navbar-nav>li>a:focus, 
nav.navbar-default .navbar-nav .open .dropdown-menu>li>a:focus, 
nav.navbar-default .navbar-nav>li>a:hover,
nav.navbar-default .navbar-nav .open .dropdown-menu>li>a:hover {
  background: #a9a9a9;
}
</style>
</head>
<body>
  <nav class="navbar navbar-default navbar-static-top" ng-controller="NavCtrl">
    <ul class="nav navbar-nav">
      <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdow <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="https://www.google.fr">New</a></li>
          <li><a href="https://www.google.fr">Old</a></li>
        </ul>
      </li>
      <li><a href="https://www.google.fr">Help</a></li>
    </ul>
  </nav>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您只需选择要更改的元素

.navbar {
  background-color: #e1e1e1;
}

nav li:hover {
  background-color: #a9a9a9;
}

答案 2 :(得分:1)

这是你需要的吗?

&#13;
&#13;
.nav li {
background: #e1e1e1;
}
.nav li:hover {
background: #a1a1a1;
}
&#13;
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<body>
  <nav class="navbar navbar-default navbar-static-top" ng-controller="NavCtrl">
    <ul class="nav navbar-nav">
      <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdow <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="https://www.google.fr">New</a></li>
          <li><a href="https://www.google.fr">Old</a></li>
        </ul>
      </li>
      <li><a href="https://www.google.fr">Help</a></li>
    </ul>
  </nav>
&#13;
&#13;
&#13;