0

The color of my navigation bar text is not changing. I am using the default navigation bar, I guess. I am very new to Bootstrap and I have searched the internet but nothing seems to work.

/*Navigation bar*/

.menu-bar {
  background-color: #101010;
}

* {
  font-family: 'Poppins', sans-serif;
}

.nav-link {
  font-size: 24px;
  color: #FFFFFF;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<link rel="stylesheet" href="../css/style.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap" rel="stylesheet">

<div class="header">
  <div class="menu-bar">
    <nav class="navbar navbar-expand-lg navbar-light bg-transparent">
      <div class="container-fluid">
        <a class="navbar-brand" href="#"><img src="../assets/Icon.svg" width="32" height="auto"> </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                         <span class="navbar-toggler-icon"></span>
                       </button>
        <div class="collapse navbar-collapse justify-content-end" id="navbarNav">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item">
              <a class="nav-link active" aria-current="page" href="#">Home</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="#" tabindex="-1" aria-disabled="true">Disabled</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
  </div>
</div>

<main>

</main>

<footer>

</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>

I know writing important in front of color(.nav-link) will make my code work, but I read in an article that it is not a good practice to do so. I tried to watch tutorials on the web, but it is not working for me(Though it is working for them :).

isherwood
  • 58,414
  • 16
  • 114
  • 157
Dujana Abrar
  • 111
  • 1
  • 8

2 Answers2

2

In your CSS code, try changing your selector .nav-link to .navbar-light .navbar-nav .nav-link, as follows:

.navbar-light .navbar-nav .nav-link{
    font-size: 24px;
    color: #FFFFFF;
}

In fact, for applying style to this element, Bootstrap use the selector .navbar-light .navbar-nav .nav-link which is more specific than yours. In CSS, the most specific selector will be applied.

More about CSS Specificity : https://www.w3schools.com/css/css_specificity.asp

<!DOCTYPE html>
<html lang="en">


<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stiks</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
    /*Navigation bar*/
.menu-bar{
    background-color: #101010;
}

*{
    font-family: 'Poppins', sans-serif;
}

.navbar-light .navbar-nav .nav-link{
    font-size: 24px;
    color: #FFFFFF;
}
    </style>
</head>


<body>
            <div class="header">
              <div class="menu-bar">
               <nav class="navbar navbar-expand-lg navbar-light bg-transparent">
                 <div class="container-fluid">
                    <a class="navbar-brand" href="#"><img src="../assets/Icon.svg" width="32" height="auto"> </a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                     <span class="navbar-toggler-icon"></span>
                   </button>
                    <div class="collapse navbar-collapse justify-content-end" id="navbarNav">
                      <ul class="navbar-nav ml-auto">
                        <li class="nav-item">
                         <a class="nav-link active" aria-current="page" href="#">Home</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="#" tabindex="-1" aria-disabled="true">Disabled</a>
                        </li>
                     </ul>
                   </div>
                  </div>
                </nav>
              </div>
            </div>

    <main>

    </main>

    <footer>

    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>

</body>
</html>
lysenn
  • 48
  • 6
  • You might copy the snippet from the question post and apply your solution to it. I'm not sure it works. This is the right approach, though. – isherwood May 28 '21 at 13:57
1

try to give an id to header and then change css accordingly like this and this is how you can change the preference of appying css styles

<div class="header" id="myId"> 
#myId .menu-bar{
    background-color: #101010;
}

#myId .nav-link{
    font-size: 24px;
    color: #FFFFFF;
}