views:

184

answers:

3

Hello All,

I would like to brighten an image on my webpage on mouseover using css or javascript. Ive seen some examples using opacity and filters in styles but they dont seem to work for me.

Thanks in advance

CP

+4  A: 

Why not? You can always set the background of the parent container to #fff (white) and then lower the opacity of the image.

HTML:

<div class="white">
    <img src="image.jpg" alt="Image" />
</div>

CSS:

.white { background: #fff; }
img:hover {
    opacity: 0.5;
    filter: alpha(opacity=50);
}

Another solution is to use a JavaScript library, such as Pixastic.

Mickel
I realise CSS < Photoshop, but that won't brighten the image as much as wash it out... Of course, it could be just what CP is after though. :)
MatW
Added another suggestion as well, the use of Pixastic: http://www.pixastic.com/lib/docs/actions/lighten/
Mickel
I get the errorfilter is not a known css property nameopacity is not a known css property name
CP
I'm too slow. You may get that error, as I dont think it is valid CSS. I cannot remember why. But if it works, it works. Who cares if it validates.
Ryan
it would be if it worked. when i hover over the image nothing seems to happen
CP
it won't work in all browsers. What browser are you testing in?
Mickel
ie 6 i can get the style to brighten but not on mouseover see post below
CP
IE6 does not support the :hover psudeo class on other elements than anchors. You can get around this by turning the image to an anchor or use JavaScript to add/remove class as the user hover over the image.
Mickel
thanks mickel I used the javascript methodThanks to all who replied
CP
A: 

I would use something liek:

  filter: alpha(opacity=80);
  opacity: 0.8;

As far as I know, this is the only way to accomplish it in CSS. Is that what you are after?

Ryan
Yes, I can get it to brighten the image but its not working on mouseover so i must be doing something wrong!img class = "mouseover" .mouseover img { opacity: 0.3; filter: alpha(opacity=30); }
CP
In this case, change that to: img.mouseover:hover { etc. }
Ryan
the class needs to come immediately after the tag. then the selector, immediately after that. so: tag.class:selector { etc. }
Ryan
Thanks Ryan, nearly there the hover bit doesnt work tho .img.mouseover { filter: alpha(opacity=100); opacity: 1.0; } .img.mouseover:hover { filter: alpha(opacity=30); opacity: 0.3; }
CP
+1  A: 

You could use CSS sprites. Create 2 versions of the image: one normal, one bright and combine them into 1 image using Photoshop or whatever you use.

There's a good explanation of what CSS sprites are here.

Of course, this may not be something you could use on your site. For example, it's probably not something you'd want to use on large images (they'd double in size). You also couldn't do it this way if the images you want to brighten come from outside sources or are uploaded by users for example. It would work well for things like navigation bar images and other UI elements.

thesonglessbird