Style of active link fades after reloading the page

In my MVC application, I am facing an issue with the left menu. I want the active link's CSS to remain styled even after a page reload.

Within my CSS file, I have defined the following styles:

.navleft li a:hover {
    background: url('../Content/images/nav.png') no-repeat;
    background-position: -232px 0;
    color: #fff;
    left: -6px;
    padding: 19px 18px 19px 40px;
    margin-top: -10px;
}


.navleft li a:active {
    background: url('../Content/images/nav_active.png') no-repeat;
    background-position: -232px 0;
    color: #fff;
    left: -6px;
    padding: 19px 18px 19px 40px;
    margin-top: -10px;

}

In my view file, I am dynamically generating the menu using the following code:

<ul class="navleft">
@foreach (var item in Model.CLeftMenu.ToList())
{
    foreach (var content in Model.LeftSiteContent.ToList())
    {
        if (item.LeftMenuID == content.LeftMenuID)
        {
            <li><a href="@Url.Action("Details", "LeftContents", new { id = @content.LeftContentID })">@item.LeftMenuTitle</a></li>
        }
    }
}
</ul>

The hover effect works fine on the menu, but the active state is not being applied correctly. When inspecting the page, it shows:

<a href="/LeftContents/Details/4" class="active">Services</a>

It seems that the "active" class is automatically added. To make it work as intended, I had to update my CSS to:

.active {
    background: url('../Content/images/nav_active.png') no-repeat;
    background-position: -232px 0;
    color: #fff;
    left: -6px;
    padding: 19px 18px 19px 40px;
    margin-top: -10px;
}

However, this change affected all active links across the site. Is there a way to apply this style only to the left menu without causing conflicts? Do I need to declare "current" as well for proper functionality?

I came across a solution for a similar problem in this Stack Overflow post, but it did not work in my case.

Thank you for any suggestions or assistance.

Answer №1

The :active CSS pseudo-class is designed to style an element, like a button, when it is being actively clicked by the user. This interaction usually begins when the primary mouse button is pressed and ends when it is released. While commonly used on buttons and links, the :active pseudo-class can also be applied to other elements. Check out more information here

It's worth noting that no specific :active class needs to be explicitly defined; instead, you can use .active to target the active state.

.navleft li a:active should be renamed to .navleft li a.active {

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

Narrow down your search results with date range filtering in Angular Material

Seeking guidance as a newcomer to Angular Material, I am trying to implement a date range filter for a table of N results. The options in the filter select are (1 day, 5 days, 1 week, 15 days), which are populated using a variable JS vm.rangos=[ {id ...

Issue Encountered While Attempting to Show a Div Element within a Function

Check out this HTML code snippet: <div class="div1" id ="div1" onclick="onStepClicked()" style ="text-align:center">Step 1</div> And here is the corresponding Script: function onStepClicked() { var elem = document.getElementById(&apo ...

Attempting to interpret HTML in Javascript

I have a text string that contains HTML tags. Initially, I attempted to insert this using innerHTML, but the tags were displayed as plain text. After some troubleshooting, I realized that I needed to properly parse the HTML content. Although jQuery prov ...

Guide on showcasing specific columns from a table by utilizing an HTML listbox in conjunction with SQL coding

Hello, I need assistance with a specific task. I am looking to create a table that has two columns: one for the Country and one for the Capital. This table should list every country in the world along with its corresponding capital. Additionally, I would l ...

Tips for utilizing promises to create automated waiting for a function's completion in node.js/javascript

When I instantiate a module, it triggers numerous asynchronous functions. var freader = new filesreader(); // <-- this triggers multiple async functions var IMG_ARRAY = freader.get_IMG_ARRAY(); // <-- i retrieve the array where content is store ...

Get rid of emojis and unicode characters

My website and database are both configured to utf-8 and utf8mb4 encoding. While textareas accept utf-8 symbols and emojis without issue, I want to restrict certain input fields (such as name and address) to only allow basic text and numbers. This include ...

Simple steps to add a click event listener to every element within a div

I need to assign a click handler to multiple elements and perform different actions based on which one is clicked. To illustrate, I can create an alert displaying the class of the button that was clicked. The elements I am working with have a similar str ...

Load the values into the dropdown list based on the selection from the previous dropdown menu

Currently, I am working on implementing cloning functionality for select boxes. There are 3 select boxes: country, state, city. The user selects the country first which then populates the state select box based on the ID, and similarly, the city dropdown ...

How can one effectively style nested elements within a React component?

When styling multiple div elements using CSS, there are different ways to achieve the same result. In traditional CSS, you can use a nested structure like this: .wrapper{ width: 300px; height: 500px; .container { width: 50%; height: 20%; ...

Can someone please provide assistance with a JavaScript game?

Is there a way to prevent the game from allowing unlimited money to be generated by simply pressing a button? var workbard = document.getElementById("workbar"); function work() { var zaman = setInterval(function () { workbard.value +=1; if ( ...

When location.href is used, the page freezes up

When attempting to transition to another page by clicking on a button, I encountered an issue where the intended page would hang. The transitions to other pages worked fine, but not the specific one I needed: location.href = http://localhost:3306/mountainB ...

Issue encountered where Moment locale functionality is working in local development environment, but not functioning properly in the

My application built with Next.js requires displaying the date in Bengali instead of English. The issue arises after running the build command 'build:next build' where the production version displays the date in English (e.g. '20 January, Su ...

Error encountered with Vuetify stepper simple component wrapper and v-on="$listeners" attribute

I'm working on developing a custom wrapper for the Vuetify Stepper component with the intention of applying some personalized CSS styles to it. My aim is to transmit all the $attrs, $listeners, and $slots. I do not wish to alter any functionality or ...

Executing a program on a website that records every single keystroke made while the webpage is active

Just to clarify, I am not developing a keylogger. I am working on a website for a video game where users can earn items by completing specific in-game actions, like typing something in the chat. Given the nature of this game, I believe the most effective ...

CSS Positioned Float in Reality

I'm in need of a specific feature that IE supports, which can be clearly seen in action at However, the issue arises as no other web browsers seem to support the particular attribute required for this functionality. How can I achieve a similar effec ...

Find the mean of three numbers stored in an array of objects

I am currently working on a school assignment that requires me to develop a function for calculating the average mark of 3 students using an existing object in an array. Below, you can see the array and function that I have been working on as part of this ...

Tips for changing the Ion Select value in Ionic 6

Is there a way to clear the selected value in an ion-select element when navigating back from another page? Below is my .html file code for the ion-select: <ion-select #mySelect style="display: none" (ionChange)="onOrderSelection($event) ...

Transform audio file into a base64 encoding

I am currently developing a Phonegap application for a friend that will allow users to record audio on their phone, save it to the browser's local storage, and then upload it at a later time. As far as I know, local storage does not support storing b ...

Dynamically assign controllers using ng-controller

My goal is to utilize the bootstrap.ui tabset feature with an array of tab settings. These settings include the name of the controller responsible for handling its contents in order to keep my 'tab-control' template clean and simple. <div tab ...

Guide to showcasing a JSON object beginning at a designated position?

Having a json object as an example var obj = [{name: "ana", gender: "woman"}, {name: "ben", gender: "man"}, {name: "andrew", gender: "man"}, {name: "jake", gender: "man"}] Looking to display the list from the 2nd index to the last index of th ...