Gray out image with CSS?

后端 未结 9 2125
轮回少年
轮回少年 2020-12-02 03:55

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

相关标签:
9条回答
  • 2020-12-02 04:51

    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;
      }
      
    0 讨论(0)
  • Use the CSS3 filter property:

    img {
        -webkit-filter: grayscale(100%);
           -moz-filter: grayscale(100%);
             -o-filter: grayscale(100%);
            -ms-filter: grayscale(100%);
                filter: grayscale(100%); 
    }
    

    The browser support is a little bad but it's 100% CSS. A nice article about the CSS3 filter property you can find here: http://blog.nmsdvid.com/css-filter-property/

    0 讨论(0)
  • 2020-12-02 04:52

    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.

    0 讨论(0)
提交回复
热议问题