无法在bootsrap4中更改导航栏颜色

时间:2019-03-04 14:10:20

标签: html css bootstrap-4 navbar

我使用引导程序创建了两个导航栏。

<!doctype html>
   <html lang="en">
      <head>
            <!-- Required meta tags -->
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

            <!-- Bootstrap CSS -->
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
                <link rel="style" href="css/main.css" />
        		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        		<script src="js/bootstrap.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
            <title>Klevin's</title>
     </head>


          <body>


            <!-- ISLANDWIDE DELIVERY message -->
            <nav class="navbar navbar-expand-sm bg-dark navbar-dark justify-content-center" id="topmessage">
              <ul class="navbar-nav">
                <li class="nav-item">
                  <a class="nav-link" href="#">ISLANDWIDE DELIVERY</a>
                </li>
              </ul>
            </nav>

            <!-- Actual navigation bar -->
            <!-- A grey horizontal navbar that becomes vertical on small screens -->
            <nav class="navbar navbar-expand-sm bg-light" id="mainnavbar">

              <!-- Brand -->
              <a class="navbar-brand" href="/ecommercewebsite/index.php">Klevin's</a>

              <!-- Links -->
              <ul class="navbar-nav">
                <li class="nav-item">
                  <a class="nav-link" href="#">Home</a>
                </li>

                <!-- Dropdown -->
                <li class="nav-item dropdown">
                  <a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
                    Shop
                  </a>
                  <div class="dropdown-menu">
                    <a class="dropdown-item" href="#">Shoes</a>
                    <a class="dropdown-item" href="#">Handbags</a>
                  </div>
                </li>

                <li class="nav-item">
                  <a class="nav-link" href="#">Offers</a>
                </li>

                <li class="nav-item">
                  <a class="nav-link" href="#">Contact</a>
                </li>


              </ul>
            </nav>

            <!-- Optional JavaScript -->
            <!-- jQuery first, then Popper.js, then Bootstrap JS -->
            <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>
          </body>
        </html>

然后我想更改第二个导航栏的背景颜色。所以我把下面的代码放在主css文件中

   body{
     padding-top: 50px;
   }

   #mainnavbar{
      background-color: #CC0000;
   }

但是它不会改变颜色。我正在根据本教程视频进行此操作。 https://www.youtube.com/watch?v=C1phgYAzIfg&t=1066s 在他的视频中,它可以正常工作。但这对我不起作用。我正在使用更新版本的bootstrap。所以不确定是否是原因。

2 个答案:

答案 0 :(得分:2)

删除bg-light。由于它使用!important修饰符,因此它会覆盖CSS。您还需要使用navbar-darknavbar-light来赋予Navbar content (links) a light or dark color

   <nav class="navbar navbar-expand-sm navbar-dark" id="mainnavbar"></nav>

   #mainnavbar{
      background-color: #CC0000;
   }

演示:https://www.codeply.com/go/gFgVhhsoRP

答案 1 :(得分:0)

bg-light替换为navbar-light

<nav class="navbar navbar-expand-sm navbar-light" id="mainnavbar">