无法覆盖导航栏中的bootstrap .active类

时间:2013-08-20 10:06:02

标签: css ruby-on-rails twitter-bootstrap

虽然我在自己的css文件中覆盖了.active类,但我无法更改活动链接的背景颜色。我也试过一个不同的类(覆盖),但我仍然无法覆盖.active类。这是Ruby代码

%ul.nav.nav-list
%li{:class => "#{'active overwrite' if params[:controller] == 'dashboard'}"}
%a{:href => home_path}
    .container.nav-style
        %p.center
            %i.icon-dashboard.icon-3x
            %br
            %strong DASHBOARD

这是Css

 .overwrite{
         background-color:red;  
    }

2 个答案:

答案 0 :(得分:2)

您应首先覆盖活动课程的背景see here

.navbar .nav > .active > a,
.navbar .nav > .active > a:hover,
.navbar .nav > .active > a:focus {
  color: #555555;
  text-decoration: none;
  background-color: #e5e5e5;
  -webkit-box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125);
     -moz-box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125);
          box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.125);
}

示例仅覆盖类.active

.navbar .nav > .active > a {
    background:none;
}

.navbar .nav > .active > a,
.navbar .nav > .active > a:hover,
.navbar .nav > .active > a:focus {
 background:none;
}

jsfidle

注意:如果您使用的是bootstrap 2.3.x

答案 1 :(得分:0)

请参阅下面的示例 使用整页运行代码段。

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
  <div id="navbar" class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Home</a>
      </li>
      <li><a href="#about">About</a>
      </li>
      <li><a href="#contact">Contact</a>
      </li>
    </ul>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js">
  </script>
  <script>
       //Click on any li in ul with class 'navbar-nav'
      $('ul.navbar-nav li').click(function(e) {
        var $this = $(this); // declare variable for current li that we click
        $('ul.navbar-nav').find('li.active').last().removeClass('active') //find all li that class class active and remove
        $this.addClass('active'); //add 'active' class to current li.
      });
    </script>
</body>

</html>