skip to Main Content

I am trying to overwrite color values in the navigation bar and just can’t get it to work. I must be doing something wrong. I defined the classes active and inactive to change colors in the navigation.

index.html

<!doctype html>
<html lang="en">
  <head>
    <title>Hello, world!</title>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="author" content="WDieter Reuther">
  <meta name="description" content="Testing CSS">


<!-- Favicon -->
<link rel="shortcut icon" href="assets/images/favicon.png">


  <!-- Bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"     rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

  <!-- Customization -->
  <link rel="stylesheet" type="text/css" href="assets/css/custom.css">


  </head>

  <body>

<header class="navbar-light navbar-sticky header-static">

<nav class="navbar navbar-expand-lg bg-body-tertiary">
      <div class="container-fluid" style="background-color: #27a800;">

    <a class="navbar-brand" href="#">Navbar</a>

    <div class="collapse navbar-collapse" id="navbarNavDropdown">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="/">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link inactive" href="/page1.html">Page 1</a>
        </li>
        <li class="nav-item">
          <a class="nav-link inactive" href="/page2.html">Page 2</a>
        </li>
      </ul>
    </div>
  </div>
</nav>
</header>

<h1>Hello, world!</h1>
  </body>
</html>`

custom.css

@charset "UTF-8";
/*!
Custome Styling
*/

 .inactive {color: #ffffff !important; }
 .active {color: #333333 !important; }

I was expecting that my custom css colors would overwrite the bootstrap colors. They don’t.

I also would be happy if I could change the bootstrap colors, i.e. –bs-navbar-active-color via css.

2

Answers


  1. just change order of the style import in the hierarchy (custom.css should be applied first and thus will have higher priority)

      <!-- Customization -->
      <link rel="stylesheet" type="text/css" href="assets/css/custom.css">
    
      <!-- Bootstrap -->
      <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"     rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    
    Login or Signup to reply.
  2. This CSS code lets you customize the colors of links in a Bootstrap navbar. Just set the --bs-nav-link-color to change the default link color, and --bs-navbar-active-color to change the color of active links.

    .nav-link {
        --bs-nav-link-color: yellow;
    }
    
    .navbar-nav .nav-link.active, .navbar-nav .nav-link.show {
        --bs-navbar-active-color: red;
    }
    
    .nav-link {
        --bs-nav-link-color: yellow;
    }
    
    .navbar-nav .nav-link.active, .navbar-nav .nav-link.show {
        --bs-navbar-active-color: red;
    }
    <!doctype html>
    <html lang="en">
       <head>
          <title>Hello, world!</title>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
          <meta name="author" content="WDieter Reuther">
          <meta name="description" content="Testing CSS">
          <!-- Favicon -->
          <link rel="shortcut icon" href="assets/images/favicon.png">
          <!-- Bootstrap -->
          <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"     rel="stylesheet" crossorigin="anonymous">
       </head>
       <body>
          <header class="navbar-light navbar-sticky header-static">
             <nav class="navbar navbar-expand-lg bg-body-tertiary">
                <div class="container-fluid" style="background-color: #27a800;">
                   <a class="navbar-brand" href="#">Navbar</a>
                   <div class="collapse navbar-collapse show" id="navbarNavDropdown">
                      <ul class="navbar-nav">
                         <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="/page1.html">Page 1</a>
                         </li>
                         <li class="nav-item">
                            <a class="nav-link" href="/page2.html">Page 2</a>
                         </li>
                      </ul>
                   </div>
                </div>
             </nav>
          </header>
          <h1>Hello, world!</h1>
       </body>
    </html>
    `
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search