Ways to update the image in a hover container

I am working on a project with HTML code and I am trying to create a hover effect for an image. I want the image to change when hovering anywhere on the box, not just on the image itself. I have tried adding multiple classes and ids, but the image only changes when hovered directly.

Below is the code snippet:

          .expandable-box {
           width: 20%;
           height: 151px;
           margin: 30px 12px 30px 0;
           padding: 30px;
           border: 2px solid #fff;
           border-radius: 5px;
           box-sizing: border-box;
           display: inline-block;
           position: relative;
           overflow: hidden;
           cursor: pointer;
          -webkit-transition: all .3s ease-in-out;
          -moz-transition: all .3s ease-in-out;
          -o-transition: all .3s ease-in-out;
          -ms-transition: all .3s ease-in-out;
           transition: all .3s ease-in-out;
           }

          .expandable-box .icon-cont {
           margin: 20px auto;
           display: block; 
           text-align: center;
           position: absolute;
           top: 10px;
           left: 0;
           right: 0;
         }

         .expandable-box a{
             display:block;
             color:#fff;
             font-weight:600;
            font-size:15px;
             text-align: center;
           position: absolute;
           padding-top:35%;
           width: 100%;
           height:100%;
           left:0;
           top:0;
           margin:0 auto;
          z-index: 4;
         }

         .expandable-box a:hover{
             color:#333;

         }
         .expandable-box:hover{
            background:#fff;
         }
<div class="expandable-box">
     <span class="icon-cont">     
            <img style="display: block; margin-left: auto; margin-right: 
            auto;width: 64px;height:64px;" src="img.png" /> 
           </span>
           <a title="Title" href="page.html.html">This</a>
            </div>
            

I am trying to make the image change when the entire box is hovered over, not just when the image itself is hovered.

Thank you

Answer №1

1. utilizing background-image

Utilize background-image to span instead of using the img tag

then on :hover modify the background url to the desired image's src

      .expandable-box {
       width: 20%;
       height: 151px;
       margin: 30px 12px 30px 0;
       padding: 30px;
       border: 2px solid #fff;
       border-radius: 5px;
       box-sizing: border-box;
       display: inline-block;
       position: relative;
       overflow: hidden;
       cursor: pointer;
      -webkit-transition: all .3s ease-in-out;
      -moz-transition: all .3s ease-in-out;
      -o-transition: all .3s ease-in-out;
      -ms-transition: all .3s ease-in-out;
       transition: all .3s ease-in-out;
       }

      .expandable-box .icon-cont {
       margin: 20px auto;
       display: block; 
       text-align: center;
       position: absolute;
       top: 10px;
       left: 0;
       right: 0;
       width: 64px;height:64px;
           background-size: 100% 100%;
    background-repeat: no-repeat;
       background-image: url(https://i.sstatic.net/mSXoO.png);
     }

     .expandable-box a{
         display:block;
         color:#fff;
         font-weight:600;
        font-size:15px;
         text-align: center;
       position: absolute;
       padding-top:35%;
       width: 100%;
       height:100%;
       left:0;
       top:0;
       margin:0 auto;
      z-index: 4;
     }

     .expandable-box a:hover{
         color:#333;

     }
     .expandable-box:hover span{
        background-image: url(https://i.sstatic.net/3mG2d.jpg);
     }
<div class="expandable-box" onmouse>
     <span class="icon-cont">                    
               
       </span>
       <a title="Title" href="page.html.html">This</a>
        </div>

2. using img with JS onmouseenter/onmouseleave

function changeImg(){
 var item=document.getElementById("imageid");
 if(item.src=="https://i.sstatic.net/mSXoO.png")
   item.src="https://i.sstatic.net/3mG2d.jpg";
 else 
  item.src="https://i.sstatic.net/mSXoO.png";
}
.expandable-box {
  width: 20%;
  height: 151px;
  margin: 30px 12px 30px 0;
  padding: 30px;
  border: 2px solid #fff;
  border-radius: 5px;
  box-sizing: border-box;
  display: inline-block;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  -webkit-transition: all .3s ease-in-out;
  -moz-transition: all .3s ease-in-out;
  -o-transition: all .3s ease-in-out;
  -ms-transition: all .3s ease-in-out;
  transition: all .3s ease-in-out;
}

.expandable-box .icon-cont {
  margin: 20px auto;
  display: block;
  text-align: center;
  position: absolute;
  top: 10px;
  left: 0;
  right: 0;
}

.expandable-box a {
  display: block;
  color: #fff;
  font-weight: 600;
  font-size: 15px;
  text-align: center;
  position: absolute;
  padding-top: 35%;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  margin: 0 auto;
  z-index: 4;
}

.expandable-box a:hover {
  color: #333;
}

.expandable-box:hover{
  background: #fff;
}
<div class="expandable-box" onmouseenter="changeImg()" onmouseleave="changeImg()">
  <span class="icon-cont">     
    <img id="imageid" style="display: block; margin-left: auto; margin-right:auto;width: 64px;height:64px;" src="https://i.sstatic.net/mSXoO.png" /> 
  </span>
  <a title="Title" href="page.html.html">This</a>
</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

Adding padding-top to the h1 element within a vertically aligned div

I have a div with a set height and the content inside adjusts vertically according to the height. To align it vertically, I added the property display-table; to the parent div and display: table-cell; vertical-align: middle; to the child div. The alignment ...

What is the best way to link to a CSS file located in a different directory while being in a subdirectory?

For a project I am working on, I am developing a simple streaming site and have organized my files into different folders. These folders include: HTML CSS Shows Within the "Shows" folder, there is a subfolder named "Testshow". I am facing an issue with ...

What is the best way to ensure an image is shown in full screen exclusively on mobile devices?

My goal is to have two different site templates, one for desktop (which is already good) and one for mobile. For the mobile template, I want it to display only an image that fills the entire screen. The image should take up all the available space in term ...

Error: 'require is not defined' pops up while trying to import into App.js for a React App built with CDN links

My latest project involves a React App that showcases an h1 tag saying "Hello World" on the browser. Rather than utilizing npm, I have opted for CDN links to set up the application. The structure of the App consists of three key files: An index.html file ...

Turn off the autofill option for passwords in Google Chrome

Is there a way to prevent the password autocomplete dropdown from appearing when clicking on the password field? In Chrome Version 61.0.3163.100, none of the solutions seem to be working. Screenshot This issue has been raised many times, but setting autoc ...

The pseudo class before is experiencing issues with background color in Safari and not functioning as

Issue with Pseudo class before not displaying background colour in Safari browser To view the code, please visit the following link here HTML <div id='test'> <p>Test </p> </div> CSS #test { position: relative; per ...

Arrange the table by adding and editing before and after appending

I have a table data that needs to be dynamically appended. But I want to allow users to add new data using text input and also edit the existing data before and after it's appended. The problem is that when I append new data, it overwrites the previo ...

Spacing between Div tags

Struggling with a tricky HTML cross-browser problem. The site was built by multiple people, making it quite messy, but needs to go live as soon as possible! Each of the 4 page layouts on the site are different and handled by separate classes. However, whe ...

The dimensions of the bottom border on left floating divs are inconsistent

One of my designers frequently uses a particular technique in various designs, and I'm struggling to determine the most effective way to style it with CSS so that it fluidly adapts (as it will be incorporated into a CMS). Essentially, when implementi ...

Ways to retrieve the innerHTML of a Script Tag

After spending what feels like an eternity searching, I still can't figure out how to get the innerHTML of a script tag. Here's where I'm at... <script type="text/javascript" src="http://www.google.com" id="externalScript"></script ...

What could be causing the href to malfunction on my local website?

I'm currently working on adding a new link that directs to a local HTML website within a menu list on a website. The main website is in ASPX format, but my focus is on the HTML version. The link I want to add leads to an HTML website stored on my loca ...

What is the best way to split a single column into two using blocks?

I am working with a container that currently has all its blocks lined up in a column. I need to divide them into two columns, with 5 blocks in each. Unfortunately, the plugin I am using generates the code for me and does not allow me to insert my own div e ...

Half-extended Bootstrap 4 container reaching the edge of the page

I'm looking to create a layout with a .container that consists of two distinct halves. The left half should function as a standard .col-6, while the right half should extend all the way to the edge of the screen, similar to a .col-6 within a .contain ...

Choosing multiple classes using Xpath

<div class="unique"> <a class="nested" href="Another..."><img src="http://..."/></a> <p> different.... </p> <p><img src="http://....." /></p> </div> I have this interesting HTML struc ...

Ensure that the footer remains at the bottom of the page without being fixed to the bottom

I am currently working on configuring the footer placement on pages of my website that contain the main body content class ".interior-health-main". My goal is to have a sticky footer positioned at the very bottom of these pages in order to eliminate any wh ...

Concealing a Column within a Hierarchical HTML Table

Can anyone guide me on how to hide multiple columns using jQuery with the ID tag? I've tried implementing it but it doesn't seem to work. I also attempted to switch to using Class instead of IDs, but that didn't solve the issue either. Any h ...

Styling two divs with borders

Hello everyone, I could really use some assistance with merging the borders of two <div>s. Here's what I'm trying to achieve compared to where I currently stand: view image here and this is my desired outcome view image here Would anyone b ...

Particle JS - Taking over the entire screen

I have incorporated Particle JS into the banner using the link provided. It should be confined within the banner, with a white background underneath displaying the header text "hello there." However, the Particle.JS effect is currently taking over the enti ...

Extracting information from an HTML table with Jsoup

Currently, I am attempting to utilize jsoup in order to parse an HTML table. As a newcomer to jsoup, I have taken the time to review some tutorials on how it operates. My objective is to retrieve values from each column within this specific website's ...

Adjust the dimensions of the dropdown menu

Objective: How can I adjust the width of a select dropdownlist that is utilizing bootstrap v2? Challenge: I am uncertain about how to modify the width in the context of bootstrap. Additional Information: Keep in mind that there are three dropdownli ...