Skip to content Skip to sidebar Skip to footer

Change Image Of Img Tag On Hover Using Css

I have a image menu. I want to change the image into TD table when the link is activated.
<

Solution 1:

Instead of using <img> tags use background-image property in your CSS file.

For example,

tabletda{
    display: block;
    background-image:url('FirstImageURL');
    /* other background properties like "background-size", if needed */
}

tabletda:active{
    background-image:url('SecondImageURL');
}

or else

You can change the image using content css property: (works in chrome)

.className { /* or "img" */content:url('ImagePathURL');
}

Working Fiddle.

The above you can do by assigning unique classes (or Id's) to each img tag. or else using :first-child and :last-child selectors in combination with +(sibling) selector. Something like this:

table > img:first-child{ /* css properties */ }                     /* first  child */table > img:first-child + img{ /* css properties */ }               /* second child */table > img:first-child + img + img { /* css properties */ }        /* third  child */table > img:first-child + img + img + img { /* css properties */ }  /* fourth child */table > img:last-child { /* css properties */ }                     /* fifth  child */

For more information check this link.

I hope you know CSS because you haven't used anywhere in your code :)

Solution 2:

a:visitedimg ,a:activeimg
{
content:url('change img link as you want);
}

Solution 3:

Two choices:

JavaScript solution:

var links = document.getElementsByTagName("a");
var n = links.length;
for(var i = 0; i < n; i ++) {
    var cur = links[i];
    if(cur.getAttribute("href") == window.location) {
        cur.parentNode.childNodes[1].src = "http://i.imgur.com/newimagesrc.png";
    }
}

CSS-only solution:

Instead of using an img tag, use a div with a background-image, HTML would look like:

<tablewidth="100%"><tr><tdalign="left"><ahref="http://pinkmodels.16mb.com/"><divclass = "image first"></div></a></td>

CSS:

a:active > .image.first {
     background-image: url(newimageurl.png);
}

Post a Comment for "Change Image Of Img Tag On Hover Using Css"