0

I'm trying to make image two appear below image one when a mouse hovers over image one.

<ul>
   <li>
    <img id="img1" src="imageone.png">
    <br>
    <img id="img2" src="imagetwo.png">  
  </li>
</ul>

Help is appreciated!

So far I have this CSS which does not seem to work

#img2 {
    display: none;
    position: absolute;
}
#img1:hover + #img2,
#img2:hover {

 display:block;
}
Billy
  • 2,823
  • 3
  • 16
  • 33

4 Answers4

2

The + selects siblings that are immediately after. Your images are not, because they are separated with a <br>.

Use ~ instead:

#img1:hover ~ #img2
Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346
1

You are very close.

First, remove the <br> tag since it breaks the adjacent sibling selector.

The hover works as long as you over over the first image.

If you apply display: block to the img, then you don't need the <br> tag.

Depending on your layout, the tilde (~) selector could also work but it depends on whether you have other images in the layout (I am thinking a photo gallery).

ul {
  list-style: none;
}
#img2 {
  display: none;
}
#img1:hover + #img2 {
  display: block;
}
<ul>
   <li>
    <img id="img1" src="http://placehold.it/150x50">
    <img id="img2" src="http://placehold.it/150x50">  
  </li>
</ul>
Marc Audet
  • 46,011
  • 11
  • 63
  • 83
  • I think your answer is more complete actually. I wont know until I include more images later in my design. Thanks though – Billy Jan 26 '16 at 23:20
0

use the tilde instead of the + sign, because you have a br inbetween

#img2 {
    display: none;
    position: absolute;
}
#img1:hover ~ #img2,
#img2:hover {

 display:block;
}
<ul>
   <li>
    <img id="img1" src="imageone.png">
    <br>
    <img id="img2" src="imagetwo.png">  
  </li>
</ul>
Billy
  • 2,448
  • 1
  • 10
  • 13
0

You may be after something like this:

.parent{
  position: relative;
}
.holder{
  position: fixed;
  width: 100px;
  height:100px;
  background-color: #efefef;
}
.img{
  width: 100px;
  height:100px;
  background-color: #ffefef;
}
#img2{
  display: none;
}
.holder:hover #img2{
  display: block;
}
<ul>
   <li class="parent">
     <div class="holder">
       <img class="img" id="img1" src="imageone.png">
       <br>
       <img class="img" id="img2" src="imagetwo.png">  
    </div>
  </li>
</ul>
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100