Tips on maintaining the consistent color of an active link until selecting a different link

Is it possible to make a link change color when clicked, and have that color stay until another link is clicked? How can I achieve this using the "active" attribute? I attempted it, but the link reverts back to its original color after clicking.


    <html>
    <head>

    <style>

    a
    {
        display:block;
        height:5%;
        width:10%;
        background-color:#96C;  
        border: solid #96C 1px;
        margin-top:5px;
    }

    a:hover
    {
        background-color:#666;
    }

    a:active
    {
        background-color:#C99;
    }
    </style>


    </head>

    <body>

    <a href="#">Home</a>

    <a href="#">Sign in</a>
    <a href="#">Sign up</a>
    <a href="#">Exit</a>


    </body>
    </html>

Answer №1

The :active pseudo class was not intended for this purpose. You can achieve it using JavaScript instead. If you are familiar with jQuery, you could implement something similar to the following:

var links = $("a");
links.on("click",function(){
  links.removeClass("highlighted");
  $(this).toggleClass("highlighted");
});

Don't forget to include the "highlighted" CSS rule:

a.highlighted {
 background-color: #C99;
}

You can view a working example in this fiddle http://jsfiddle.net/QQb27/

Answer №3

To highlight the currently active link, you can utilize JavaScript (specifically jQuery). Check out an example of this in action by visiting this fiddle.

Here is a simple setup:

<a href="#">Link 1</a>
<a href="#">Link 2</a>

In your CSS file, define the styles for your links:

a { color: green; }
.active { color: orange; }

Now, using jQuery, you can toggle the "active" class on click:

$('a').on('click', function() {
   $('a').removeClass();
    $(this).addClass('active');
});

Answer №4

Enhanced version of dward's response with custom styles: http://jsfiddle.net/zEwUY/1/

a:hover = When you hover over the link.

a:active = Clicking and holding your mouse on the link.

a:visited = Appearance after visiting the link before.

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

Adding a subtle gradient to the image, just ahead of the SVG

Is there a way to achieve this particular look? https://i.stack.imgur.com/yQjvK.png I'm currently encountering this appearance issue. https://i.stack.imgur.com/eQjQ4.png Any suggestions on how I can make it match my desired outcome? Your assistan ...

Utilizing Material UI to access CSS classes in React

Seeking guidance on incorporating CSS within another CSS in React using Material-UI. Although I am familiar with using @extends and @mixin for applying CSS rules, I encountered a challenge when working with React and Material-UI. How can I address this iss ...

Evaluate numerical values using xsl:choose function

Using XSL version 1.0, I am populating an HTML table with percentage values from XML. My goal is to display the highlighted color of the percentages based on a condition, but the condition is not being processed as expected, leading to the exclusion of the ...

Issues with the loading of CSS in Wordpress

I transferred the project from the server to my personal computer (localhost). However, whenever I make changes to the css file, these modifications do not appear on the website when viewed locally. After attempting to switch browsers and clear the browse ...

What is the best way to remove a class from an element upon successful completion?

How can I make an image disappear when the user clicks a button in the associated form? I'm struggling to implement this feature. Any suggestions? <script type="text/javascript"> $(document).ready(function() { $(".removebutton").s ...

Differentiate pointer for checkbox HTML

Just starting out with html and css, and I'm working with a checkbox: <input type="checkbox" name="vehicle" value="Bike">I have a bike<br> My goal is to display a hand icon when hovering over the checkbox. Ho ...

Why is my second CSS animation, which is running late, causing such chaos?

I am currently in the process of animating a simple text. However, I am encountering some issues with making it disappear after a certain period of time: During the initial fade in, the opacity value does not seem to be respected, as the text seems to a ...

The overflow of CSS text selection color spills beyond the boundaries of the box

I'm just starting to learn CSS and was wondering if there is a way to prevent the text selection color from extending into the 'gutter' (I believe that's the correct term) like shown here: It may seem trivial, but I've observed th ...

Collapsing tabs feature in Bootstrap 4

I am working on a project with Bootstrap 4 Tab. My goal is to initially hide all tab contents and then show them when the corresponding tab is clicked. I achieved this by removing the active class from the tab-pane div. However, I encountered an issue wher ...

What iOS Printing and HTML formatter should you use and what level of sophistication is best?

Currently, I have successfully created an iOS 5 application that sends a job to the print queue. The functionality works fine and utilizes the UIMarkupTextPrintFormatter to generate the document in HTML format. At this point, I am using a simple HTML str ...

The marquee's position is reset the first time text is loaded dynamically from an API

In my angular project, I'm implementing a marquee feature to display data fetched from an API. However, I've noticed a strange issue where after a page reload, the marquee starts from right to left but once it reaches the end of the div, it reset ...

Exclude the UL hierarchy from removing a class in jQuery

Check out this fiddle to see the code snippet: http://jsfiddle.net/3mpire/yTzGA/1/ I am using jQuery and I need to figure out how to remove the "active" class from all LIs except for the one that is deepest within the hierarchy. <div class="navpole"&g ...

The padding on the button inside the v-card is not being applied as expected

I am facing an issue with the following code snippet: <v-card height="200"> <v-card-actions class="mb-0"> <v-btn flat color="orange">Share</v-btn> <v-btn flat color="orange">Explore</v-btn> & ...

What could be the reason for the malfunctioning dropdown menu in my navigation bar upon clicking it?

After spending hours practicing creating a navbar using Bootstrap 3.3.7, I've hit a roadblock - the dropdown is not working when clicked on. It's frustrating because I have double-checked all my scripts and ensured that I have the latest version ...

How can I make the navbar in Angular stay fixed in place?

After using the ng generate @angular/material:material-nav --name=home command to create a navbar, I am trying to make it fixed at the top while scrolling. I attempted using position: fixed; on my mat-sidenav-content but it didn't work. Does anyone ha ...

Minimize the entire project by compressing the .css, .js, and .html files

After recently incorporating Grunt into my workflow, I was thrilled with how it streamlined the process of minifying/concatenating .css files and minifying/uglify/concatenating .js files. With Grunt watch and express, I was able to automate compiling and ...

"Unlocking the power of your device's microphone with

Hello, I'm wondering if it's possible to utilize a device's microphone through trigger.io. Perhaps by integrating the native plugin functionality and creating a wrapper for microphone access using a forge native API, or exploring alternative ...

Resizable vector graphics with consistent border dimensions

I need a solution that maintains a constant line width while still making the SVG responsive. Here is the desired outcome: https://codepen.io/dudleystorey/pen/wMLBLK The example linked above achieves this using CSS, but I am looking to achieve the same r ...

Vue.js fails to show Font awesome icon

Currently, I am attempting to incorporate a font-awesome arrow icon using my CSS code as seen below: <style> .negative { color: red; } .positive { color: green; } .negative::after { content: "\f106"; } </style> Despite ...

Managing collapsible content in Bootstrap 4: A comprehensive guide

How can I customize collapsible content in Bootstrap 4? For instance, take a look at this navbar: https://i.sstatic.net/UYbMQ.png https://i.sstatic.net/OuVdw.png https://i.sstatic.net/lXvYt.png I'm looking to modify the behavior of this example. ...