0

I have actually a structure like this.

<div class="container">
   <ul>
       <li>
         <img>
       </li>
   </ul>
</div>

There is always a background image for the main container. What I want to achieve is to change the background image of the container when I hover the mouse on the image in the list. And the background-image should be the one in the list.

and when the mouse is away, the background image should be the original (the one at the beginning).

Is it possible to do it with css only? or javascript?

Thanks.

P.G
  • 1
  • 5

1 Answers1

1

It can not be achieved using CSS. It's in the name: Cascading Style Sheets only supports styling in cascading direction, not up.

You can do this using Javascript

var myDiv = document.getElementById('foo');

myDiv.onmouseenter = function() { 
    // code to change image here
}

myDiv.onmouseleave = function() { 
    // code to reset to old image here
}
void
  • 36,090
  • 8
  • 62
  • 107