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

Dealing with h2 text overflowing within a bordered container

I need help creating an h2 element with a double border. Here is my current HTML+CSS snippet: h2.titulo { width: 100%; margin: 10px 0; padding: 10px 0; text-transform: uppercase; font-weight: 400; font-size: 30px; display: block; color: ...

Tips for choosing a sibling element on hover using CSS

I'm having trouble figuring out how to make the rect element change color to purple when the text is highlighted. Right now, only the text color is changing. Is there a way to achieve this using just CSS? The goal is for the rect to turn purple on ho ...

What steps can be taken to create a curved bottom for the header section?

While I've seen this question asked before, I'm not satisfied with how the design has been implemented. Here is my desired look for the curved header I've experimented with border radius left and right, but the edges are excessively rounde ...

The div element's width does not match the width of the textbox

I attempted to implement the following code in HTML. However, I am puzzled as to why the text box and div width are not the same size. Here is what I have experimented with. <style> .test { border:1px solid red;width:100px;height:30px;padding:3p ...

What is the best way to use jQuery to apply CSS only to elements with a specific attribute set?

Currently, I am attempting to utilize jQuery to apply specific CSS styles to elements, but only those that possess a particular attribute. The rationale behind not resorting solely to CSS is due to the immense amount of content on the site, making it impr ...

Discovering Characters in String using jQuery and Implementing a Personalized Class

Update: To achieve the desired typography, I will need to implement some creative jquery / css techniques. The first step is to create a class called .underlined which will allow me to have complete control over the underline by setting a background imag ...

Can someone help me troubleshoot why my multi-step form is not functioning properly?

I've been working on a multi-phase form, but it's not behaving as expected. I've tried different code variations, but for some reason, it's not functioning properly. After spending two days on it, I'm starting to feel a bit frustra ...

Animating CSS styles in Vue.js based on JavaScript triggers

Within my Vue.js application, I've implemented a login form that I'd like to shake whenever a login attempt fails. Though I have achieved the desired shaking effect, I'm not completely satisfied with my current solution. Here's a simpl ...

Trigger .gif on hover using ng-repeat in AngularJS

Many solutions to this problem involve using jQuery, such as the following examples: Stop a gif animation onload, on mouseover start the activation and Animating a gif on hover. However, I'm interested in achieving the same functionality using Angular ...

Javascript and CSS combo for creating a stylish fade effect

Hey everyone, I have a design challenge where I need to change the color of a picture inside a box when the user hovers over it. I have four boxes like this and my goal is to make everything else fade out when a user hovers over a specific box. Anyone kn ...

Is there a way to assign a specific color to individual users in a chat?

I have successfully implemented a chat feature using Pusher, but now I want to customize the appearance by changing the color of the username for the logged-in user when they are active in the conversation. Currently, both my username and another user&apos ...

Positioning elements at the bottom of the container

There is a dilemma that only those in the world of web development will understand. Beware! I have envisioned a layout for a website I am constructing. The concept involves tilted <hr/> elements on the page, with text wrapping around them (refer to ...

What is the best way to pass a value to a modal and access it within the modal's component in Angular 8?

How can I trigger the quickViewModal to open and send an ID to be read in the modal component? Seeking assistance from anyone who can help. Below is the HTML code where the modal is being called: <div class="icon swipe-to-top" data-toggle="modal" da ...

Unable to access or click on Wordpress links or site titles

On my website, www.website.com, I have a main Wordpress installation and a test site at www.website.com/test. The main site functions correctly, but the links on the test site are behaving strangely. I have set the post title as the permalink structure (w ...

Unleash the power of attribute selectors in a SASS/SCSS list variable: a comprehensive guide!

This is a sample code snippet: $list: ( input[type="text"], input[type="name"], input[type="email"], textarea, select[multiple] ) !default; @each $value in $list { ...

How can I ensure that the content within a div does not overflow and that horizontal scrolling is enabled? What is the correct way

After designing my profile page, I encountered a problem related to horizontal scrolling caused by overflow. Despite trying various solutions like adjusting the width of .profilePg, I haven't been able to resolve this issue without disrupting the over ...

Maximizing Efficiency on the Frontend: Balancing Requests with Caching

I am currently tackling a large website that has accumulated quite a bit of technical debt that needs to be addressed. The site contains a significant amount of JavaScript and CSS files being loaded. Currently, these files are aggregated and minified in la ...

Utilizing JQuery to capture user input in a textarea and showcase it in a span element with key press

Is there a way to capture the user's input and display it in a span above the textarea? Specifically, how can I detect when the user presses the enter/return key (keyCode 13) and correctly insert a line break ( ) in the span? $('#InviteMessage ...

What is the procedure for utilizing Javascript to redirect a user on a website back to the login page if necessary?

Is there a way to redirect a website user back to the login page if they try to access a secure page without logging in first? I'm looking to implement this using JavaScript and cookies. Any suggestions or ideas on how to achieve this seamlessly for t ...

Achieving proper layout in Material-UI Autocomplete by splitting long words

Exploring the Material-UI autocomplete feature for the first time has brought some challenges my way. I am working with usernames, which have a specific length limit but could exceed the space available in the autocomplete view. Upon examining the sandbox ...