Adjusting the background color of the active class in Bootstrap 4

I am currently attempting to modify the background-color of the active Bootstrap class, but I am facing some difficulties.

To do this, I have created a separate CSS file named custom.css and have tried various approaches such as:

.navbar-dark .nav-item.active .nav-link,
.navbar-dark .nav-item .nav-link:active,
.navbar-dark .nav-item .nav-link:focus,
.navbar-dark .nav-item:hover .nav-link {
    color: #00B159;
}

.navbar .nav-item.active .nav-link,
.navbar .nav-item .nav-link:active,
.navbar .nav-item .nav-link:focus,
.navbar .nav-item:hover .nav-link {
    color: #00B159;
}

.active {
    background: rgba(165, 168, 168, 0.329);
}

Unfortunately, none of these solutions seem to be working at the moment :)

To set the active class using ViewBag (which is successfully displaying the active page in blank), I have used the following script:

<script>
    $(document).ready(function () {

        if (@ViewBag.ActiveMenu != null) {
            $('#' + '@ViewBag.ActiveMenu').addClass('active');
        };
    });
</script>

Below is the complete code for the navigation structure:

<nav class="navbar navbar-expand-sm navbar-dark fixed-top" style="background-color:#5AC5F1">
    <div class="container">
        <a class="navbar-brand" href="/Home/Index">
            <img src="/images/logo_header.png" height="40" width="40">
        </a>
        <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 text-center">
                @if (@HttpContextAccessor.HttpContext.Session.GetString("isAuth") == "true")
                {
                    <li class="nav-link" id="MyProfile">
                        @Html.ActionLink("My Profile", "Index", "Contacts", null, new { @class = "nav-link" })
                    </li>
                    <li class="nav-link" id="EventHistory">
                        @Html.ActionLink("Registration History", "Index", "History", null, new { @class = "nav-link" })
                    </li>
                    <li class="nav-link" id="Registration">
                        @Html.ActionLink("New Registration", "Index", "Registration", null, new { @class = "nav-link" })
                    </li>
                }
            </ul>
            <div class="dropdown-divider"></div>
            <partial name="_LoginPartial" />
        </div>
    </div>
</nav>

Answer №1

To ensure that your custom CSS styles take precedence over Bootstrap's default styles, make sure to import your CSS file after the Bootstrap one. The last CSS file imported will override any conflicting styles.

So if your current setup looks like this:

<link href="~/css/bootstrap1.css" type="text/css" rel="stylesheet" /> 
<link href="~/css/custom.css" type="text/css" rel="stylesheet" />

For a more in-depth explanation on how CSS files prioritize styles, check out this Stack Overflow answer.

Additionally, you may want to take a look at this answer which discusses how caching works in Google Chrome. Disabling cache can help prevent any inconvenience caused by outdated styles.

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

If every single item in an array satisfies a specific condition

I am working with a structure that looks like this: { documentGroup: { Id: 000 Children: [ { Id: 000 Status: 1 }, { Id: 000 Status: 2 ...

Understanding page navigation in PHP

Is there a way to validate the inputs on a given page, display error messages if any, and then move to the next page if the inputs are correct? How can this be achieved? <html> <head></head> <body> <?php // define variables an ...

Tips on keeping a div element in a fixed position until triggered by jQuery

I've managed to create a navigation bar within a header that sticks to the top of the screen once you scroll down past 100px. The functionality works well, but I'm looking to make the navigation bar stay fixed on the right until it snaps, essenti ...

Tips for adjusting the font color of user-provided text within an input box using HTML and CSS

HTML code:- <p><input type="text" placeholder="Enter your username"></p> CSS code:- .main-header input { width: 90%; padding: 1rem; margin: 20px 0px; border: none; border-bottom: 4px solid #5f5fb0; ...

Access remote web page within bootstrap modal

Dealing with a recurring issue, I am trying to create a modal that opens content from a remote page populated by a MySql database. Custom styling is also required for the modal. Progress has been made, but now I'm stuck. Below is the current code: Ou ...

Combining jQuery and CSS for seamless background image and z-index chaining

I am facing an issue where I need to add a background image within a div containing a generated image with the class .result. Despite my attempts, I have not been successful in displaying the background image correctly. The code snippet I used is as follow ...

The width of the absolute div shrinks to such an extent that the text starts wrapping around itself

My goal is to create a button that, when hovered over, displays a div. However, I am encountering challenges with the width and position of this div. The issue is illustrated in the images below and in the accompanying jsfiddle. When I set the positioning ...

What is the best way to design a polygon-shaped responsive div?

I've been attempting to design a unique layout with a pentagon-shaped div, however, I'm encountering some challenges. Despite using SVG for the shape, it does not display correctly in Firefox. Additionally, I need the div to be responsive and sca ...

determine the vertical dimension of the horizontal scrollbar

I ran into an issue where I needed to determine the height of a horizontal scrollbar. This question and answer recommended using the clientHeight property to calculate the difference. Unfortunately, this method no longer works as shown here: https://jsfid ...

Adjusting the height property to zero in the absence of an element

I'm currently working on creating a timeline for changeLogs and populating it with data from an AngularJS controller. The issue I'm facing is that the height of the timeline "Line" is set to 6px. This means that even when there is no data in the ...

Having Troubles with PHP File Upload Form

Executing index.php initiates the user input of metadata and files: <!DOCTYPE html> <html lang="en> <head> <meta charset="utf-8"> <meta name="robots" content="noindex, nofollow"/> <meta http-equiv="X-UA-Compati ...

Setting the second tab as the primary active tab

I am currently working on a script that is well-known, and everything is functioning perfectly. However, I want to change it so that when the page is first opened, it displays the second tab instead of the first one (the first tab being a mail compose tab ...

Use Javascript's JQuery library to apply functionality to a newly inserted and cloned

I am facing an issue while trying to implement JQueryUI's DatePicker on a node. It works perfectly fine for all the existing nodes, but when I use the clone function to add new nodes, it doesn't seem to apply as expected. Here is the function th ...

Embedding a picture within a uniquely shaped container

I have multiple sets of skewed divs and I would like to include a separate image in each of them. However, when I attempt to insert an image, it also becomes skewed and distorted. Is there a way to prevent this from happening? Ideally, I want the images to ...

ASP view displaying overlapping controls

I currently have a form written in ASP that incorporates JQuery for handling two elements: an image upload and a datepicker. The code snippet responsible for the image upload functionality is $(function() { $("#wcpImage").makeAsyncUploader({ uplo ...

Displaying text and concealing image when hovering over a column in an angular2 table

I am currently using a table to showcase a series of items for my data. Each data entry includes an action column with images. I would like to implement a feature where hovering over an image hides the image and reveals text, and vice versa (showing the im ...

Unusual behavior observed when calculating in Angular

Utilizing Angular.js, I dynamically calculate the height and add the value to the CSS style attribute within an ng-repeat loop. The issue arises with this particular line of code: <div style="height: {{60.0 / 100.0 * (100 - (100.0 / 0.27 * (item.wert ...

"Remaining Elements" within a CSS Design

I am faced with a challenge where I have 4 elements nested inside a container element. The height of the container should be set to 100% of the browser window. The inner elements need to stack vertically, with the first two and last elements having a natur ...

Conceal button divider on pager in Jqgrid

Within my grid setup, there are 3 buttons placed in the pager section: 'Refresh', 'Constution', and 'Developed'. These buttons are separated by two vertical navSeparators. Upon grid load, the 'Developed' button is hi ...

How can I refresh the information without appending it to the existing table using JavaScript and jQuery?

I am currently utilizing the pusher API and I am facing an issue where the data gets added to my table every time a new state is called. Instead, I want to update the existing data in the table without creating a new row every time. I only want to add a ne ...