Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invert colors of an image in CSS or JavaScript

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.

like image 364
laggingreflex Avatar asked Nov 10 '12 20:11

laggingreflex


People also ask

How do I invert the colors of an image in CSS?

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.

How do I invert colors in JavaScript?

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.

What is invert color in CSS?

The invert() CSS function inverts the color samples in the input image.


2 Answers

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">
like image 144
toxicate20 Avatar answered Sep 26 '22 10:09

toxicate20


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.

like image 33
Kareem Avatar answered Sep 24 '22 10:09

Kareem