HTML: Hover effect does not work for icons when hovered alongside anchor elements within a list item

I wanted to add a special effect to my navigation list items: When the mouse hovers over the anchor tags of the list items (or even in the active state), I wanted the image in the <span> to be replaced with a colored version of the image. The color of the text and the color of the icon should change together.

The code that I tried using is provided below, but unfortunately, it did not work as expected. Here's an example showcasing the desired result vs. the actual result:
Desired Result: https://i.sstatic.net/NCTkd.jpg
Actual Result: https://i.sstatic.net/yMRQe.jpg

nav ul li a:hover,
.active {
  color: #3B94D9;
}

nav ul li:hover {
  border-bottom: 2px color #3B94D9;
}

span {
  margin-right: 4px;
  margin-top: 10px;
}

span#home {
  width: 20px;
  height: 18px;
  background: url('../img/home.png') no-repeat;
  display: inline-block;
  vertical-align: bottom
}

span#home:hover {
  background: url('../img/home-hover.png') no-repeat;
}

span#notif {
  width: 20px;
  height: 18px;
  background: url('../img/bell.png') no-repeat;
  display: inline-block;
  vertical-align: bottom
}

span#notif:hover {
  background: url('../img/bell-hover.png') no-repeat;
}

span#msg {
  width: 20px;
  height: 18px;
  background: url('../img/messages.png') no-repeat;
  display: inline-block;
  vertical-align: bottom
}

span#msg:hover {
  background: url('../img/messages-hover.png') no-repeat;
}
<nav>
  <ul>
    <li><a href="#" class="active"><span id="home"></span> Home</a></li>
    <li><a href="#"><span id="notif"></span>Notifications</a></li>
    <li><a href="#"><span id="msg"></span>Messages</a></li>
  </ul>
</nav>

Answer №1

You're on the right track, but your CSS is currently set to target the span only when it's directly hovered over. To achieve the desired effect, you should target the span when its containing <a> element is in the hover state.

Instead of:

span#home:hover {
  background: url('../img/home-hover.png') no-repeat;
}

Use this code to target the span when the parent a element is being hovered over:

a:hover span#home {
  background: url('../img/home-hover.png') no-repeat;
}

You can also apply this method to the .active class by using a.active span#home


See the updated snippet below:
(Please note that I've replaced the icons with block colors for demonstration)

nav ul li a:hover,
.active {
  color: #3B94D9;
}

nav ul li:hover {
  border-bottom: 2px solid #3B94D9;
}

span {
  margin-right: 4px;
  margin-top: 10px;
   }

span#home {
  width: 20px;
  height: 18px;
  background: url('../img/home.png') no-repeat;
  display: inline-block;
  vertical-align: bottom;
}

a.active span#home,
a:hover span#home {
  background: url('../img/home-hover.png') no-repeat;
}

span#notif {
  width: 20px;
  height: 18px;
  background: url('../img/bell.png') no-repeat;
  display: inline-block;
  vertical-align: bottom
}

a.active span#notif,
a:hover span#notif {
  background: url('../img/bell-hover.png') no-repeat;
}

span#msg {
  width: 20px;
  height: 18px;
  background: url('../img/messages.png') no-repeat;
  display: inline-block;
  vertical-align: bottom
}

a.active span#msg,
a:hover span#msg {
  background: url('../img/messages-hover.png') no-repeat;
}

/* added for TESTING ONLY as your images are not visible in the snippet */
span#home, span#notif, span#msg{
  background-color:#eee;
  height: 20px;
  width:20px;
}
a.active span#home, a.active span#notif, a.active span#msg,
a:hover span#home, a:hover span#notif, a:hover span#msg{
  background-color:#3B94D9;
}
<nav>
  <ul>
    <li><a href="#" class="active"><span id="home"></span> Home</a></li>
    <li><a href="#"><span id="notif"></span>Notifications</a></li>
    <li><a href="#"><span id="msg"></span>Messages</a></li>
  </ul>
</nav>

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

The content entered into the text area does not appear as it was originally typed

Users have reported an issue with the input area - when they update the text, it does not display as expected. For instance, new lines are not being shown. Here is a snapshot of what the user typed in: https://i.sstatic.net/JK0OQ.png However, after upda ...

How can AngularJS utilize ng-repeat and ng-bind-html to display arrays of HTML strings?

I'm currently attempting to display HTML strings from an array of HTML strings in my AngularJS project. As a newcomer to AngularJS, I have been trying to follow the examples provided on the official AngularJS website, but I am struggling to find a sui ...

Comparing Flexbox Wrapping Behavior in Chrome and Firefox

Hey there! I'm currently grappling with ensuring that this (3-3) flexbox layout appears consistently on both Chrome and Firefox browsers. If you'd like to see a functioning example of what I'm trying to achieve (three columns and two rows) ...

How to successfully incorporate Google fonts into an HTML email for Outlook

Hey everyone – I've come across multiple websites discussing this issue, but I'm still struggling to get it right. I'm trying to incorporate a Google font (specifically, Fira Sans) into an HTML email so that it shows up correctly in Outloo ...

What is the best way to iterate through anchor links surrounding an image and dynamically load content into the figcaption element using AJAX?

Seeking assistance for an issue related to my understanding of the $this keyword in jQuery. I am facing a problem where I have 3 images on a page, each wrapped in an anchor link. My goal is to loop through these links, retrieve the URL of each anchor lin ...

Calculate the sum of a column in a table that is dynamically populated

I'm using jQuery to load a table with data: function getSales(customerId, fromDate, toDate){ $.ajax({ type: 'POST', url: 'ajax/sale_pl.php', data:{customer_id:customerId, from_date:fromD ...

Retain the formatting of HTML while extracting the text

Is there a simple way to extract text from HTML source without losing formatting (such as line breaks and spaces)? Currently, my text extraction process looks like this: page_title_element = driver.find_element_by_xpath("x-path") page_title = pa ...

An effective way to emphasize the full area of a list item when hovering over it

Is there a way to modify the hover behavior of a list item in Fiddle? I'd like the black highlight to cover the entire area of the list item, without leaving any white space on top or bottom. Currently, it only covers the center area of the list item. ...

What is the best way to maintain consistent scaling for elements of varying widths and heights?

I'm searching for a method to evenly scale my elements regardless of their width and height. I don't want them to be proportional to their size. Check out the JSFiddle version body, html { height: 100%; width: 100%; margin: 0; backgro ...

Python Selenium: Having trouble clicking a button without any element identifiers such as id, value, or name associated with the button tag

I'm experiencing trouble with Selenium as it attempts to interact with the "Become A Member" button found on this link: . Here is the HTML code relating to the button: https://i.sstatic.net/Pjeu3.png Despite my efforts in using various methods such ...

Tips on customizing the appearance of the dropdown calendar for the ngx-daterangepicker-material package

Is there a way to customize the top, left, and width styling of the calendar? I'm struggling to find the right approach. I've been working with this date range picker. Despite trying to add classes and styles, I can't seem to update the app ...

The issue of custom breakpoints not functioning properly with Bootstrap 4's navbar-expand-* feature

Here is my customized Bootstrap navbar: <div class="container-fluid bg-secondary"> <nav class="navbar utep-nav navbar-inverse navbar-expand-lg"> <button class="ut-toggler navbar-toggler ml-auto" type="button" data-toggle="collapse ...

Utilize jQuery to populate checkbox and label attributes with elements from an array

I am looking to populate attributes for 4 checkboxes and their labels from specific arrays without appending the entire markup. I have everything set up in the markup and just need to fill in the attributes based on the selection from a select menu. var ...

Having trouble making a Bootstrap 4 input-group with beta 3 prepend/append function properly

When working with Bootstrap 4 Beta 3, I encountered some challenges with the Beta 3, particularly with the input-group-append and input-group-prepend classes. I struggled with placing these classes at the end of my input group. The last button in my inpu ...

I have successfully implemented the show animation for the Bootstrap dropdown menu, but now I'm struggling to find a way

I have successfully implemented a CSS animation dropdown menu as shown below: /* Custom CSS for animation */ .dropdown .dropdown-menu { animation-name: example; animation-duration: 1s; } @keyframes example { from { transform: scale(0); o ...

Save modified content in CKEditor to an HTML document

Currently, I am utilizing the Ajax success function to dynamically load the content from an HTML file into CKeditor. function retrieveFileContent(fileUrl) { $.ajax({ url: fileUrl, success: function (data){ ...

Unable to display the string following a space in the value attribute of an hbs file

<input type="text" class="form-control" id="exampleInputEmail2" name="productName" value={{product.productName}} > When I input 'Smart Phones' into product.produc ...

What is the best way to display SQL data in PHP using div elements?

I am currently working on a project that requires me to display posts from a database. Each post consists of a title and text content. I have successfully retrieved the necessary information from the database using PHP and SQL, however, I am struggling wit ...

Guide on making a dynamic table with HTML and CSS featuring square cells

Looking to design an 8x8 table using HTML and CSS, with the first column and row functioning as headers (ideally with header row height equal to header column width), and all table body cells being square. Shown below is the current HTML and CSS code: < ...

Unable to retrieve the values from document.getElementById("") or document.getElementById("").innerHtml appears to be malfunctioning

I am currently working on an HTA application that includes multiple <input type="text"> fields for users to input data. My goal is to pass this data to a VBScript for processing. Below is the HTML structure: <div id="tableContent" name="tableCon ...