Swap the image (either the last or first child) within a div when it is hovered over

I have multiple Div's on my webpage and I am looking to change the color and image when a mouse hovers over them.

Changing the text color from grey to blue is quite simple.

CSS
#div1:hover{color:#0000CC}

If I want to change an image, I can achieve that using the following code:

CSS
a img:last-child {
  display: none;  
}

a:hover {
    Color: var(--Ability-orange);
}

a:hover img:last-child {    
  display: block;  
}

a:hover img:first-child {
    display: none;
}

Both tasks are easy to accomplish..

However, if there is a div with text and two images inside, hovering over the div only changes the text (hovering over the image as it's in the div changes both).

Any suggestions?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Vision</title>
  <style>
    .container {
      padding: 25px;
      overflow-x: hidden;
      height: 90%;
    }
    
    #div1:hover {
      Color: #0066CC;
    }
    
    a img:last-child {
      display: none;
    }
    
    a:hover img:last-child {
      display: block;
    }
    
    a:hover img:first-child {
      display: none;
    }
  </style>

</head>

<body>
  <div class="container">
    <!-- page container-->

    <div id="div1" style="font-size:x-large">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="2%">
            <a><img src="../Images/on.png" width="32" height="32" /> <img src="../Images/off.png" width="32" height="32" /></a>
          </td>
          <td width="98%">Change this text</td>
        </tr>
      </table>
    </div>

  </div>
  <!-- page container-->


</body>

</html>

Answer №1

If you want the images to switch when hovering over the div, simply update the a:hover (etc.) rules to #div1:hover a (etc.)

.container {
    padding:25px;
    overflow-x:hidden;
    height: 90%;    
}

#div1{
    min-height: 90px;
    display: flex;
    align-items: center;
}

#div1 img{
  margin-right: 10px;
}

#div1:hover{
    Color:#0066CC;  
}

a img:last-child {
  display: none;  
}

#div1:hover a img:last-child {    
  display: block;  
}

#div1:hover a img:first-child {
    display: none;
}
<div class="container">  <!-- page container-->
  <div id="div1" style="font-size:x-large">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
              <td width="2%">
                  <a>
                      <img src="http://placekitten.com/190/190" width="80" height="80" /> 
                      <img src="http://placekitten.com/200/200" width="80" height="80" />
                  </a>
              </td>
              <td width="98%">Modify this text</td>
          </tr>
     </table>
    </div>
</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

What is the best way to retain the background image in a fixed position?

Is there a way to fix the size of the picture in my code? The current size is 5834*3886, but when I minimize the browser, part of it disappears. Here is the CSS code I have written: .contact{ position: relative; min-height: 100vh; padding: 50 ...

Elements of <nav> within a <footer> section

I've recently started using HTML5 display elements like header, footer, and nav. While reading about the nav element in particular, I found this interesting information in the HTML5 spec: The nav element is primarily intended for major navigation b ...

How to create a fixed header navigation in SquareSpace

If you take a look at my Squarespace website using the Adirondack theme over here: The issue I'm facing is that whenever I scroll on either desktop or phone, the top header area shrinks. Currently, the navigation remains fixed on the screen and the l ...

What is the best way to make an element "jump to the top" when the parent element has reached its maximum height?

I have a parent div containing 6 child divs. The height of the internal divs changes dynamically based on the data. The outer div has a fixed height set. I want the internal divs to move to a new column inside the parent div when they no longer fit in term ...

I'm curious, which effect or plugin is being used here?

Do you know the name of this particular parallax effect or the plugin used for it? I'm also interested in finding more examples with a similar type of effect, so if you have any tutorials on how to achieve it, that would be greatly appreciated. ...

Adjusting the navbar color on scroll

I am looking to change the color of my navbar upon scrolling. When the navbar is at the top, the background should be transparent. Thank you to everyone for the attention and I apologize for my poor English! /* Navbar Color */ .navbar-default .navbar-t ...

Step-by-step guide to implementing a floating action button on the bottom-right corner of a screen using Material-UI in a React application

I have been attempting to place the FloatingActionButton on the bottom right corner of the screen. To achieve this, I am utilizing the following library: http://www.material-ui.com/#/components/floating-action-button import React, { Component } from "reac ...

How can you show a green check mark next to an input field in AngularJS after inputting valid data?

I am diving into the world of AngularJS and Angular Material with my web application. As a beginner in AngularJS and Angular Material, I need some help. My current task is to display a green checkmark next to an input field only when valid data is entere ...

Incorporating a HTML layout with a JS backdrop

I have successfully implemented a JavaScript background and now I want to apply this background across all my PHP files. The issue is that the HTML code either overlaps with the JS content or appears behind it. How can I resolve this? Below is the JS fil ...

Enhancing Animation Speed with jQuery

I've provided the code snippet at this link: http://jsfiddle.net/LnWRL/4/: Here is the HTML: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <div id="wrap_demo"> <div id="demo"></di ...

The CSS form appears to be too large for the page

On every single one of my pages, the content fits perfectly within the body: However, when I have forms on the page, they extend beyond the footer and the body doesn't resize accordingly: Where could the issue be coming from? ...

The Material UI dialog box popped up against an unexpected gray backdrop

Currently, I am using Material UI in conjunction with React to create a dialog that appears when a button is tapped. This button is located within a table, which is displayed over a Paper component. The problem arises when I utilize the dialog with its def ...

Tips for aligning multiple columns within a Bootstrap 3 carousel

For quite some time, I have been trying to solve this puzzle. My goal is to have the featured image displayed to the left of the carousel, with its associated text to the right. I've created a mockup to illustrate exactly what I mean. I can achieve t ...

Creating a Vertical Stack of Divs Using HTML and CSS

I've attempted various methods to align the links vertically in the left-most column, but without success. Any suggestions or advice would be greatly appreciated! http://jsfiddle.net/adRuz/112/ .box { background: #E8E8E8; border-radius: 8px ...

How to make a DIV element the same width as a TABLE element

Within my container DIV, there are three elements: two banner DIVs (representing a header and footer) and a wide TABLE. Despite the horizontal scrolling required for the large TABLE, I need the banner background colors to extend the full length of the DIV. ...

Automatically select and pre-fill a list item based on its value using jQuery

I'm a beginner in JQuery. In my application I have the following: Here is my HTML code: <ul id="list"> <li> <a class="selected" value="0" href="#" id="sel">ALL</a> </li> <li> <a hre ...

Make a <div> element that has a fixed size, but allows for scrolling text inside

I have tried searching the forum for a solution to my problem, but haven't found one yet. I am trying to create two "divs" with fixed height and internal scrolling. However, whenever I add text inside one of them, its height increases accordingly. Wha ...

I am looking to expand the width of the Bootstrap container specifically for the Max width

I need to adjust the width of the Bootstrap container to be responsive on all devices, especially maximizing the width on larger screens. Can anyone provide guidance on how to achieve this? Thank you in advance. To view my website, please follow this link ...

The parent container is not properly wrapping its content in Internet Explorer only

Check out this code snippet I have here: https://jsfiddle.net/kimwild/mtxgtwr5/2/ main#wrapper { z-index: 1; width: 100%; position: relative; overflow: hidden; background-color: red !important; border: 10px dotted #4D37DB; } figure#about-im ...

"Creating a visually appealing form with the input element

When using ASP.Net, there is a tag known as CheckboxList that generates the following output: <table class="checkbox"> <tbody> <tr> <td> <input id="/*longdynamicstring1*/" type="checkbox" name="/*longdynamicstring2*/ ...