Adjust the color of both the image and text when hovering over either one

I need help creating a hover effect for an image and text pair. My goal is to have the image change when hovered over, along with changing the color of the text next to it.

Below is the code I am currently working with:

<ul id="recherche">
    <li><a href="#"><img id="glo" src="images/glosaire_off.png" alt=""/></a></li>
    <li><a href="#">Glossaire</a></li>
</ul>

This is what I have so far for the image hover effect:

#glo:hover{
    background-image:url(../images/glosaire_on.png) ;
}

Is there a way to apply this hover effect to both the image and the text at the same time? Any suggestions or help would be greatly appreciated. Thanks!

Answer №1

When using the src attribute of the img tag and the background-image style, the image set by the src attribute will overlap the one set by the background-image style. To address this issue, make sure to follow these steps: View Demo

HTML:

<ul id="recherche">
            <li><a href="#"><img id="glo" src="#" alt=""/></a></li>
            <li><a href="#">Glossaire</a></li>
</ul>

CSS:

ul:hover #glo{
    background-image:url(../images/glosaire_on.jpg) ;
}
ul:hover a{ color: teal;}

ul #glo{
    width: 300px;
    height: 200px;
    background-image:url(../images/glosaire_off.jpg) ;
}
ul a{ color: green;}

Remember, specify the width and height for the img element.


UPDATE:

To apply hover effects to pairs of 2 li elements together, wrap them within separate ul elements like this:

<ul id="recherche">
    <li id="glo"><a href="#"></br></a></li>
    <li><a href="#">Glossaire</a></li>
</ul>
<ul>
    <li id="rech"><a href="#"></br></a></li>
    <li><a href="#">Recherche</a></li>
</ul>

Updated Code Demonstration

Make sure to adjust the CSS to target the correct elements. CSS:

ul {
    width: 300px;
}
ul:hover li:first-child {
    background-image:url(http://mailboxtolucalake.com/wp-content/themes/thesis/rotator/sample-2.jpg);
}
ul:hover li:first-child + li a {
    color: teal;
}
ul li:first-child {
    width: 300px;
    height: 200px;
    background-image:url(http://mailboxtolucalake.com/wp-content/themes/thesis/rotator/sample-1.jpg);
}
ul li:first-child + li a {
    color: green;
}

The use of first-child selector and sibling selector (+) in CSS helps in targeting the appropriate elements accurately.

Answer №2

There are two ways to accomplish this!

I recommend using a class="" selector.

Caution: You have set the attribute src:, but you are attempting to change the background-image:

For example,

<ul id="recherche">
            <li><a href="#" class="glow"></a></li>
            <li><a href="#" class="glow" style="background-image:none">Glossaire</a></li>
</ul>

You should use the inline method in order to avoid applying the background image to the second anchor tag.

Now, for the CSS:

.glow{
background-image:url('/*some url*/'); /*the default one*/
}


.glow:hover{
background-image:url('') /*New url inside*/
color: /*some color here, this helps with the text color*/;
}

Answer №3

You can attempt the following recommendation:

#search:hover #global{
    background-image:url(../images/dictionary_on.png) ;
}

Answer №4

Below is the provided answer:

 li:hover {
        background-image:url(../images/glosaire_on.png) ;
    }

Answer №5

.search-results li a:hover{ background:url(../images/glossary_on.png)}

Answer №6

At the beginning, you have included glosaire_off.png in your markup and are attempting to add a background image on hover using CSS. If you want to change the image source to glosaire_on.png on hover and alter the text color, JavaScript will be necessary for this task.

UPDATE

In case jQuery is available

<ul id="recherche">
    <li><a href="#"><img id="glo" src="images/glosaire_off.png"/>Some text</a></li>
    <li><a href="#">Glossary</a></li>
</ul>

$(document).ready(function () {
    $("#glo").mouseenter(function(){
       $(this).attr("src","images/glosaire_off.png");
       $(this).parent().css("color", "green"); 
    }).mouseleave(function(){
       $(this).attr("src","images/glosaire_on.png");
       $(this).parent().css("color", "red");        
    });
});

JSFIDDLE

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

Animating SVGs in Internet Explorer (IE)

I'm currently working on an animation project using SVG images and JS, but unfortunately, I've run into a roadblock with SVG animations not functioning correctly in Internet Explorer. Do you happen to know of any solutions that could make it work ...

What is the best way to incorporate responsive centered text into my HTML element using Bootstrap 4?

Is there a way to add a responsive centered text element to my <a> element without being affected by the overlay color? Also, is there an alternative method to using an extra overlay div in Bootstrap 4? Check out this link for more details. HTML & ...

combine multiple select options values in a single function using jQuery

My HTML code includes two select options for users to choose the origin and destination cities. I need to calculate the cost of travel between these cities. How can I compare the selected options using jQuery? </head> <body> <div> ...

Alter the loaded image based on the switch's current status

I am working with AngularJS material and I want to dynamically load an image based on the status of a switch. Here is the relevant HTML code: If the switch status sw_status.sw1 is true, then load the image truePic.jpg. If it is false, load the image false ...

Displaying a loading screen while a jQuery ajax request is in progress

Currently, I am attempting to display a loading div while waiting for an ajax call to finish. Despite experimenting with various methods, I have not been able to consistently achieve the desired outcome. In my present code, everything functions properly o ...

Issue with using float-right in Bootstrap v4. [Alternative to pull-right]

I am currently using the most recent version of Bootstrap, which is v4.0.0-beta.3. I am having trouble getting elements to align properly according to the official documentation. Despite searching online for solutions, I have not been able to find any that ...

Incorporating JavaScript and CSS files into a content page of a master page in ASP.NET: Steps to follow

I am facing an issue with adding javascript files and css to a content page of a master page in asp.net. I attempted to include a datetime picker on my ContentPage, but it only works on the masterpage. When I try to add the same code to my contentpage, i ...

Using Angular 2 to position a md-fab button with 'position: fixed' inside an inner component

Utilizing the md-fab-button from the initial angular2 material-framework has presented a challenge for me. I am attempting to set the md-fab-button(for adding a new entity) with position: fixed, but my goal is to position the button within an inner compone ...

Create an HTML table using data from a Python dictionary

Can anyone help me convert the dictionary below into an HTML table? {'Retry': ['30', '12', '12'], 'Station MAC': ['aabbccddeea', 'ffgghhiijj', 'kkllmmnnoo'], 'Download&ap ...

Would you like to learn how to dynamically alter a button's color upon clicking it and revert it back to its original color upon clicking it again?

My Upvote button starts off transparent, but when I click on it, the background color changes to green. What I need is for the button to become transparent again when clicked a second time. I've attempted using the following code snippet: function ...

At times, PHP mail usage may result in certain $_POST arrays being unexpectedly empty

I've encountered an issue while using the php mail function to send data from a html form. The mail function is functioning correctly and I receive the email upon form submission, but occasionally I receive empty arrays in the email content. Here is ...

Securing form submissions in Express JS: Best practices for protecting your data

In order to enhance security, the loginform.html located in the public folder sends the username and password input by the user to a server route '/login' for authentication. Server-Side Code: var express = require('express'); var app ...

Instructions for adding a Key every time a user clicks the space Key within an Input field

I built a Movie Search Page with the OMDB API. My current issue is that the API returns an error when searching for a movie with more than one word because the API URL requires a + key between each word. I am looking for a way to automatically add the + ke ...

I am attempting to adjust the CSS code so that it does not shrink when clicked on. How can I prevent this from happening?

Here is the element I am attempting to modify. I attempted &:active { flex:7;} but upon releasing the left click button, it reverted back to its original size. I was aiming for something similar to this <h1>Gallery</h1> < ...

The system encountered an issue with the CSS code, indicating "Invalid CSS after "...inear-gradient(": expected selector, was "{"

While attempting to compile my sass code, I encountered the following error message regarding a syntax issue: /* { "status": 1, "file": "C:/Users/faido/Desktop/Productivity/sass css/project/sass/main.sass", "line": 36, "column": 9, "mes ...

Styling elements with CSS when the cursor hovers over them

I have a unique design challenge where I have stacked elements - an image, text, and an icon. My goal is to make only the icon animate when any of these elements are hovered over. <style> .hvr-forward { display: inline-block; vertical-align: mi ...

I am struggling to find a recurring element within a BeautifulSoup object

The challenge that is currently plaguing me is absolutely driving me up the wall. I am attempting to extract some text from the Pro Football Reference website. The specific data I require can be found within a td element labeled as qb hurries situated in ...

Concealing a hyperlink based on the login status of a particular customer using PHP

I'm relatively new to coding and I'm encountering some difficulty in finding a solution to what seems like a simple problem. My issue revolves around hiding a link if a specific client is logged in. Let me provide some context: On our website, c ...

Leverage jQuery to retrieve information and fill in an HTML form

Seeking assistance with retrieving data based on a dropdown selection change. Specifically, choosing an Item ID from the dropdown and then populating textboxes and other dropdowns with values from the database. The other dropdowns already have assignment o ...

The function of JQuery .click() is successful when used on a local machine, however it is not functioning

I am facing a puzzling issue. The code in question functions perfectly on my local server, but once I uploaded it to my hostgator server, a specific function no longer executes. When I set a breakpoint in the Firefox debugger, I noticed that the function i ...