What is the best way to ensure that an image in the navbar changes when hovered over?

In my quest to enhance my skills, I am currently trying to figure out how to create a hover effect on an image within the navbar. My objective is to have the div with the ID of "pic-index" react to hovering over the "HOME" link in the navbar and change the image displayed. Unfortunately, I seem to be at a loss on how to achieve this task. Below is a snippet of my HTML code for reference:

To provide more context, I have included a snippet of the relevant section from my current code:

     
        
    
    body {
      margin: 0;
      padding: 0;
      background: white;
    }
    
    .nav ul {
      list-style: none;
      background-color: white;
      text-align: center;
      padding: 0;
      margin: 0;
    }
    
    .logo{
    position: absolute;
    float: right;
    margin-left: 1136px;
    margin-top:-3px;
    }
    
    .mainul {
      height: 145px;
    box-shadow: 1px 1px 1px #7e7e7ea6;
    }
    
    .mainul2{
        
      height: 145px;
      box-shadow: 5px 9px 29px -2px #0000005e;
    }
    
    .pic-index{
        
        position:absolute;margin-left:936px;margin-top:62px;
    }
    .nav li {
      font-family: Varela Round;
      font-size: 1.2em;
      line-height: 40px;
      text-align: left;
        padding-right:;
    }
    
    .nav a {
      font-size:15px;
      margin-top:50px;
      margin-left:20px;
      text-decoration: none;
      color: #5a5a5a;
      display: block;
      padding-left: 15px;
    
      transition: .3s background-color;
    }
    
    .nav a:hover {
     color:#57c0ea;
    }
    
    .nav a.active {
      
      color: #444;
      cursor: default;
    }
    
    /* Sub Menus */
    .nav li li {
      font-size: .8em;
    }
    
    /*******************************************
       Style menu for larger screens
    
       Using 650px (130px each * 5 items), but ems
       or other values could be used depending on other factors
    ********************************************/
    
    @media screen and (min-width: 650px) {
      .nav li {
        width: 130px;
        border-bottom: none;
        height: 50px;
        line-height: 50px;
        font-size: 1.4em;
        display: inline-block;
        margin-right: -4px;
      }
    
      .nav a {
        border-bottom: none;
      }
    
      .nav > ul > li {
        text-align: center;
      }
    
      .nav > ul > li > a {
        padding-left: 0;
      }
    
      /* Sub Menus */
      .nav li ul {
        position: absolute;
        display: none;
        width: inherit;
      }
    
      .nav li:hover ul {
        display: block;
      }
    
      .nav li ul li {
        display: block;
      }
    }
    <div class="nav"> <ul class="mainul"> <ul class="mainul2">
        <div class="logo"><img src="images/Logo-1.png"></div>
        <div class="pic-index"><img src="images/nav-home-normal.png"></div> <li class="contact"><a href="#">צור קשר</a> <ul>
    
    </ul> </li> <li class="services"><a href="#">שירותים</a> <ul> <li><a href="#">Tutorial #1@@</a></li> <li><a href="#">Tutorial #2</a></li> <li><a href="#">Tutorial #3</a></li> </ul> </li>
         <li class="about"><a href="#">אודות</a>
    
    </li> <li class="home"><a href="#">דף הבית</a></li>
        </ul> </ul> </div>

   

Answer №1

When the .home element is positioned after the .pic-index element, achieving a desired effect can only be done using JS or jQuery. Below is a solution using jQuery to accomplish this. If the .pic-index element precedes the .home element, CSS alone would have been sufficient, but in this case, that's not applicable.

(I've included an image to demonstrate the effect - run the snippet in FULLSCREEN for better visualization)

EDIT
Additionally, I've made a minor update to the CSS, adding styles to the img class. Also, there are some HTML structure errors in the lists where some ul and li elements are incorrectly placed.

/* HOVER */
$(function() {

  $('.home').mouseenter(function() {
    $('.pic-index img').attr(
    'src', 'http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/sign-check-icon.png'
    );
  });
  
  $('.home').mouseleave(function() {
    $('.pic-index img').attr(
    'src', 'http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-help-about-icon.png'
    );
  });
  
});
body {
      margin: 0;
      padding: 0;
      background: white;
    }
    
    .nav ul {
      list-style: none;
      background-color: white;
      text-align: center;
      padding: 0;
      margin: 0;
    }
    
    .logo{
      position: absolute;
      float: right;
      margin-left: 1136px;
      margin-top:-3px;
    }
    
    .mainul {
      height: 145px;
      box-shadow: 1px 1px 1px #7e7e7ea6;
    }
    
    .mainul2{        
      height: 145px;
      box-shadow: 5px 9px 29px -2px #0000005e;
    }
    
    .pic-index{        
        position:relative;
        top:30%;
        float: right;   
        margin-right: 50px;
    }
    .pic-index img{
        max-width: 48px;
        max-height: 48px;        
    }
    
    .nav li {
      font-family: Varela Round;
      font-size: 1.2em;
      line-height: 40px;
      text-align: left;
        padding-right:;
    }
    
    .nav a {
      font-size:15px;
      margin-top:50px;
      margin-left:20px;
      text-decoration: none;
      color: #5a5a5a;
      display: block;
      padding-left: 15px;
    
      transition: .3s background-color;
    }
    
    .nav a:hover {
     color:#57c0ea;
    }
    
    .nav a.active {
      
      color: #444;
      cursor: default;
    }
    
    /* Sub Menus */
    .nav li li {
      font-size: .8em;
    }
    

    
    /*******************************************
       Style menu for larger screens
    
       Using 650px (130px each * 5 items), but ems
       or other values could be used depending on other factors
    ********************************************/
    
    @media screen and (min-width: 650px) {
      .nav li {
        width: 130px;
        border-bottom: none;
        height: 50px;
        line-height: 50px;
        font-size: 1.4em;
        display: inline-block;
        margin-right: -4px;
      }
    
      .nav a {
        border-bottom: none;
      }
    
      .nav > ul > li {
        text-align: center;
      }
    
      .nav > ul > li > a {
        padding-left: 0;
      }
    
      /* Sub Menus */
      .nav li ul {
        position: absolute;
        display: none;
        width: inherit;
      }
    
      .nav li:hover ul {
        display: block;
      }
    
      .nav li ul li {
        display: block;
      }
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 
   <div class="nav"> <ul class="mainul"> 
   <ul class="mainul2">
        <div class="logo"><img src="images/Logo-1.png"></div>
        <div class="pic-index"><img src="http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-help-about-icon.png"></div> 
        <li class="contact"><a href="#">צור קשר</a>
        <ul>
     </li>
    </ul>  
    <li class="services"><a href="#">שירותים</a>
    <ul> 
      <li><a href="#">Tutorial #1@@</a></li>
      <li><a href="#">Tutorial #2</a></li>
      <li><a href="#">Tutorial #3</a></li> 
    </ul> 
    </li>
         <li class="about"><a href="#">אודות</a></li>
         <li class="home"><a href="#">דף הבית</a></li>
      </ul>       
  </div>

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

Color remains unchanged for selected value in dropdown menu

I'm facing a challenge in changing the color of the selected value in the dropdown menu. Each value has a different color, but when I select one, it reverts back to black. .appt-status select option[value="0"] { color: #3EA47B; } .appt-status o ...

What could be causing the unexpected white gap within my div element even though I've adjusted its dimensions to be minimal?

<div style=" height:0; width: 0 ; border: 1px solid black ; "> This code snippet displays a div element with a specified height, width, and border color. However, there seems to be an issue with the rendering as some st ...

The vertical scrolling feature seems to be malfunctioning within my angular 10 application

I'm having an issue with the scrollbar not working on this Angular 10 template, even after adding the CSS style overflow: auto; to the main container. Although I've included the overflow property in the main container "cont" as shown in the HTML ...

Tips for implementing bslib package pop up window within a table displayed using rhandsontable in R Shiny

I am trying to implement the template from Laurent's answer on R Shiny - Popup window when hovering over icon in my code below, which involves integrating a popup window into a rhandsontable table. My goal is to display the popup window as depicted in ...

Tips for creating a stylish, blurred, and centered box for a login form on a full-size background that is also responsive

I attempted to create a login form on an HTML page using Angular, featuring a full-size background image that is centered. The form itself is contained within a div with a blurred background, also centered both horizontally and vertically within the browse ...

Pressing an HTML button can enable and disable the pause and resume functions

Currently, I am working on an audio player where I need to implement functionality to pause and resume the playback. I have already set up the pause() and resume() functions in my script. The issue I am facing is related to the HTML button that triggers th ...

Employing featherlightGallery.js without the need for anchor tags

I've been experimenting with the FeatherlightGallery plugin for a lightboxing feature on an image slider. The slider contains images directly, without anchor tags wrapping them. However, I'm facing an issue where clicking next/prev doesn't l ...

What is the process for altering the image on a jQuery mobile a href button?

Is it possible to use a custom image instead of a normal rectangular shape for a jquery mobile button? I'm looking for references on how to do this. The code below is an example of what I want. Here is an example of a normal button: <a data-role= ...

Column reverse flex-direction is functioning correctly, however, it is imperative that it does not automatically scroll to the bottom

I have a Flexbox set up where I need the contents to display in reverse order. Everything is working well, but the list has many items and the newest ones are appearing at the top. Currently, the Flexbox automatically scrolls to the bottom, but I want it t ...

Create concise information in a single line with Twig

So, I basically need to display the following information inline on my website: Just like this: Family: Open Sans - Variant: Regular Italic Size: 15px - Line Height: 25px - Paragraph: 15px Color: grey_3 This is the code snippet: {% include "../compone ...

Guide to adjusting the screen resolution on your iPad 3

Hello everyone, I'm fairly new to designing for the iPad 3. Could someone please provide some guidance on how to set media queries specifically for the iPad 3? Thank you in advance. ...

When comparing the Raspberry command line editor 'Nano' to LeafPad for editing a text file string, it leads to issues where PHP cannot interpret the string accurately

Working on a Raspberry Pi, I noticed that when using Nano as the text editor to edit a text file, PHP files struggle with querying variables correctly in SQL. However, if I use the LeafPad editor that comes built-in with the Pi, the PHP file is able to out ...

Aligning items to the left and center using CSS flexbox

Is there a way to align the first element to the left and the second element to the center within a flex container? I've tried using different options like justify-content and align-items with center, but I can't seem to get the second element in ...

I am having trouble with the CSS and Bootstrap not being applied after printing

Once the submit button is clicked on the initial output page, the CSS styling disappears and only a simple default form page is displayed. This does not meet my requirements. Using inline CSS allows it to work, but external CSS does not. Can someone please ...

Creating a dynamic table using jQuery and XML content

Hello everyone, I am new to jQuery and JavaScript in general. My goal is to generate a table based on XML data, but I'm struggling to achieve the correct output. Here's what I have tried so far: Here is my HTML code: <table id="daily_fruit"& ...

What advantages come with using PHP comments instead of HTML comments within HTML code?

Word on the street is that there might be, so I wanted to confirm. CSS comment: <!-- Comment your styles here. --> JavaScript comment: <script> // Add your comments here </script> ...

Faulty toggle functionality within a JavaScript-created accordion panel

HTML I'm looking to add a FAQ question within the div with the class "section-center" <body> <section class="questions"> <div class="title"> <h2>FAQ SECTION</h2> < ...

Centered CSS Box with Pointing Arrow

I'm looking for a way to create a unique div design that includes an arrow pointing downwards attached to the bottom. After some exploration, I was able to achieve this look through this process: http://jsfiddle.net/hyH48/. However, my challenge lies ...

"Guidance on jQuery syntax: Use a textbox to filter out and hide select options with each keystroke

Is there a way to modify my existing code so that it can show or hide elements based on a filter condition, specifically for Internet Explorer? I want to wrap the unmatched elements in a SPAN tag and hide them if the browser is IE, and vice versa by remo ...

Issues arise with form submissions when attempting to implement them through jQuery

When submitting a form, I am using jQuery to set the action dynamically. Below is the HTML code: <form name="frmSearchTops" id="frmSearchTops" method="post" onsubmit="searchitem();"> <select name="cboCityNameRes" id="cboCityNameRes"> ...