Turn off HTML link hover color

If I have a <div>, I can easily control its color using CSS:

<div class="good">Hello</div>
.good { color: green; }

Similarly, I want to style links in the same way:

<a href="...", class="good">Hello</a>
.good { color: green; }

I prefer the link to remain green even during mouse hover, similar to the behavior of the <div>. While I can achieve this by declaring:

.good:hover { color: green; }

It becomes cumbersome as I need to remember to add it for each class applied to the <a> element. Is there an easier way to prevent color change on hover for <a>? I'm considering something like:

a:hover { color: do-not-change; }

or:

a:hover { color: inherit-from-non-hover; }

Update I forgot to mention that I've globally set styles for a and a:hover to override default browser settings:

a, a:hover { color: black; text-decoration: none; }

This is because I'm unsure how to disable hover color changes. Ideally, I would like to achieve the following:

a { color: black; }
a:hover { color: <some way to prevent changing>; }
.good { color: green; }

This setup ensures that regular <a> text remains black regardless of hovering, while <a class="good"> appears green whether hovered over or not.

Answer №1

To achieve the desired effect, you can utilize either the inherit or initial property. Keep in mind that using these will result in the text turning black when hovered over, as it inherits the color property from the parent element of the anchor tag.

a:hover{
    color: inherit; /* Inherits color property from its parent */
}

a:hover{
    color: initial; /* Takes initial value of the property which is black in this case */
}

Answer №2

There are multiple ways you can accomplish this task...

You could create a new CSS class that can be utilized on any element where the hover effect needs to remain the same:

<div class="consistent-color">Hello</div>
<a href="..." class="consistent-color">Hello</a>
.consistent-color:hover{
    color:inherit;
}

Add this class to your existing class good:

<a href="..." class="good consistent-color">Hello</a>
.good:hover, .consistent-color:hover{
    color:inherit;
}

Alternatively, you could apply it to all hyperlink elements:

<a href="...">Hello</a>
a:hover{
    color:inherit;
}

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

Toggle the sliding effect on click using jQuery

I'm currently working on a sidebar menu that involves a lot of javascript. The challenge I am facing is with the submenu items within some of the menu items. When I click on a menu item, its submenu opens using jQuery slideToggle, but when I click on ...

How can you make a drop-down menu in React/Bootstrap appear above all other elements?

My current goal is to implement a Bootstrap dropdown menu that appears when a button is clicked. Within my component structure, the code looks like this: <> <div className='1'> <navbar/> <div> < ...

The Javascript calculation function fails to execute proper calculations

I have been facing immense frustration while working on a project lately. The project involves creating a unique Webpage that can calculate the total cost for users based on their selections of radio buttons and check boxes. Assuming that all other functi ...

How come my counter is still at 0 even though I incremented it within the loop?

Within my HTML file, the code snippet below is present: <div id="userCount" class="number count-to" data-from="0" data-to="" data-speed="1000" data-fresh-interval="20"></div> In my Ja ...

Adjusting the appearance of a heading through CSS upon being clicked

My goal is to create a feature that highlights the border of a selected box in a different color. I attempted to achieve this by using classes in the TypeScript file and utilizing document.getElementById within the selectFlight method, which is called in t ...

I'm struggling to comprehend why this code isn't functioning as expected

There is a need to update the displayed image. The two images are stacked on top of each other, with one having an opacity of 100 (active) and should be positioned above the inactive one based on z-index. The inactive image has an opacity of 0. When one im ...

Populating the DOM with a mix of strings and HTMLDivElements by iterating through an array using *ngFor

I have a specific layout requirement that needs to resemble this example: https://i.sstatic.net/4kP2q.png The desired layout should utilize CSS properties like display: grid; someFunction(data) { this.data = data; ...

Click the Button in the Cypress table/grid

My table is created using DIVs which contain customer information. I am trying to implement a function where I can click on a delete button with the attribute nat=Delete, but the rows have the same name elements. Below is a snippet of my table: <div cla ...

Using Div in Bootstrap to Overlay Text on an Image

Trying to overlay text on an image using the code snippet below; https://i.sstatic.net/7jkEC.png <div class="wrapper"> <img src="https://cdn0.vox- cdn.com/thumbor/qLH7593e3D2SSSIdkwgBWdYhjjk=/289x0:802x684/400x533/filters:forma t(webp)/cdn0.vo ...

What is the best way to resize an image to fit in a div element if the image is larger than the div?

I have an image tag nested within a div element. <div id = "myDiv"> <img alt = "myImage" src = "some_source"/> </div> The image is larger than the div, causing it to extend beyond the boundaries of the div. Initially, I considered usin ...

Alignment of Bootstrap 5 card text and buttons

I've been diving into the BootStrap 5 Crash Course from this YouTube link: https://www.youtube.com/watch?v=4sosXZsdy-s. The final website from the tutorial can be found here: One specific part focuses on using Cards, but I'm facing an issue wher ...

The functionality of my website is currently experiencing difficulties when accessed through the Android UC Browser

My website, , is experiencing issues with product loading and the '+' button in the side menu not working on UC Browser. It works fine on other Android browsers like Chrome and Firefox, but I am confused as to why it is not functioning properly o ...

Maintain the CSS style of a parent element even when hovering over its child elements

I have successfully created a navigation bar with a menu that highlights a blue line whenever you hover over a section. However, I am facing an issue with the submenu within a section. I want the parent section to still display the blue line when hovering ...

What's causing the failure of the greater than function in Smarty?

I have been attempting to add code that will change the CSS style of the Add to Cart button on my online store. When the product quantity is 0, it should display "Order", and when the quantity is greater than 1, it should display "Add to Cart". However, th ...

Tips for honing in on a particular card within a list of cards using React

I am currently working with React 17.0.2 and Material UI, and I have a specific requirement to focus on a particular card from a list of cards by clicking on it. (Please refer to the attached picture for clarification). I hope this explanation helps you un ...

Troubleshooting CSS and Bootstrap display problems and bugs

Hey, I'm having some trouble with the PHP code for a Star Wars webpage that I'm working on for a university project. I'm encountering issues that I can't seem to fix. I've set up templates and have a basic design ready to connect t ...

How can I use HTML5 to create a playlist of videos that play sequentially in a video tag from an array?

Attempting to play multiple videos in HTML5 video tag consecutively has been quite a challenge for me. After browsing through some related queries, I attempted to modify my code based on the solutions provided. However, all I seem to achieve is playing t ...

Navbar content not stretching to edge of screen:

Click here for image of problem I initially used the code from getbootstrap.com, but the search bar and search button aren't aligning properly in the navbar. I've attempted to selectively align the search option without success. Any assistance wo ...

What is the best way to submit a form inside a Bootstrap (4) modal that only contains hidden inputs when pressing the enter key?

I have set up a form within a Bootstrap modal that contains hidden inputs and a concealed submit button. The goal is for the user to click a button, open the modal, and then press the enter key to automatically submit the form. Through my research and tes ...

Is it possible to loop through a directory containing images and dynamically generate a CSS rule that positions each image based on its

I'm not very familiar with PHP so I could use your expertise in coming up with the most efficient and straightforward solution for this specific task. In my directory "path/images/headers," I have multiple header images all cut to the correct size, ea ...