如何更改已打开的bootstrap下拉选项的背景颜色。这是代码:
<nav class="navbar navbar-toggleable-sm navbar-inverse bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbar6">
<span class="navbar-toggler-icon"></span>
</button>
<a href="#" class="navbar-brand">Title </a>
<div class="navbar-collapse collapse justify-content-stretch" id="navbar6">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">About<span class="sr-only">Home</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Services
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Design</a>
<a class="dropdown-item" href="#">Development</a>
<a class="dropdown-item" href="#">Consulting</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
在bootstrap 3中,我借助以下方式对其进行了更改:
.navbar-default .navbar-nav > .open > a,
.navbar-default .navbar-nav > .open > a:focus,
.navbar-default .navbar-nav > .open > a:hover {
background: #080808;
}
这是bootstrap 3的标准代码(在bootstrap4中不起作用)
我怎么才在bootstrap 4中做同样的事情?
答案 0 :(得分:1)
.navbar .dropdown-menu {
background-color: #BADA55;
}
/* and this styles the dropdwon trigger link, when open */
.navbar .dropdown.show a {
background-color: #BADA55;
}
......就够了。
注意.navbar
将其限制为菜单,如果没有它,只要您在.dropdown
之后加载CSS
,它就会设置所有bootstrap(.min).css
的样式。
.navbar .dropdown-menu {
background-color: #BADA55;
}
.navbar .dropdown.show a {
background-color: #BADA55;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<nav class="navbar navbar-toggleable-sm navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Title</a>
<div class="collapse navbar-collapse justify-content-stretch" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Services
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Design</a>
<a class="dropdown-item" href="#">Development</a>
<a class="dropdown-item" href="#">Consulting</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
请注意,您当前的选择器都不匹配任何内容,因为您似乎没有将navbar-default
类应用于任何元素。
作为一般规则,期望 许多 类名称已在v4中重构,从而允许更短,更具表现力的语法和更高的整体灵活性。
将v3解决方案应用于v4示例通常被认为是不好的做法,就像期望v2解决方案适用于v3示例一样。