Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG filter not showing up in Firefox, working fine in Chrome

I want a piece of dark text on dark background to have a white glow outside of it. Although the default drop shadow filter in CSS (like filter: drop-shadow(2px 2px 2px black)) officially should support a 'spread-radius' fourth attribute, support for this attribute is basically non-existent. Without this extra spread, the drop shadow will not be big enough to allow the text to be read.

So, I decided to define my own filter in an SVG tag, including a dilation operation to implement the extra spread, and apply that to the text instead. This filter works fine in Chrome, but causes the text to become entirely invisible in Firefox (both tested under Ubuntu 14.04). According to caniuse.com (usually very reliable, I've found), Firefox should support the filter perfectly fine.

The HTML code with SVG filter:

<svg xmlns="http://www.w3.org/2000/svg">
<defs>

<filter id="my-drop-shadow" x="0" y="0" width="200%" height="200%">

    <feColorMatrix type="matrix" values=
                "0 0 0 0   1
                 0 0 0 0   1
                 0 0 0 0   1
                 0 0 0 1   0"/>
    <feMorphology operator="dilate" radius="2" />
    <feGaussianBlur stdDeviation="2" in="coloredOut" result="coloredBlur"/>
    <feMerge>
        <feMergeNode in="coloredBlur"/>
        <feMergeNode in="SourceGraphic"/>
    </feMerge>

</filter>

</defs>
</svg>

<p>Hello there.</p>

The CSS:

body {
    color: #000; background: #002;
    font-family: serif; font-size: 30px;
    font-weight: bold;
}

p {
    -webkit-filter: url(#my-drop-shadow);
    filter: url(#my-drop-shadow);
}

Result on Chrome (version 46.0.2490.80 (64-bit)):

Result on Chrome

Result on Firefox (version 42.0):

Result on Firefox

I've put the code above in a working CodePen (edit: note that I've now updated the CodePen to reflect @RobertLongson's answer and it works; see below though for why this was not the full answer).

Any ideas? Typo I haven't spotted in my SVG code?

like image 351
EelkeSpaak Avatar asked Nov 12 '15 16:11

EelkeSpaak


3 Answers

For me, it was that I had a display:none on the parent <svg> tag which I think causes Firefox to completely ignore the filter tags inside. So changing <svg style="display:none"> to <svg style="position:absolute; height:0"> did the trick.

like image 194
Chris Avatar answered Nov 06 '22 14:11

Chris


There's an input called coloredOut but no output called coloredOut so the filter chain fails. Removing the in="coloredOut" attribute seems to fix that. You might want to raise a Chrome bug that it's not enforcing valid filter chains.

In addition the SVG pushes the text down so you can't see it in the codepen. Adding width="0" height="0" to the <svg> element fixes that issue.

like image 6
Robert Longson Avatar answered Nov 06 '22 13:11

Robert Longson


The answer by @RobertLongson is excellent and provides part of the solution. In the codepen I provided, everything works when I implement his fix. However, my own code still did not work properly.

The crucial thing is that in the real code, my CSS was in an external file. It turns out that Firefox (correctly!) interprets my selector in filter: url(#my-drop-shadow) as referring to the CSS file, and not to the HTML document to which the CSS is linked. When I specify it as a URL + selector, as in filter: url(../../index.html#my-drop-shadow), everything works fine. (Alternatively, I could have put the filter in a <style></style> element in the same HTML document.)


Source (why Firefox is correct); from CSS 2.1 spec:

In order to create modular style sheets that are not dependent on the absolute location of a resource, authors may use relative URIs. Relative URIs (as defined in [RFC3986]) are resolved to full URIs using a base URI. RFC 3986, section 5, defines the normative algorithm for this process. For CSS style sheets, the base URI is that of the style sheet, not that of the source document.

like image 4
EelkeSpaak Avatar answered Nov 06 '22 15:11

EelkeSpaak