bootstrap活动类背景颜色

时间:2017-11-17 12:28:21

标签: html css frontend

我有navbar的HTML,我需要更改.active类的背景颜色。

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
      <a class="navbar-brand" id="white" href="#">TableMailerMM</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Projects</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
      <!--<ul class="nav navbar-nav navbar-right">
        <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
      </ul>-->
    </div>
  </div>
</nav>

我试过的是,

.nav.navbar-nav li:active{
   background-color: #ff812c;
}

但它没有用,有什么想法吗?

3 个答案:

答案 0 :(得分:2)

尝试使用

.nav.navbar-nav li.active a{
   background-color: red;
}

&#13;
&#13;
.nav.navbar-nav li.active a {
      background-color: #ff812c;
  }
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" id="white" href="#">TableMailerMM</a>
        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Projects</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div>
    </div>
</nav>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

https://jsfiddle.net/94yqf9sa/4/

:active是关于元素的状态。

.active是关于元素的类。

答案 2 :(得分:0)

你正在使用:active pseudeselector这里不适用使用.active类选择器并选择后代锚点,如:

ul.nav.navbar-nav li.active a {
    //styles
}