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 filters, but none of them make my image colorized. I can only change the image in the range of grayscale, or sepia.
So my question is: Is there any way to change my totally white png to (for example) red using css only?
Like shown on this image:
We can change the color of PNG image using following CSS styles: filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url() | initial | inherit; The above property is used to set the visual effect of the image.
We can change the image color in CSS by combining the opacity() and drop-shadow() functions in the filter property. We can provide the color of the shadow from the drop-shadow function, and we can set the shadow as thin as possible so that the image's color will only change without forming an actual shadow.
If the product team was kind enough to also provide a white version of the image, you can simply toggle the image's src on hover. This can be done using JavaScript. You can use an onmouseover function that sets the image's src to white. png and then an onmouseleave function that sets the image's src to black.
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); }
<div style="background-color: red;"></div> <div style="background-color: blue;"></div> <div style="background-color: green;"></div> <div style="background-color: orange;"></div> <div style="background-color: purple;"></div> <svg height="0" width="0"> <mask id="mymask"> <image id="img" xlink:href="http://www.clker.com/cliparts/F/5/I/M/f/U/running-icon-white-on-transparent-background-md.png" x="0" y="0" height="50px" width="50px" /> </mask> </svg>
I recently have the same question and I think this approach is worth sharing for future readers. Try this
filter: brightness(50%) sepia(100) saturate(100) hue-rotate(25deg);
Brightness will darken the image, sepia will make it a bit yellowish, saturate to increase the color, and lastly hue-rotate to change the color. It's not cross browser friendly though:
It's not supported on IE
On chrome, hue-rotate(25deg)
will make any image red, but you need negative value in firefox e.g. hue-rotate(-25deg)
to make it red.
You can use this to target mozilla browsers: https://css-tricks.com/snippets/css/css-hacks-targeting-firefox/
Here are some useful tips and tools that I've used when I work with images/icons using css:
color:#f00;
just like font colors.filter: brightness(0) invert();
on NON-hover state, and filter: brightness(1);
on hover state. Hovever this will still won't work on IEIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With