如何更改导航栏下拉单击颜色(Bootstrap使用bootswatch主题)

时间:2018-03-10 00:24:08

标签: html bootstrap-4

我试图找出如何更改导航栏下拉单击颜色的颜色。我正在使用一个主题来修改Bootstrap的颜色(特别是Journal主题)

以下是问题https://i.gyazo.com/09bed7887cf59f7e1e0131fd8edb0ddc.mp4

的视频

这是我的代码

<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand">NAME</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
        <li class="nav-item active">
            <a class="nav-link" href="index.html">Home<span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Project showcase
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                <a class="dropdown-item" href="projects/discordbot.html">DOTA 2 Discord bot</a>
                <a class="dropdown-item" href="projects/connect4.html">Connect 4 using TypeScript</a>
                <a class="dropdown-item" href="projects/dailyweatherapp.html">Dailyweatherapp using Electron</a>
            </div>
        </li>
    </ul>
</div>

我的CSS样式表主题 <link rel="stylesheet" href="https://bootswatch.com/4/journal/bootstrap.min.css">

我查找的所有CSS似乎都没有解决我的问题。

1 个答案:

答案 0 :(得分:0)

因此,您要在此处控制的内容称为dropdown-item有效状态。当您单击并在按住鼠标按钮时保持活动状态时,它将变为活动状态。

通常,所有链接都具有活动状态以及正常状态以及悬停状态(和访问状态)。

以下是Bootstrap 4中dropdown-item的活动状态的css规则集:

.dropdown-item.active, .dropdown-item:active {
    color: #fff;
    background-color: darkviolet;
}

我使用“darkviolet”作为背景颜色,但当然,您可以根据需要自定义它。

以下是完整的工作代码段(点击下面的“运行代码段”并展开到整页):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    
<style>
    .dropdown-item.active, .dropdown-item:active {
        color: #fff;
        background-color: darkviolet;
    }
</style>
   
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand">NAME</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavDropdown">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="index.html">Home<span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Project showcase
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                    <a class="dropdown-item" href="projects/discordbot.html">DOTA 2 Discord bot</a>
                    <a class="dropdown-item" href="projects/connect4.html">Connect 4 using TypeScript</a>
                    <a class="dropdown-item" href="projects/dailyweatherapp.html">Dailyweatherapp using Electron</a>
                </div>
            </li>
        </ul>
    </div>
</nav>