I've run into a problem getting Firefox's filter css property to grayscale properly on the printed page. For some reason, the grayscaled image is not visible on the printout, though it displays as expected onscreen. After referring to questions like this one, I've gotten to this point (simplified to demonstrate the issue):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<style type="text/css">
.grayscale {
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"); /* Firefox 10+ */
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(100%); /* Chrome 19+ & Safari 6+ */
filter: grayscale(100%);
}
img {
width:100px;
}
</style>
</head>
<body>
<img class="grayscale" src="http://alltheragefaces.com/img/faces/png/rage-nuclear.png" />
<img src="http://alltheragefaces.com/img/faces/png/rage-nuclear.png" />
</body>
</html>
And here's the fiddle
Is there a viable workaround for this, or am I doing something wrong? Applying the filters to other elements seems to cause the same problem. I would greatly appreciate any constructive input.
Note: Using Firefox v20.0.1
It turns out this almost appears to be a browser bug in FireFox. I noticed that anytime (on screen) when an svg is referenced that the browser can't find, the I would get the exact same issue as I did when printing: the image layout would be reserved, but it would be blank space. This prompted me to wonder if there was a difference in what could be loaded/found when rendering for a printer rather than the screen, so I started trying different ways of loading/referencing the svg. I tried loading the svg from a separate file, from an id in the html, and inline, all in combination with defining the filter in a separate css file, in-page classes, and inline styles. Of all the combinations, this is what worked:
Define the svg in html, and reference it using inline styles or in-page css classes.
I know it sounds weird, but literally everything else I tried breaks, including doing everything the same but setting the filter css property in an external css file. Going the in-page class approach, here's what the fixed html would look like:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<style type="text/css">
.grayscale {
filter: url(#grayscale); /* Firefox 10+ */
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(100%); /* Chrome 19+ & Safari 6+ */
}
img {
width:100px;
}
</style>
</head>
<body>
<img class="grayscale" src="http://alltheragefaces.com/img/faces/png/rage-nuclear.png" />
<img src="http://alltheragefaces.com/img/faces/png/rage-nuclear.png" />
<svg xmlns='http://www.w3.org/2000/svg' height="0px">
<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>
</body>
</html>
And again, the modified fiddle from which you can print and now see the image just fine in Firefox.
Siiigh, this is the kind of thing I'd expect from IE...hopefully helps save somebody some time/grief/murderous thoughts
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have been reading for grayscale image support for a while now. I have found the solution with using CSS code in my template.css:
.partner {
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"); /* Firefox 3.5+ */
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(100%); /* Chrome 19+ & Safari 6+ */
}
.partner:hover {
filter: none;
-webkit-filter: grayscale(0%);
}
For image itself I am using:
<img class="partner" src="/joom/images/partners/image.png" alt="image logo" height="30" width="105">
However this doesn't work with IE9 and next versions of IE10, 11... I know that I should use SVG to get it working on IE10. So I need to use both CSS and JS. What files and what codes do I need to use to get SVG grayscale support for images?
Thank You!
For IE10/11 you must display the image via an SVG <image> element and not an html <img> element. Only SVG elements can be subject to filters in IE. i.e.
<svg height="30" width="105">
<image class="partner" xlink:href="/joom/images/partners/image.png" height="30" width="105">
<desc>image logo</desc>
</image>
</svg>
So, I have code for hover effect on picture. It works fine on Chrome, but problem is when I open site on Firefox. Here is code:
img.img-responsive {
/* for Webkit browsere, Chrome 19+, Safari 6+ ... */
-webkit-filter: grayscale(1);
/* this is for Firefox 3.5+, Firefox mobile */
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'>
<filter id=\'gs\'><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>#gs");
/* for IE6+ */
filter: gray;
}
img.img-responsive:hover {
/* for Webkit browsere, Chrome 19+, Safari 6+ ... */
-webkit-filter: grayscale(0);
/* this is for Firefox 3.5+, Firefox mobile */
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'>
<filter id=\'gs\'><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>#gs");
/* for IE6+ */
filter: gray;
}
So, what should I do that hover effect works on Firefox? Pictures are shown in black-white and when you hover them color should appear.
filter: grayscale(100%) /* Or lower */
should work in a latest version of Firefox. But then again, this is Firefox.
If it doesn't, try replacing your 'filter' url with this:
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"); /* Firefox 3.5+ */
By the way, a simple Google search for "Firefox Grayscale" turned out a StackOverflow reply to your question. It was answered by defau1t a while ago:
CSS Filter not working in Firefox
thanks for help but this didn't solve my problem. Either your answers works for me. Now hover effect works only in one solution, picture is now in color and when I hover it it becomes black-white. I need opposite, picture has to be black-white and when I hover it, it has to be in color.
The filter effect you are using for Firefox seems to be very convoluted. All modern browsers should be able to use the CSS3 notation now, but specific selectors for slightly older browsers are available;
You might need to use the -moz selector for your version of Firefox.
The full list for all browsers is below;
img {
filter: grayscale(0);
-webkit-filter: grayscale(0);
-moz-filter: grayscale(0);
-o-filter: grayscale(0);
-ms-filter: grayscale(0);
}
img:hover {
filter: grayscale(1);
-webkit-filter: grayscale(1);
-moz-filter: grayscale(1);
-o-filter: grayscale(1);
-ms-filter: grayscale(1);
}
Hope this helps.
try JS fix
It is using javascript to detect the browser and creates a canvas and then uses filter.
http://www.majas-lapu-izstrade.lv/cross-browser-grayscale-image-example-using-css3-js/
I've been using -webkit-filter: grayscale(100%); to make an image gray. Is it possible to tint the image blue? I thought I could possibly use and SVG filter and reference it in the CSS like this:
-webkit-filter: url(filters.svg#grayscale);
file filters.svg:
<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>
The above code is also just a greyscale filter and I don't know enough about SVG to make a blue one. The blue I am looking for is #002060.
Any help or pointers in the right direction would be greatly appreciated.
I think you are looking for
filter: hue-rotate(xxdeg); /* unprefixed */
JSFiddle Demo
HTML (with raw image and filtered
<img src="http://lorempixel.com/output/abstract-q-c-200-200-8.jpg" alt="">
<img class="filtered" src="http://lorempixel.com/output/abstract-q-c-200-200-8.jpg" alt="">
CSS (with -webkit prefix only)
.filtered {
-webkit-filter: hue-rotate(180deg);
}
Useful Article
Useful Color Wheel
It looks like the rotate value you are looking for is 238deg but this may not have the effect you are looking for.
You can use blend modes, their support is increasing:
(credit: CSS-tricks)
https://css-tricks.com/almanac/properties/b/background-blend-mode/
And/or: https://css-tricks.com/snippets/css/monotone-image-css/
I have 3 icons of FCB, twitter and RSS and I want them to be grayscale but on hover they should change to color version. It is working great but these 3 images looks a bit different in grayscale. Is there a way to make them looks the same?
This is my code for grayscale:
img.grayscale{
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"); /* Firefox 10+, Firefox on Android */
filter: gray;
-webkit-filter: grayscale(1);
}
RSS and twitter looks almost the same but FCB icon is a lot darker. I think that the only way is to play with grayscale percentage. Is there any other solution?
You can add a class of fb on the fb image and set
img.fb {
opacity: 0.7;
}
This will give the illusion of making the image more light.
Here is the jsfiddle
I am trying to show some images on a page where they should be shown in grayscale, except on mouse hover when they smoothly transition into color. I've made it work nicely on IE, Chrome and Firefox, but it doesn't work on Safari 5.x. The problem is on Safari for Mac and Safari for Windows. Here is the code I have so far:
filter: url('desaturate.svg#greyscale');
filter: gray;
-webkit-filter: grayscale(1);
The first line loads an external .svg filter (I don't inline it with a url("data:... rule because I want to avoid a bug in old versions of Firefox).
The second line is for IE and seems to work just as well as filter:progid:DXImageTransform.Microsoft.BasicImage(grayScale=1);.
The last line about webkit is supposed to work on Safari 6 and above, as well as Chrome.
Is there any CSS rule to show the images with grayscale on Safari 5.x? Or, if that is not possible, can someone recommend a javascript solution, preferably one that will handle the animation to and from grayscale? I would like to avoid a server-side hack with grayscale images because that will mess up my HTML and then I'll have to do some nasty browser detection to serve HTML conditionally.
thanks
Edit:
As this has turned out to be a "notable question", please don't keep posting here more answers that only work on Safari 6 and above, or answers that include an .svg file in a data url. At the time when I wrote the OP, it was important for me to support some versions of Safari and Firefox that are today considered very dated, but nevertheless that was my original question.
I am well aware that for modern browsers grayscale filtering is easily accomplished with a few lines of CSS code, but the graphics designer was using Safari 5.x and the client was using Firefox 3.x at the time I did this project. The solution that worked for me was what Giona suggested, i.e. to use Modernizr to test for css-filtering, and if it's not supported to fall back to javascript.
If I was doing the same thing today, I'd be telling both to go update their browsers!
As you can see on caniuse.com , CSS3 filters are supported by very few browsers at the moment.
There are many JavaScript/jQuery fallback if you Google "javascript grayscale plugin".
Here are some:
Grayscale.js
jQuery GreyScale plugin
Hoverizr
Do it with canvas (tutorial)
But i've no experience with them, so i can't suggest you which one is the best.
I tried jQuery Black And White long time ago, and it gave me a lot of issues with relative/absolute positioned images, so maybe avoid it.
Including this Modernizr build into your pages, you can target browser not supporting filters via Javascript:
if(!Modernizr.css_filters){
/* javascript fallback here */
}
or CSS:
.no-css_filters .element {
/* css fallback here */
}
Oh, and don't forget dowebsitesneedtolookexactlythesameineverybrowser?
It's really simple, actually:
http://www.karlhorky.com/2012/06/cross-browser-image-grayscale-with-css.html
http://jsfiddle.net/KDtAX/487/
I tried using the javascript fallback, but it really made no sense, and it was really slow on large images. This made a lot more sense. Note that there is a new syntax for grayscale, and I had to manually edit the resulting minified CSS from LESS.
Here's my mixin:
.filter (...) {
-webkit-filter: #arguments;
-moz-filter: #arguments;
-ms-filter: #arguments;
-o-filter: #arguments;
filter: #arguments;
}
And my class:
.grayscale-hover, .home-image {
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"); /* Firefox 10+, Firefox on Android see http://jsfiddle.net/KDtAX/487/*/
.filter(grayscale(1) blur(1px));
filter: gray; /* IE6-9 */
-webkit-backface-visibility: hidden; /* Fix for transition flickering */
&:hover {
.filter(none);
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'1 0 0 0 0, 0 1 0 0 0, 0 0 1 0 0, 0 0 0 1 0\'/></filter></svg>#grayscale");
}
}
Works and tested in IE 6+, Firefox, Chrome.
This is something like that:
.grayscale {
filter: url(css/grayscale.svg#greyscale);
-webkit-filter: grayscale(1);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
}
You need to download the svg file either.
This one worked great for me:
img { float:left;
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\' filterRes=\'800\'><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"); /* Firefox 10+ */
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(100%); /* Chrome 19+ & Safari 6+ */
-webkit-backface-visibility: hidden; /* Fix for transition flickering */
-webkit-transition: all 1.5s ease;
-moz-transition: all 1.5s ease;
-ms-transition: all 1.5s ease;
-o-transition: all 1.5s ease;
transition: all 1.5s ease;
z-index: 40 !important;
display:block;
}
img:hover {
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\' filterRes=\'800\'><feColorMatrix type=\'matrix\' values=\'1 0 0 0 0, 0 1 0 0 0, 0 0 1 0 0, 0 0 0 1 0\'/></filter></svg>#grayscale");
-webkit-filter: grayscale(0%);}
The images looks really overexposed in Safari however (But they are in greyscale). And the transition isn't supported by Firefox.