tags:

views:

37

answers:

2
+1  Q: 

css hover on image

I have this simple problem. I'll try to explain with a sample code

An image in a td

<td style="text-align:right;"><a class="ratingstar" href="javascript:rate('1','27')" title="1"><img src="assets/images/star.png" alt="*" /></a></td>

Css for this image

.ratingstar:hover img, .ratingstar:focus img
{
   margin-bottom: 3px;
}

So when i hover on image it moves up a bit to give this lifted feeling. Problem is that at the same time height of td increases and so elements below gets pushed down.

How can i keep td's size fixed while having my image lifted. I can increase height of td to solve this but as this is a mobile site I don't want to set specific height to size but have it expanded according to content inside.

I hope this is clear enough.

A: 

You can do this with a negative top margin:

.ratingstar:hover img, .ratingstar:focus img
{
   margin-top: -3px;
}

Or, if you do not need the actual image, you can do this really easily using background image and positioning.

.ratingstar
{
   background-image: url(assets/images/star.png);
   background-position: 0 0;
   background-repeat: no-repeat;
}
.ratingstar:hover img, .ratingstar:focus img
{
   background-position: 0 -3px;
}
Dustin Laine
I need the image(s) it is a rating display system and images gets listed according to given rating
nLL
Check my update
Dustin Laine
margin-top works too. to be fair i'm gonna have to accept Felipe Alsacreations's answer as it has been posted before
nLL
No worries, glad you found your solution they are both elegant solutions. I prefer the background image solution myself as it allows the use of including the image in a sprite.
Dustin Laine
A: 

First solution (seen in CSS-tricks website): use relative positioning like

.ratingstar:hover img, .ratingstar:focus img {
  position: relative;
  bottom: 3px;
}

Second solution: play with padding-bottom going from 3 to 0px when margin-bottom goes from 0 to 3px, if your design permits it.
edit: It could also be border-top or bottom (same color as your background, if it isn't a gradient or image but a unique color)

Felipe Alsacreations
thanks, first solution works
nLL