Bootstrap - Unable to modify class attributes

I've been having trouble changing the color values in my navigation bar using CSS. I created classes for "active" and "inactive" to modify the colors, but it's not working as expected.

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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8eece1e1fafdfafceffecebba0bda0bd">[email protected]</a>/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; }

Despite creating custom CSS styles, I'm unable to override the default bootstrap colors in my navigation menu. Additionally, I would like to know if it's possible to change the bootstrap colors directly via CSS, such as --bs-navbar-active-color.

Answer №1

Do you want to change the colors of links in your Bootstrap navbar using CSS? You can easily customize the default link color by setting --bs-nav-link-color, and adjust the color of active links by setting --bs-navbar-active-color.

.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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d0f0202191e191f0c1d2d58435e435e">[email protected]</a>/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>
`

Answer №2

Modify the order of the style import in the hierarchy (ensure that custom.css is applied first to have higher priority)

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

  <!-- Bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e98b86869d9a9d9b8899a9dcc7dac7da">[email protected]</a>/dist/css/bootstrap.min.css"     rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

Edit: The instructions given previously are incorrect. To achieve desired results, use a more specific CSS selector:

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

.navbar-nav .nav-item .nav-link.active {
  color: #333333;
}

.navbar-nav .nav-item .nav-link.inactive {
  color: #ffffff;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to dynamically resize SVG images based on the size of the window for better scalability

I have a container that contains two SVG images. I want both the container and the images to adjust their height based on a percentage of the viewport size, while staying within a specified min-height/max-height range (and maintaining the aspect ratio of t ...

Failure in the application of CSS styles to HTML

I have set up a very simple thing, but it just won't work for some reason. The HTML document doesn't seem to be picking up on the CSS. When I click on the href attribute on line 6 in View Source, it shows me the file, so the browser can see it. I ...

What steps can I take to resolve the issue of my popup closing automatically upon opening and simultaneously refreshing my webpage?

After creating a popup in my HTML page, when I click on the element that triggers it, the popup appears for a brief moment before automatically closing and causing my page to refresh. Below is the code I am using: HTML Code : <a class="lv-footer" id= ...

Establish a timeout period for ajax requests using jQuery

$.ajax({ url: "test.html", error: function(){ //do something }, success: function(){ //do something } }); At times, the success function performs well, but sometimes it does not. How can I add a timeout to this ajax re ...

An image inside this stylishly framed photo with rounded corners

.a { clip-path: polygon(10% 0, 100% 0%, 100% 100%, 10% 100%,10% 60%, 0% 50%, 10% 40%); } .a { position: relative; width: 500px; height: 500px; background:url("https://cdn.pixabay.com/photo/2020/02/16/07/55/thailand-4852830_960_720.jpg"); border-radi ...

Struggling with CSS to display images

Struggling to align my images within the designated red lines in the CSS sections I created. Can't seem to get it right. Here's my HTML: <!-- Your code goes here --> <div class="container"> <section class="left"> <h1& ...

Is there a way to adjust the speed of the transitions between animations?

I've been experimenting with ways to increase the time between animations in my cycle. Adjusting the duration of the keyframes animation hasn't yielded the desired effect. span { animation: rotateWordsFirst 15s linear infinite 0s; ...

Left-align elements within a div container that is centered

I want to left-align my elements within a div container that is centered. I used the text-align property to center the overall div, but now I'm having trouble aligning another content container's text to the left within the center-aligned div. H ...

Having trouble finding a hyperlink with CSS selectors

I am attempting to utilize Selenium to retrieve some data from Google, but the CssSelector I have implemented is consistently returning "No element found". Below is my code snippet: //Open google page IWebDriver driver = new FirefoxDriver(); WebDriverWai ...

How can I ensure the end of the list is clearly visible?

I'm experiencing an issue where the last element in my list is getting cut off. When I check the console logs, I can see that it's rendering properly. If I remove the 'position: fixed' style, I can see the element at the bottom of the l ...

Implementing React inline styles by directly incorporating brackets into the HTML rendering

I'm working on a project in React using create-react-app and trying to apply an inline style. Based on my research, the following line of code should work fine: <div id="root" style={{ height: 'auto' }}></div> However, when I r ...

The navigation bar expands in height as the media width changes

I have a rather straightforward navbar: <nav class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header pull-left"> @Html.ActionLink("Contact ...

Is there a way to conceal the scrollbar in the mobile view (on a mobile device or emulator) in a React application without disrupting the scrolling functionality?

I am looking to conceal the scrollbar on mobile devices throughout my app while still allowing users to scroll. I have attempted to hide the scrollbar using CSS properties like scrollbar, scrollbar-thumb, scrollbar-thumb:hover, and scrollbar-track. This ...

The drop-down does not move above the subsequent div when focused

I have implemented a dropdown feature on focus of an <input type='text'> box in the following manner http://jsfiddle.net/Newtt/7ffdF/ The issue I am facing is with the positioning of the dropdown box. It currently displaces the content of ...

Encountering issue with Django locating static file (1.5) showing ERROR 404 code 1649

I'm facing some difficulties in setting up my local Django site. I've reviewed the previous questions and answers, but haven't been able to find a solution. [20/Oct/2013 03:56:33] "GET /rides/ HTTP/1.1" 200 230 [20/Oct/2013 03:56:33] "GET / ...

Ways to stop the click event from being triggered in JQuery

There is a single toggle switch that, when clicked, switches ON and a popup with close and OK buttons appears. However, if the toggle is clicked again, it switches OFF and disappears. The specific requirement is that with every click where the toggle switc ...

Tips for toggling the visibility of an element when a button is clicked in React

In my todo list, I need to display the details of each item when clicked on a button. There are two buttons available: "View Details" and "Hide Details". Below is the code snippet: class Todos extends React.Component{ construc ...

Troubleshooting Bootstrap: Identifying errors in my bootstrap code

Could you help me diagnose the issue with this code? I have tailored it to be responsive on my tablet by using 12 columns for two divs, expecting the third div to be in a new row. However, upon checking my index.html on the tablet, all the divs are display ...

Easiest method to globally disable form autocompletion in an application without manually setting autocomplete off for every individual form

Is there a quick way to disable autocomplete in all forms within the application without individually adding "autocomplete=off" to each form? ...

CSS: Dynamically adjusting height based on parent element's current height

I'm seeking clarification on the topic at hand. Why am I unable to simply use the following code: <div style="min-height: 100vh"> <div style="height: 100%"> </div> </div> When I implement this code, ...