0

i have 4 different icons and based on which icon i hover i want to change the background image in a div. The html is like so

  <div class="col side">
    <i id="icon1"></i>
    <i id="icon2"></i>
    <i id="icon3"></i>
    <i id="icon4"></i>
  </div>
  <div class="col">
    <img src="img1" id="img1" class="content" alt="" />
    <img src="img2" id="img2" class="content" alt="" />
    <img src="img3" id="img3" class="content" alt="" />
    <img src="img4" id="img4" class="content" alt="" />
  </div>

So when i hover icon1 i want to display img1 and so on. I know how to do it with jquery/javascript but is it possible with pure css?

Adam Baser
  • 115
  • 7

3 Answers3

0

it is not possible but try with adding background image on hover to each icon

#icon1:hover
 {
  background: url("path to img1");
 }

like wise add this to all icons but i think it will result in bad alignment on hover

Dinesh s
  • 313
  • 4
  • 19
0

It can be done with changed html-structure as someone has already mentioned in the comments. For example,

.content {
  display: none;
}

#icon1:hover ~ .col img#img1 {
  display: block;
}

#icon2:hover ~ .col img#img2 {
  display: block;
}
<div class="col side">
    <i id="icon1">First Icon</i> <i id="icon2">Second Icon</i>
    <div>Images:</div>
    <div class="col">
        <img src="https://themcrazycats.files.wordpress.com/2020/11/1ce50-b8480db4-5bda-4912-84e4-3f8d79cae545.jpeg?w=400&h=200&crop=1" id="img1" class="content" alt="" />
        <img src="https://themcrazycats.files.wordpress.com/2020/11/a66f0-4c055b18-860a-46b6-95a2-967b72e3547b.jpeg?w=400&h=200&crop=1" id="img2" class="content" alt="" />
    </div>
</div>
 

The key here is to make possible usage of css ~ selector by placing .col inside .side. Why? There is no parent selector as of now in css as pointed out here.

curveball
  • 4,320
  • 15
  • 39
  • 49
0

You can try some thing like

.content {
display: none;
}
#icon1:hover + #img1{
display: block; // or inline-block
}
#icon2:hover + #img2{
display: block;
}
#icon3:hover + #img3{
display: block;
}
#icon4:hover + #img4{
display: block;
}