Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I desaturate and saturate an image using CSS?

Update

I just realized that the desaturation is only working in Chrome. How do I make it work in FF, IE and other browsers? (Headline changed)


I'm converting a color picture to greyscale by following the suggestions here: Convert an image to grayscale in HTML/CSS

And it works great (in Chrome): http://jsfiddle.net/7mNEC/

<img src="https://imagizer.imageshack.us/v2/350x496q90/822/z7ds.jpg" />  // CSSS img {     filter:         url(~"data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale");     -webkit-filter: grayscale(100%);     -moz-filter:    grayscale(100%);     -ms-filter:     grayscale(100%);     -o-filter:      grayscale(100%);     filter: gray;  }  img:hover {     filter: none;     cursor: pointer; } 

But I'm not able to remove the desaturation on e.g. mouse over.

Any ideas to what I'm doing wrong?

like image 899
Steven Avatar asked Apr 10 '14 17:04

Steven


People also ask

What is saturate in CSS?

The saturate() function is an inbuilt function in CSS which is used to super-saturate or desaturates the input image. Syntax: saturate( amount ) Parameters: This function accepts single parameter amount, which holds the amount of conversion. The value of the parameter is set in terms of number or percentage.

How do you saturate an image in CSS?

Use the saturate() function to adjust the saturation of an image. The CSS saturate() function is used with the filter property to adjust the saturation levels in an image. The saturate() function requires an argument to be passed to it.

How do I darken an image using CSS?

Method 1: Using the filter property: The filter property is used to apply various graphical effects to an element. The brightness() function can be used as a value to apply a linear multiplier to make it appear darker or lighter than the original.

How do I change a color picture to black and white in CSS?

First, brightness(0) makes all image black, except transparent parts, which remain transparent. Then, invert(1) makes the black parts white.


2 Answers

You just have to reverse the grayscale for each browser prefix CSS property:

img:hover {     filter: none;     -webkit-filter: grayscale(0%);     -moz-filter:    grayscale(0%);     -ms-filter:     grayscale(0%);     -o-filter:      grayscale(0%);     cursor: pointer; } 

http://jsfiddle.net/7mNEC/1/

like image 137
Alex W Avatar answered Sep 30 '22 09:09

Alex W


Since this question is about saturation, the saturate() filter may be a better fit. This also allows for super-saturated colors (values above 100%):

img {     filter: saturate(0%); } img:hover {     filter: saturate(300%); } 

https://jsfiddle.net/t1jeh8aL/

like image 25
Sphinxxx Avatar answered Sep 30 '22 08:09

Sphinxxx