0

In my php page dynamically visualize the thumbnails. To make sure that these are all of the same size I do in this way

<a class="zoom" href="...">
<img src="thumb/default.png" width="130" style="background-image:url(thumb/<?php echo $images_jpg;?>);" class="centered" />
</a>

CSS

img.centered {
background-repeat: no-repeat;
background-position: center center;
}

/* 1 attempt */
a.zoom:hover {
background-image: url(thumb/zoom.jpg);
}

/* 2 attempt */
a.zoom img:hover {
background-image: url(thumb/zoom.jpg);
}

I would like to display a different image on event: hover, but this does not work. How could I do that? thanks

2
  • try setting display: block to the img Commented Feb 4, 2014 at 11:01
  • is it like if you hover on image, u shud see the image present in the background???? Commented Feb 4, 2014 at 11:04

3 Answers 3

1

You could always do it like this.

HTML:

<div class="image" style="background-image:url(http://www.randomwebsite.com/images/head.jpg);">
    <div class="overlay"></div>
</div>

CSS:

.image {
    width: 200px;
    height: 200px;
    border: 1px solid;
}
.overlay {
    width: 100%;
    height: 100%;
}
.overlay:hover {
    background: url(http://1.bp.blogspot.com/-lJWLTzn8Zgw/T_D4aeKvD9I/AAAAAAAACnM/SnupcVnAsNk/s1600/Random-wallpapers-random-5549791-1280-800.jpg);
}

So here we have the image you are getting via PHP on top as a div. And inside we have the overlay, the image you want when a user is hovering. So we set that to 100% width and height so it takes up all of the parent div and set the hover.

DEMO HERE

Sign up to request clarification or add additional context in comments.

Comments

1

In your example the <img> always lays over the <a> background-image. To avoid that, you could hide the image on hover. But that is kinda ugly ;)

a.zoom:hover {
    background-image: url(thumb/zoom.jpg);
}

a.zoom:hover img
{
   opacitiy: 0;
}

4 Comments

This seemed like a good solution, but unfortunately it does not work
I can only guess what your problem is. But i think your problem is, that you do not only have one, but multiple zoom images, that you want to change on hover. Since the URL for the zoom-pictures are generated automatically, you should go for a javascript solution. Is that correct?
in part is correct. I visualize the images taking their names from a db. The images are different sizes and to display the same size I use (style: background.url...) generated dynamically. I just want to show an image/overflow (always the same - "zoom the image") on the thumb mouse over
@PaoloRossi Check my answer out.
0

try this

<img class="centered" src="thumb/default.png"/>

and jquery

$(".centered").attr("src","second.jpg");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.