Is there any way to colorize a white PNG image with CSS only?

后端 未结 4 887
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 07:33

I know there are many css filters out there especially for webkit, but I can\'t find a solution for colorize a white (#FFFFFF) image. I\'ve tried some combination of the fil

4条回答
  •  [愿得一人]
    2020-12-08 08:25

    This can be done with css masking, though unfortunately browser support is really bad (I believe webkit only).

    http://jsfiddle.net/uw1mu81k/1/

    -webkit-mask-box-image: url(http://yourimagehere);
    

    Because your image is all white, it is a perfect candidate for masking. The way the mask works is that wherever the image is white, the original element is shown as normal, where black (or transparent) the original element is not shown. Anything in the middle has some level of transparency.

    EDIT:

    You can also get this to work in FireFox with slight help from svg.

    http://jsfiddle.net/uw1mu81k/4/

    div {
      width: 50px;
      height: 50px;
      mask: url(#mymask);
      -webkit-mask-box-image: url(http://www.clker.com/cliparts/F/5/I/M/f/U/running-icon-white-on-transparent-background-md.png);
    }

提交回复
热议问题