Reputation: 8541
As far as I understand, CSS filters should work anywhere in Chrome, but I fail to make them apply to SVG elements.
This works fine in all most browsers:
div{ filter:sepia(50%) }
However, this will not work in Chrome:
rect{ filter:sepia(50%) }
Here is an example:
div{
width:100px;
height:50px;
background-color:red;
}
rect{
fill:red;
}
rect:hover, div:hover{
-webkit-filter:sepia(50%);
-moz-filter:sepia(50%);
filter:sepia(50%);
}
<h2>SVG</h2>
<svg width="100" height="50">
<rect x="0" y="0" width="100" height="50"/>
</svg>
<h2>DIV</h2>
<div></div>
...and here is a fiddle: https://jsfiddle.net/LtffLagn/2/
Upvotes: 18
Views: 8447
Reputation: 8541
All credit to @Robert Longson, who gave the answer: CSS filters can not be applied to SVG elements in Chrome. They can, however, be re-implemented as SVG filters, with some extra work. This is what I ended up with:
rect{
fill:red;
}
rect:hover{
filter:url("#sepia");
filter:sepia(100%);
}
<svg width="100" height="50">
<defs>
<filter id="sepia">
<feColorMatrix type='matrix' values='0.30 0.30 0.30 0.0 0
0.25 0.25 0.25 0.0 0
0.20 0.20 0.20 0.0 0
0.00 0.00 0.00 1 0'/>
</filter>
</defs>
<rect x="0" y="0" width="100" height="50"/>
</svg>
Firefox will now use the CSS filter, and Chrome the SVG filter. Of course, if you manage to make the SVG filter work the way you want, you could just stick with that. For me, I never managed to get exactly the effect I wanted, so I let Firefox use the better looking CSS filter.
Bug tracker for CSS filters on SVG elements in Chrome/ium: https://bugs.chromium.org/p/chromium/issues/detail?id=109224
Edit February 2021: CSS filters on SVG elements will start working in Chrome 89, if everything goes according to plan!
Edit 2023: CSS filters on SVG elements work in Chrome now.
Upvotes: 19