我需要将导航栏链接更改为白色,并在导航栏中将它们分开.我已为此编写代码,但我似乎无法弄清楚为什么/它一直被覆盖的地方.导航栏的背景是一个图像,链接似乎变为白色而没有图像,所以有没有办法解决这个问题并将图像保留在背景中?
谁能帮助我理解我做错了什么?
.navbar { background-image: url("images/navbarbgnew.png"); background-size: cover; margin: -30px 0 15px 0; } .navbar ul { display: flex; justify-content: space-around; } .navbar ul li a { color: #FFF; font-family: "Helvetica," sans-serif; font-weight: 600; font-size: 14px; }
<nav class="navbar navbar-expand-md navbar-light bg-light mb-3"> <button class="navbar-toggler" 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> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="nav navbar-nav"> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> BOOKS </a> <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <a class="dropdown-item" href="#">Branding Sutra</a> </div> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> LEARNING LAB </a> <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <a class="dropdown-item" href="#">Workshops</a> <a class="dropdown-item" href="#">Classes</a> <a class="dropdown-item" href="#">Audio Downloads</a> </div> </li> <li class="nav-item"> <a class="nav-link" href="#">SERVICES</a> </li> </ul> </div> </nav>
解决方法
Can anyone help me understand what I’m doing wrong?
自定义css规则的特殊性必须匹配或超过相应的Bootstrap规则的特异性.
因此,在这种特殊情况下,您需要一个如下所示的规则:
.navbar-light .navbar-nav .nav-link { color: red; }
点击下面的“运行代码段”按钮,然后展开到整页以验证其是否有效:
<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> .navbar-light .navbar-nav .nav-link { color: red; } </style> <nav class="navbar navbar-expand-md navbar-light bg-light mb-3"> <button class="navbar-toggler" 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> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="nav navbar-nav"> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> BOOKS </a> <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <a class="dropdown-item" href="#">Branding Sutra</a> </div> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> LEARNING LAB </a> <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <a class="dropdown-item" href="#">Workshops</a> <a class="dropdown-item" href="#">Classes</a> <a class="dropdown-item" href="#">Audio Downloads</a> </div> </li> <li class="nav-item"> <a class="nav-link" href="#">SERVICES</a> </li> </ul> </div> </nav>
有关CSS特异性的更多信息:
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity