Tips on displaying a different image upon hovering

I'm looking for assistance with a website I'm currently developing. I need to have a new image appear when hovering over an existing image.

<div id="buttons">
            <div id="hover-info"><a href="#"><img src="images/Informasjon_button.png" width="120px" height="120px" /></a></div>
            <div><a href="#"><img src="images/Mail_button.png" width="120px" height="120px" /></a> </div>
            <div><a href="#"><img src="images/Facebook_button.png" width="120px" height="120px" /></a></div>
            <div><a href="#"><img src="images/TLF_button.png" width="120px" height="120px" /></a></div>
            <div><a href="#"><img src="images/Portfolio_button.png" width="120px" height="120px" /></a></div>
    </div>

So far, this is the HTML code that I have implemented.

    #buttons {
        width:45%;
        height:200px;
        margin-top:100px;
        padding-left:130px;
    }
    #buttons div {
        float:left;
        margin-right:100px;
        height:120px;
    }
    #hover-info {
        height:120px;
        width:120px;
    }
    #hover-info a:hover img {
        background-image:url(../images/Informasjon_button_hover.png) no repeat;
        height:120px;
        width:120px;
    }

Despite my attempts with the CSS styling provided above, I am unable to achieve the desired effect of having the new image display upon hovering over the original image. Any suggestions or guidance on how to resolve this issue would be greatly appreciated! :)

Answer №1

When using an img tag, it contains its own image so if you try to assign a new image with css background-image, it won't appear.

Also, the code

background-image: url(path) no-repeat;
is actually invalid. Instead, you can use a shortcut with background like this:

#hover-info a{
  background:url(../images/Informasjon_button.png) no repeat;
  height:120px;
  width:120px;
}
#hover-info a:hover{
  background:url(../images/Informasjon_button_hover.png) no repeat;
  height:120px;
  width:120px;
}

Make sure to remove the html img tag when implementing this solution.

Answer №2

Here is the code snippet for your reference:

<a href="#"><img src="path-to-default-image" onmouseover="this.src='path-to-image-on-hover'" onmouseout="this.src='path-to-default-image'"></a>

It's important to pay attention to the use of single and double quotes while copying or modifying the src attribute.

I hope this solution works for you! 😊

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

Firefox causing line-height problem

Having an issue with styling a search button in Firefox. The input submit is created using an iconic font, white background, and border-radius as shown below: display: block; width: 60px; height: 60px; line-height: 60px !important; padding: 0; background: ...

The brush functionality does not display rotated x-axis labels

I am experiencing issues with my D3 area chart that includes brush functionality on a rotated x-axis at a 45-degree angle. Initially, the x-axis labels are displayed properly but when I apply the brush to the chart, the labels disappear and do not reappe ...

Ways to transform the appearance of every occurrence of a specific string or symbol?

Is there a method to modify the color of all occurrences of a specific string or symbol, such as * in my case, to red using HTML or JavaScript? ...

What is the proper way to include a URL when submitting an AJAX form?

When I try to call a servlet using HTML, jQuery and Ajax by submitting a form, I keep getting a 404 error saying the resource cannot be found. My project is built with Maven. The Servlet is located in the src/main/java package under : com.mycompany.sample ...

Customizing Dropdownlist generation within a shared template

My ProfileViewModel class contains a list of DJTypeModel and uses this template: @using DigitalDjPool.Website.Domain.Profiles @model DigitalDJPool.Website.UI.Models.ProfileWizard.UserDJTypeModel <div class="row no-margin"> <div class="input- ...

Tips for Keeping a Responsive Image at the Forefront of a Text-Image Layout as You Scroll

I'm currently in the process of creating a website where text appears on the left side with an accompanying image on the right. The challenge I'm encountering is ensuring that as users scroll, the image adjusts dynamically based on the associated ...

Prevent accidental selection when clicking on empty space on a webpage using CSS

Can anyone help me with a frustrating issue I'm facing on my website? Whenever I click multiple times on empty spaces, it ends up selecting entire rows and paragraphs like in this fiddle. The most confusing part is that there's no text under the ...

CSS grid: guarantee that all dynamic rows maintain a uniform size throughout

I'm facing a unique scenario here. I'm attempting to utilize CSS grid to display content side by side. The challenge is that one side of the content needs to have dynamic height while the other side should remain fixed. Here's an example: ...

Passing references using a personalized component

So, I've encountered this issue with my code: import MuiDialog from "@mui/material/Dialog"; import { styled } from "@mui/material/styles"; const TheDialog = styled((DialogProps) => ( <MuiDialog {...DialogProps} /> ))(( ...

Versatile CSS Navigation with Multiple Rows

Looking to implement a content panel switcher with 6 navigation items split between 2 rows, each containing 3 items. I want all columns to be evenly aligned to the left without excessive use of divs and clutter in the HTML code. Any suggestions on achiev ...

Is there a way to update a field request without the need to refresh the page?

I am currently working on a form that includes several fields, with the first one being an ID number input field. This ID is associated with another model for verification purposes. I have created a function called get_employee_name, which retrieves the em ...

Focus on selecting each label within a table using JavaScript

In my current setup, I am attempting to customize radio buttons and checkboxes. Array.from(document.querySelectorAll("tr")).forEach((tr,index)=>{ var mark=document.createElement("span"); Array.from(tr.querySelectorAll("input")).forEach((inp,index ...

Utilizing AngularJS to include information into JSON-LD

As a newcomer to AngularJS, I find myself stuck in one of my projects. My goal is to convert the user-entered data in a form into the format: << "schema:data" >> and then push and display it within the @graph of the json-ld. Here are my HTML an ...

Implement an overflow property at the end of an Angular 2 animation

To create a smooth slide in and out animation, I have implemented the following code: animations: [ trigger('assignState', [ state('maximize', style({ height: '*', })), ...

Ensuring an image fits perfectly within a div that spans 100% width using CSS

I'm brand new to CSS and could use some guidance Here's the issue: I have a 1600 pixel wide image and a div that is 100% wide. Is there a method to adjust the image so it fits within the div, eliminating any horizontal scroll bars and cutting o ...

Experiencing a lack of desired outcome in Outlook when using simple HTML with table elements

When creating a template for Outlook, I encountered an issue where the logo appeared on the right and the link text was on the left, which is the opposite of what I wanted. How can this be resolved? <center> <table width="600px;" style="margin:au ...

Running a shell script in PHP and supplying inputs for any prompts

My Raspberry Pi is running an Apache server with a simple program that allows users to input a string into an HTML form. Upon submitting the form, the entry is sent to a PHP file which then calls a shell script and passes the input as a prompt. I am strugg ...

Retrieving a singular data entry through ajax request

I have encountered an issue with my code. While using ajax to retrieve data, I am only able to get one field at a time. The value I receive is correct, so that's not the problem. It seems like there might be an issue with how my array is constructed. ...

How to Stop Bootstrap's table-hover Class from Altering Cell Background Colors in a Table

I have a Django project where I am working on customizing the background color of priority cells in a table based on their status (high, medium, low). My table is styled using Bootstrap, which includes the table-hover class. However, when I hover over a ro ...

Switch the scroll direction in the middle of the page and then switch it back

Yesterday, while browsing online, I stumbled upon this website and was amazed by the unique scroll direction change from vertical to horizontal mid-page. I'm curious about how they managed to achieve that effect. Does anyone have insight into the pro ...