G P
G P

Reputation: 65

Changing picture inside img on hover with css

I need to change source URL on hover.

I have tried this but won't work :

This is my Css:

.nav > li > div {
    position: absolute;
    display: block;
    width: 100%;
    top: 38px;
    left: 0;
    opacity: 0;
    visibility: hidden;
    overflow: hidden;
    background: #ffffff;
    border-top-color: #5f7ec7;
    border-top-style: solid;
    border-top-width: 1px;
    border-bottom-color: #5f7ec7;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    -webkit-transition: all .3s ease .15s;
    -moz-transition: all .3s ease .15s;
    -o-transition: all .3s ease .15s;
    -ms-transition: all .3s ease .15s;
    transition: all .3s ease .15s;
}
.nav > li:hover > div {
    opacity: 1;
    visibility: visible;
    overflow: visible;
}

And This is my html:

<ul class="nav" style="width: 100%">
    <li>
        Something
        <a title="D" itemprop="url" href="/Default.aspx"><span itemprop="name">Something</span><img src="/images/Top-Menu-Layer/arrow_122b87.gif" style="vertical-align:middle; padding-left:5px; height:4px; width:7px" /></a>
       </li>
</ul>

I would like to change the content of the img tag inside the html via the css. How can I do it ? Any advice ?

Upvotes: 4

Views: 4146

Answers (2)

XYZ
XYZ

Reputation: 4480

Use content:url("imageURL"); Note[ This method only works in chrome not in Firefox or IE ].

.image{
  width:100px;
  height:100px;
}

.image:hover{
    content:url("http://via.placeholder.com/350x150");
}
<img class="image"/>

The best method is to use javascript or if you want a complete css solution use background-image instead of img tag and change background-image on hover

Upvotes: 3

Ehsan
Ehsan

Reputation: 12951

You can't with pure css,but can help Jquery.

$(document).ready(function(){
  $('.nav li').hover(function(){
        $('.target').attr('src','https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRLd77JVS_4xE04KaLd3E-j2pTyiO_fTcEwHgQL9tj8GMsXZQW2');
   },function(){
	$('.target').attr('src','http://www.mrwallpaper.com/wallpapers/cute-bunny-1600x900.jpg');
   })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <ul class="nav" style="width: 100%">
   <li>
     Something
     <a title="D" itemprop="url" href="/Default.aspx">
        <span itemprop="name">Something</span>
        <img class="target" src="http://www.mrwallpaper.com/wallpapers/cute-bunny-1600x900.jpg" style="vertical-align:middle; padding-left:5px; height:40px; width:70px" />
        </a>
    </li>
</ul>

Upvotes: 1

Related Questions