我在Bootstrap中设计了navbar
。当nav-item
的班级设为active
时,我试图将其设为不同的颜色。
但是我无法这样做。当我设置background
背景颜色正在改变时,但当我使用color
时,颜色不会改变。请注意,我正在使用Bootstrap v4。
$(document).ready(function() {
"use strict";
$('ul.navbar-nav > li').click(function(e) {
e.preventDefault();
$('ul.navbar-nav > li').removeClass('active');
$(this).addClass('active');
});
});
.navbar-toggler:focus {
outline: 0;
}
.navbar-nav > .active {
color: aqua!important; **// THIS DOES NOT WORK**
background-color: chartreuse; **//THIS WORKS**
}
.nav-item > a:hover {
color: aqua!important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="navbar navbar-light animated fadeInLeft" style="background-color: #C0C0C0 ;">
<button class="navbar-toggler navbar-toggler-left" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="#navbarNav" aria-expanded="false" aria-label="Toggle navigation"><span class="lines"></span><span class="lines"></span><span class="lines"></span>
</button>
<h1 class="navbar-brand mb-0 move-header ">NavBrand</h1>
<div class="collapse animated fadeInLeft" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</div>
</nav>
答案 0 :(得分:2)
问题是因为.nav-item > a:hover
比.navbar-nav > .active
更具体。要解决此问题,请移除!important
运算符(因为您应该不惜一切代价避免它)并使您想要覆盖的规则更具体。
.navbar-nav > .active > a {
color: aqua;
background-color: chartreuse;
}
.nav-item > a:hover {
color: aqua;
}
$(document).ready(function() {
"use strict";
$('ul.navbar-nav > li').click(function(e) {
e.preventDefault();
$('ul.navbar-nav > li').removeClass('active');
$(this).addClass('active');
});
});
.navbar-nav > .active > a {
color: aqua;
background-color: chartreuse;
}
.nav-item > a:hover {
color: aqua;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
答案 1 :(得分:1)
尝试在a
标记上添加颜色CSS:
.navbar-nav > .active a{
color : aqua;
}
.navbar-toggler:focus {
outline: 0;
}
.navbar-nav > .active {
color : aqua!important; **// THIS DOES NOT WORK**
background-color: chartreuse; **//THIS WORKS**
}
.navbar-nav > .active a{
color : aqua
}
.nav-item > a:hover {
color : aqua!important;
}
ul li{
list-style:none;
margin: 10px
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-light animated fadeInLeft" style="background-color: #C0C0C0 ;">
<button class="navbar-toggler navbar-toggler-left" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="#navbarNav" aria-expanded="false" aria-label="Toggle navigation"><span class="lines"></span><span class="lines"></span><span class="lines"></span></button>
<h1 class="navbar-brand mb-0 move-header ">NavBrand</h1>
<div class="collapse1 animated fadeInLeft" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li>
<li class="nav-item"> <a class="nav-link" href="#">Features</a> </li>
<li class="nav-item"> <a class="nav-link" href="#">Pricing</a> </li>
<li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li>
</ul>
</div>
</nav>
&#13;
答案 2 :(得分:1)
.active a{
color: red !important;
}
这应该适用于覆盖自定义bootstrap 4主题。