How do I invert colors of an image (jpg/png..) in either css if possible or javascript?
Previous related questions don't give enough detail.
CSS allows you to invert the color of an HTML element by using the invert() CSS function that you can pass to the filter property. Because the default color of the text is black, the color is inverted into white with filter: invert(100%) syntax.
To invert color on all elements of a page with JavaScript, we just set use the invert(100%) value with CSS. Then we can invert all the colors by writing: const css = `html { -webkit-filter: invert(100%); -moz-filter: invert(100%); -o-filter: invert(100%); -ms-filter: invert(100%); }` const head = document.
The invert() CSS function inverts the color samples in the input image.
CSS3 has a new filter attribute which will only work in webkit browsers supported in webkit browsers and in Firefox. It does not have support in IE or Opera mini:
img { -webkit-filter: invert(1); filter: invert(1); }
<img src="http://i.imgur.com/1H91A5Y.png">
Can be done in major new broswers using the code below
.img { -webkit-filter:invert(100%); filter:progid:DXImageTransform.Microsoft.BasicImage(invert='1'); }
However, if you want it to work across all browsers you need to use Javascript. Something like this gist will do the job.
If 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