1

I have two images inside a div. When the user hovers over the second image, the first one's opacity should go to 40%. I problem is that I cannot select img.first when img.second is being hovered over. I have tried looking into the general sibling selector, but that seems to only select the elements that come after your initial selector.

I know this can be done with jQuery, but I'm wondering if there is a pure CSS solution?

<div>
  <img class="first" src="#">
  <img class="second" src="#">
</div>

div > img.second:hover ~ img.first { opacity:0.4; filter:alpha(opacity=40); } //failed
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Jon
  • 8,205
  • 25
  • 87
  • 146

4 Answers4

4

I have tried looking into the general sibling selector, but that seems to only select the elements that come after your initial selector.

That is correct. As such, with a pure CSS selector this isn't possible.

However, depending on your layout, you may be able to use multiple rules with selectors such as div:hover and img:hover and play with opacity values to get at what you want; see the other answers for examples. But if you want a more foolproof solution you'll be better off with jQuery.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
2

try something like:

div:hover .img {
  opacity: 0.4;
}

div .img:hover {
  opacity: 1;
}

.img {
  display: inline-block;
  background: green;
  width: 100px;
  height: 40px;
}

demo: http://jsbin.com/idowoz/2/

Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • Assuming you're happy jiggling your html as well as CSS the obvious thing to do would be to reorder the img tags, since img:hover+img is a totally valid CSS selector. – podperson Dec 21 '12 at 17:43
  • Unfortunately, I cannot re-order my HTML. – Jon Dec 21 '12 at 17:48
  • @icu222much I don't see why you should, it would work with your html too. I just used span and classes because I did not have valid image sources to test with. Just apply the css to you markup and it will work. – Yoshi Dec 21 '12 at 17:50
  • @podperson I don't get what you're trying to say. – Yoshi Dec 21 '12 at 17:54
  • @yoshi, Sorry, my first comment was directed at podperson. – Jon Dec 21 '12 at 17:55
  • @Yoshi I like your solution, however, it didn't work for me. My example is a much simpler version of what I am working on. My current set-up involves overlaying a magnifying glass icon on top of the image which must stay at an opacity of 1.0. I appreciate the effort though :) – Jon Dec 21 '12 at 17:57
0

Try this css:

div:hover img{
    opacity:0.5            
}
div:hover img:hover{
     opacity:1           
}  

​Test: http://jsfiddle.net/WpCtL/2/

karacas
  • 2,054
  • 1
  • 19
  • 29
0

You can do a trick to make it seem like this is what is happening: http://jsfiddle.net/cwxCX/3/

<div>    
    <img src="http://placekitten.com/200/200">
    <img src="http://placekitten.com/300/300">
    <img src="http://placekitten.com/400/400">
    <img src="http://placekitten.com/500/500">
</div>

CSS

div{
    float:left;
}
img{
    width:100px;
    height:100px;
}
div img{
    float:right;
}

div img:hover ~ img{
    opacity:.4;
}
Shmiddty
  • 13,847
  • 1
  • 35
  • 52