Changing the color of Font Awesome icons when hovered is proving to be a challenge

I am facing an issue with a CSS code snippet that changes the cursor when hovering, but does not change the color as expected. Can someone help me understand why the cursor changes but not the color?

.fa-star-o:hover {
    cursor: pointer;
    color: red;
}

Below is an example of some HTML code that resembles something like this

<div <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d6469304d4062696861235d7f626b64616844606c6a687e23486168606863794c79">[email protected]</a>(i).BandyProfileImageId class="item profile-image-item">
     <img style="position: relative; left: 0; top: 0;" src="data:image/jpg;base64, @(Html.Raw(Convert.ToBase64String(Model.ProfileImages.ElementAt(i).ImageThumbnailCropped)))" alt="no profile images">
     <i style="z-index: 200; position: absolute; top: 5px; right: 5px; color: whitesmoke;" class="fa fa-trash-o fa-2x trashImage"></i>
     <i style="z-index: 200; position: absolute; top: 5px; right: 35px; color: yellow;" class="fa fa-star-o fa-2x"></i>
</div>

Answer №1

It appears that the issue lies in the inline styling used to set the color of the icon. These inline styles will not easily be overridden by external stylesheets, unless the !important rule is applied. A solution could be to define the style in a stylesheet like so:

.fa-star-o:hover {
    cursor: pointer;
    color: red !important;
}

To see this in action, check out this fiddle.

It is generally recommended to transfer all inline styling to an external stylesheet and avoid using !important whenever possible.

Update: It's worth mentioning that relying on !important too heavily can lead to difficulties in debugging, as it disrupts the expected cascading behavior of CSS. If someone needs to override the hover color down the line, they would also need to use !important, creating a cycle of dependency.

Answer №2

I would like to see the remaining HTML code for reference. However, I did manage to create a snippet of HTML myself using your CSS and it worked perfectly. Here it is:

CSS:

.fa-star-o:hover {
    cursor: pointer;
    color: blue;
}

HTML:

<i class="fa fa-star-o">Hello</i>

You can also view the jsfiddle link here: http://jsfiddle.net/rt51hzeg/8/

Answer №3

As many have pointed out, the reason for this behavior is due to the use of inline styles. When an element has inline styling, it takes precedence over any classes or ids attached to it unless the !important declaration is used. Additionally, since :hover is a pseudoclass, it also takes precedence over other hover states defined in CSS.

To illustrate this, here's a simple example:

.fa-star-o:hover {
    cursor: pointer;
    color: red;
}

.stdColor{
  color: gray;
}
<a class="fa-star-o" style="color: gray" href="www.google.com"> Inline style </a>
<br>
<a class="fa-star-o stdColor"  href="www.google.com"> Not using inline style </a>

Answer №4

It is not recommended to include the "color" attribute directly in HTML. Removing the inline style "color: yellow;" from your HTML code will ensure smooth functioning of your webpage. Utilize a separate CSS file to define all your styles, as this approach helps in avoiding compatibility issues.

Below is an example of how you can structure your CSS:

<style>
    .fa-star-o{color:yellow;}
    .fa-star-o:hover {
        cursor: pointer;
        color: red;
    }
</style>

Here is the modified HTML:

<i class="fa fa-star-o fa-2x">hi</i> 

(i).BandyProfileImageId class="item profile-image-item">

<img style="position: relative; left: 0; top: 0;" src="data:image/jpg;base64, @(Html.Raw(Convert.ToBase64String(Model.ProfileImages.ElementAt(i).ImageThumbnailCropped)))" alt="no profile images">

<i style="z-index: 200; position: absolute; top: 5px; right: 5px;" class="fa fa-trash-o fa-2x trashImage">hi</i>

<i style="z-index: 200; position: absolute; top: 5px; right: 35px; " class="fa fa-star-o fa-2x">hi</i>

Answer №5

Reposition the placement of this CSS file's link (containing the provided CSS) to the end of all other CSS links on the page.

Following this step should yield the desired outcome.

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

Make changes to external CSS using HTML and JavaScript

Is it possible to dynamically change the value of a background in an external CSS file using JavaScript? Currently, all my pages are connected to a CSS file that controls the background for each page. I am attempting to modify the background by clicking a ...

Companimation Woes: CSS3 Animations Not Triggering Unless Loaded

I am currently utilizing Mike Fowler's companimation library for Compass, but unfortunately, I am encountering an issue where my animations are not functioning properly in Firefox (even though they work perfectly in Chrome). The problem arises when el ...

Tips for preventing CSS conflicts when incorporating an Angular 6 application into another application

We recently developed an Angular 6 web application and now we want to seamlessly integrate it into one of our client's online store. However, we are facing some CSS conflict issues: For instance: - The webshop is built on Bootstrap 3 while our app u ...

Tips for Customizing the Active Class in Bootstrap 3

After spending the last 3 days searching online and trying various examples, I still can't figure out how to style the active class properly. Below is the HTML code snippet: <div class="navbar navbar-inverse navbar-fixed-top" id="mainHeader" ...

Header appearing at the same level as post title due to overlapping

After rearranging the date header below my post title on Blogger, I encountered an issue where on the latest post, the date was overlapping with the header. Adjusting the margin or padding affected other date headers, leaving me unsure of what to do next. ...

The horizontal scrollbar in Chrome is unexpectedly activated, despite elements being set to occupy the full screen width (100vw) using Tailwind and NextJS 13.4+

It took some time to identify the root cause of this issue within a larger project. Hopefully, this Question and Answer will be beneficial to someone else. Here is the scenario -- in a NextJS/Tailwind project, there is only one large vertical element on t ...

Tips to avoid the page from scrolling to the top when the menu is opened

Whenever a user taps the menu button on the page, a full-page menu opens. However, there is an issue - the main content page automatically scrolls to the top. Can you provide suggestions on how to prevent this from happening? I have already looked into a s ...

"Implementing master page and CSS styles on a child page: A step-by-step

Currently, I have a master page that is successfully being applied to all of my pages. However, it seems to be encountering an issue when trying to locate the CSS file address for pages within child folders. The folder structure looks like this: <scri ...

The mobile site is extending beyond the width of the device screen

After creating this single web page and testing it on my mobile device, I noticed an issue. When I swipe to the left, the website's width shifts slightly, as if the edges are not fully contained within the screen. This problem only occurs on Safari, n ...

Navigate only within the tbody section of the Material UI Table by scrolling

Utilizing the material-ui Table component to build a table with a sticky header presents a challenge. The default behavior is for the scroll to encompass both the thead and tbody of the table. However, I specifically require the scroll functionality to be ...

The HTML Comment Box is unable to expand its height automatically

I found a code snippet online to create a comment box in HTML. It's working fine, but as more comments are added, they start overflowing and don't stay within the designated box. I've tried using a div with overflow settings, but the issue p ...

Adaptable picture within a bootstrap container

I need help figuring out how to insert an image into a Bootstrap panel (see attached image): https://i.sstatic.net/BHQI9.jpg Here is the HTML Code I am using: <div class="panel panel-default col-md-10 col-md-offset-1"> <div class="panel-bod ...

Avoiding the occurrence of extra space due to the p tag for paragraphs

I'm currently using the following <p> tag but it's creating a gap between the tick mark and the text that follows after the closing </p> <p class="tick">✓</p> Post messages to friends all over the world. CSS: .tick { ...

Activate Auto Navigation Feature on Mouseover using jQuery

I have a series of divs that cycle automatically to display their contents one after the other every few seconds. The same content is also displayed when the corresponding link is hovered over. The functionality is working correctly, but I would like to ...

DOMPDF rearranges the order of HTML elements

I am currently using Laravel and working with an HTML view that includes integrated CSS. My goal is to convert this view into a PDF format. When I open the view directly (whether through my documents or a link in my application), everything displays correc ...

What is the best way to display text outside of the current div container?

When I include the "Y" in the code snippet below, $title1 and $title2 are positioned outside of the div and centered against the width of the page. However, without the "Y", the text shifts up and aligns with the address class instead. It's puzzling w ...

Please only choose the immediate children of the body element

<body> <div> <div class='container'></div> </div> <div class='container'></div> </body> Is there a way to use jQuery to specifically target the second container div dire ...

Changing the Color of Navbar Links in Bootstrap 4

Good evening everyone, Recently, I've been experimenting with Bootstrap 4 to create a simple Navbar. I encountered an issue where I attempted to adjust the font-size and color (or hover color) in the Navbar. Strangely, the font-size adjustments seem ...

The floating label effect will be removed when the required attribute is removed as well

Is there a method to achieve a floating label effect in Bootstrap 4 even after removing the "required" attribute from the input field? $(document).ready(function() { $('.form-group input').on('focus', function() { $(this).sibli ...

Changing color when mouse hovers using Jquery

Currently, I am in the process of converting a flash ad into an html5 ad. I found this demo here, and I would like to replicate the mouse hover effect showcased. When the cursor hovers over the details text in the banner, the entire background changes to ...