tags:

views:

9125

answers:

6

What's the best way (if any) to make an image appear "grayed out" with CSS (i.e., without loading a separate, grayed out version of the image)?

My context is that I have rows in a table that all have buttons in the right most cell and some rows need to look lighter than others. So I can make the font lighter easily of course but I'd also like to make the images lighter without having to manage two versions of each image.

+34  A: 

does it have to be gray? you could just set the opacity of the image lower (to dull it). alternatively, you could create a <div> overlay and set that to be gray (change the alpha to get the effect).

html:

<div id="wrapper"><img id="myImage" src="something.jpg" /></div>

css:

#myImage {
    opacity : 0.4;
    filter: alpha(opacity=40); // msie
}

// or

#wrapper    {
    opacity : 0.4;
    filter: alpha(opacity=40); // msie
    background-color: #000;
}
Owen
Good ol' internet explorer. You can do more with the filter attribute; as it uses DirectDraw to do the rendering. But, then it only works on IE
nlaq
Works exactly as I wanted. Thanks.
Richard Poirier
+1 Nice! Thanks
Chuck Conway
You could fix comments in your code. Css does not allow for // comments, you should use /* */
Kamil Szot
A: 

Here's an example that let's you set the color of the background. If you don't want to use float, then you might need to set the width and height manually. But even that really depends on the surrounding CSS/HTML.

<style>
#color {
  background-color: red;
  float: left;
}#opacity    {
    opacity : 0.4;
    filter: alpha(opacity=40); 
}
</style>

<div id="color">
  <div id="opacity">
    <img src="image.jpg" />
  </div>
</div>
djensen47
A: 

Considering filter:expression is a Microsoft extension to CSS, so it will only work in Internet Explorer. If you want to grey it out, I would recommend that you set it's opacity to 50% using a bit of javascript.

http://lyxus.net/mv would be a good place to start, because it discusses an opacity script that works with Firefox, Safari, KHTML, Internet Explorer and CSS3 capable browsers.

You might also want to give it a grey border.

VonC
opacity is a css3 feature though, the filter part is for MSIE specifically, other browsers will pick up opacity
Owen
+3  A: 

Better to support all the browsers:

img.lessOpacity {      
   opacity: 0.4;
   filter: alpha(opacity=40);
   zoom: 1;  /* needed to trigger "hasLayout" in IE if no width or height is set */ 
}
alexmeia
+5  A: 

If you don't mind using a bit of JavaScript, jQuery's fadeTo() works nicely in every browser I've tried.

jQuery(selector).fadeTo(speed, opacity);
Nathan Long
A: 

You can use rgba() in css to define a color instead of rgb(). Like this: style='background-color: rgba(128,128,128, 0.7); '

Gives you the same color as rgb(128,128,128) but with a 70% opacity so the stuff behind only shows thru 30%. CSS3 but it's worked in most browsers since 2008. Sorry, no #rrggbb syntax that I know of. Play with the numbers - you can wash out with white, shadow out with gray, whatever you want to dilute with.

OK so you make a a rectangle in semi-transparent gray (or whatever color) and lay it on top of your image, maybe with position:absolute and a z-index higher than zero, and you put it just before your image and the default location for the rectangle will be the same upper-left corner of your image. Should work.

OsamaBinLogin