views:

36

answers:

3

How can I change the alpha of an image in javascript? Also, what browsers support this?

+3  A: 

I don't think you can change the alpha of the image itself, but you can change it from the tag, or the container in which you put it.

The particular css properties I use for this are :

filter:alpha(opacity=50);
-moz-opacity: 0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
Francisco Soto
Since this question was tagged as jQuery and javascript, i am not sure he was looking for a CSS solution! (anyway, i learned something from this answer the -khtml-) Thanks
adardesign
+9  A: 

Using jQuery:

$(something).css('opacity', 0.5);

This will work in every browser.
However, it will not work properly with semi-transparent PNG images in IE 7 and 8 unless they are applied using a filter.

SLaks
+1 for mentioning PNGs with alpha transparency.
Pekka
A: 

The property's name is opacity and is supported by all major browsers, however in various different forms - opacity, -moz-opacity (FF pre 2.0 I think), filter (IE) and so on.

The easiest way to take is using a JavaScript Framework like jQuery or Prototype, they have a .opacity() function that takes care of the quirks.

Pekka