css - 显示无后,内联 SVG 在 Firefox 中无法正确呈现,为什么?

标签 css firefox rendering display inline-svg

我们在我们的应用程序中大量使用内联 SVG。最近我发现了不想要的行为(特别是在 Firefox 中,但我在 Chrome 中看到了类似的问题)来自不同 svg 的样式可能会相互干扰。我将我当前的问题追溯到“显示:无”的使用。

为了说明我的问题,请参阅下面随附的 HTML。这包含两个圆形德国国旗,第一个是隐藏的。在 chrome 中你会看到一个圆形的德国国旗(这是正确的)。在 Firefox 中,您会看到方形的德国国旗(这是不正确的)。如果您删除第一个 div 上的 display: none,则两个标志都会正确显示(圆形),或者如果您删除整个第一个 div,第二个标志将正确显示(圆形)。

为什么第一次出现 svg 时不显示,而第二次出现时显示不正确?

我想我可以通过在两个 svg 中使用不同的 id 来解决这个问题,但是由于我想将它们包含在一个包含内联 svg 的通用 html 文件的多个页面上,目前这是非常不切实际的。

<html>
<body>

<div style="display: none;">
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
    <defs>
        <circle id="bundesliga-a" cx="-52" cy="-52" r="32"/>
    </defs>
    <clipPath id="bundesliga-b">
        <use xlink:href="#bundesliga-a" overflow="visible"/>
    </clipPath>
    <g clip-path="url(#bundesliga-b)">
        <path fill="#FFF"
            d="M3.9 2.7S5.5.4 5-.5C4.5-1.6-8-14.2-8-14.2c.5-.9.7-1.8 1.1-2.7-1.4-1.6-1.6-3.6-.9-6.2.7-2.7 5.7-19.4-.7-31 .7-1.6 2.1-4.8 2.5-11.6 1.1-12.8-6.6-21-17.1-21-6.6 0-16.2-.5-20.1-2.3-8.5-4-10.6-14.3-6.9-17.2l.2-.2c1.1-.7 3-.2 5 .5 1.6.5 3.2.9 5 .7h.7c10.3-1.4 9.6-10.3 9.6-10.3 0 1.6-2.3 1.8-2.7 2.1-3.2.5-7.5-.7-9.8-.7-.9 0-2.7.5-3.2.7-5.7 2.1-12.5 7.3-10.5 16.2 2.1 9.3 8.4 11.8 15 12.8.5 0 .9.2 1.1.2 3.9.7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0-3.2-1.8-8.4.7 0 .9 3.4 9.6 10 9.8.7 0 1.4 0 2.1-.2 5.9-1.1 9.8-10.7 11.8-19.1 1.7-.5 3.2-1 5-1.6 3.9-1.6 7.1-3.9 9.1-6.4-1.1 4.3-6.2 7.1-6.2 7.1.5.7.7 1.8.5 3-.2 1.4-1.8 3.6-2.7 4.8-.7.7-1.4 1.4-1.8 2.1-.7 1.1-.5 2.5.2 3 0 0-4.1 2.5-6.4 8.9 4.8.5 10.3-.2 11.8-1.4 0 0 .7-3-.5-5.5.9 0 1.4-.2 1.8-.5 1.8-1.4 1.1-3.9 1.1-3.9.9-1.6 3.6-7.3 5.2-8.2 1.6-.9 2.7-1.6 3.4-3.6.5-1.6-.2-3-1.1-3.9l-.2-.2c-2.5-2.7.9-5.9 1.4-9.3 1.1 1.6.2 3.6 0 5.2-.2 1.8-.5 4.1 1.6 5.2.7.5 1.8.7 3.2.5 3.2 0 3.9-2.1 3.9-2.1-5.7-5.9-6.4-10.6-5.5-13.2 1.5-4.5 5.1-9.3 5.1-9.3-1.1 3.6-3.7 7-2.8 11.8.9 5 4.3 9.3 7.7 13 2.5 2.5 6.4 4.6 7.7 6.6C-1.8-1.5-3.7-2.7-2.8.5c.5 1.8 1.1 2.1 3 2.3 1.1 0 .9.7 2.7-.2C4.3 1.9 5 .8 5 .8s3.1-1.3 2.2-2z"/>
    </g>
    <g>
        <defs>
            <circle id="bundesliga-c" cx="32" cy="32" r="32"/>
        </defs>
        <clipPath id="bundesliga-d">
            <use xlink:href="#bundesliga-c" overflow="visible"/>
        </clipPath>
        <circle clip-path="url(#bundesliga-d)" fill="#D12028" cx="32" cy="32" r="32"/>
        <defs>
            <circle id="bundesliga-e" cx="32" cy="32" r="32"/>
        </defs>
        <clipPath id="bundesliga-f">
            <use xlink:href="#bundesliga-e" overflow="visible"/>
        </clipPath>
        <path clip-path="url(#bundesliga-f)" fill="#EEC258" d="M0 43.2h64V64H0z"/>
        <g>
            <defs>
                <circle id="bundesliga-g" cx="32" cy="32" r="32"/>
            </defs>
            <clipPath id="bundesliga-h">
                <use xlink:href="#bundesliga-g" overflow="visible"/>
            </clipPath>
            <path clip-path="url(#bundesliga-h)" fill="#292929" d="M0 0h64v20.8H0z"/>
        </g>
        <g>
            <defs>
                <circle id="bundesliga-i" cx="32" cy="32" r="32"/>
            </defs>
            <clipPath id="bundesliga-j">
                <use xlink:href="#bundesliga-i" overflow="visible"/>
            </clipPath>
            <path opacity=".07" clip-path="url(#bundesliga-j)" fill="#292929" d="M17.6 64H0V0h40"/>
        </g>
    </g>
</svg>
</div>

<div>
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
    <defs>
        <circle id="bundesliga-a" cx="-52" cy="-52" r="32"/>
    </defs>
    <clipPath id="bundesliga-b">
        <use xlink:href="#bundesliga-a" overflow="visible"/>
    </clipPath>
    <g clip-path="url(#bundesliga-b)">
        <path fill="#FFF"
            d="M3.9 2.7S5.5.4 5-.5C4.5-1.6-8-14.2-8-14.2c.5-.9.7-1.8 1.1-2.7-1.4-1.6-1.6-3.6-.9-6.2.7-2.7 5.7-19.4-.7-31 .7-1.6 2.1-4.8 2.5-11.6 1.1-12.8-6.6-21-17.1-21-6.6 0-16.2-.5-20.1-2.3-8.5-4-10.6-14.3-6.9-17.2l.2-.2c1.1-.7 3-.2 5 .5 1.6.5 3.2.9 5 .7h.7c10.3-1.4 9.6-10.3 9.6-10.3 0 1.6-2.3 1.8-2.7 2.1-3.2.5-7.5-.7-9.8-.7-.9 0-2.7.5-3.2.7-5.7 2.1-12.5 7.3-10.5 16.2 2.1 9.3 8.4 11.8 15 12.8.5 0 .9.2 1.1.2 3.9.7 8 .9 11.8 1.6 17.1 2.5 13.9 18.2 12.3 23-3.6-1.1-11.7 5.3-15.3 5.5-2.7.1-6.7-8.9-7.4-10.6 9.1 3.2 17.3-.9 18.2-1.8 1.4-.9.2-1.1-1.8-1.1-8.2.2-10.5-5-16.9-8.7-2.2-1.2-5.6-2.5-9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-1.8 3.6-2.7 4.8-.7.7-1.4 1.4-1.8 2.1-.7 1.1-.5 2.5.2 3 0 0-4.1 2.5-6.4 8.9 4.8.5 10.3-.2 11.8-1.4 0 0 .7-3-.5-5.5.9 0 1.4-.2 1.8-.5 1.8-1.4 1.1-3.9 1.1-3.9.9-1.6 3.6-7.3 5.2-8.2 1.6-.9 2.7-1.6 3.4-3.6.5-1.6-.2-3-1.1-3.9l-.2-.2c-2.5-2.7.9-5.9 1.4-9.3 1.1 1.6.2 3.6 0 5.2-.2 1.8-.5 4.1 1.6 5.2.7.5 1.8.7 3.2.5 3.2 0 3.9-2.1 3.9-2.1-5.7-5.9-6.4-10.6-5.5-13.2 1.5-4.5 5.1-9.3 5.1-9.3-1.1 3.6-3.7 7-2.8 11.8.9 5 4.3 9.3 7.7 13 2.5 2.5 6.4 4.6 7.7 6.6C-1.8-1.5-3.7-2.7-2.8.5c.5 1.8 1.1 2.1 3 2.3 1.1 0 .9.7 2.7-.2C4.3 1.9 5 .8 5 .8s3.1-1.3 2.2-2z"/>
    </g>
    <g>
        <defs>
            <circle id="bundesliga-c" cx="32" cy="32" r="32"/>
        </defs>
        <clipPath id="bundesliga-d">
            <use xlink:href="#bundesliga-c" overflow="visible"/>
        </clipPath>
        <circle clip-path="url(#bundesliga-d)" fill="#D12028" cx="32" cy="32" r="32"/>
        <defs>
            <circle id="bundesliga-e" cx="32" cy="32" r="32"/>
        </defs>
        <clipPath id="bundesliga-f">
            <use xlink:href="#bundesliga-e" overflow="visible"/>
        </clipPath>
        <path clip-path="url(#bundesliga-f)" fill="#EEC258" d="M0 43.2h64V64H0z"/>
        <g>
            <defs>
                <circle id="bundesliga-g" cx="32" cy="32" r="32"/>
            </defs>
            <clipPath id="bundesliga-h">
                <use xlink:href="#bundesliga-g" overflow="visible"/>
            </clipPath>
            <path clip-path="url(#bundesliga-h)" fill="#292929" d="M0 0h64v20.8H0z"/>
        </g>
        <g>
            <defs>
                <circle id="bundesliga-i" cx="32" cy="32" r="32"/>
            </defs>
            <clipPath id="bundesliga-j">
                <use xlink:href="#bundesliga-i" overflow="visible"/>
            </clipPath>
            <path opacity=".07" clip-path="url(#bundesliga-j)" fill="#292929" d="M17.6 64H0V0h40"/>
        </g>
    </g>
</svg>
</div>

</body>
</html>

最佳答案

好的,自发布这个问题以来,我一直在与我公司的其他一些人保持联系,我们一起解决了这个问题。

首先是问题: 当你使用内联 svg 并且一个特定的 svg 被包含两次并且这个特定的 svg 使用 id 引用(例如到一个定义的 clipPath)并且第一个 svg(至少在 firefox 上)在一个带有 display:none 的 html 元素中..然后,该 svg 第二次出现时的 id 引用引用了第一个(隐藏的)svg 中的 clipPath,这导致 clipPath 未显示,因此圆形图标变为方形。

现在我的临时解决方案: 许多导致此问题的图标都多次定义了完全相同的 clipPath。我在页面主体的顶部包含了一个“虚拟”svg,定义了这个确切的 clipPath:

<svg width="0" height="0" style="position: absolute;"><clipPath id="circle-32"><circle cx="32" cy="32" r="32"/></clipPath></svg>

然后我从所有有问题的 svg 中删除了所有 clipPath 定义,并将它们替换为对 #circle-32 clipPath 的引用。

这导致 svg 再次工作 :)

长期解决方案: 我们决定我们的设计师需要花更多的时间来创建/导出不使用这么多 clipPath 引用(或其他相关引用)的正确 svg,将来我们将用正确导出的重绘图像替换我们的整个 svg 集供网络使用,并尽量减少这些结构。

有人建议我转发这篇博文,其中包含有关如何为我们的设计师创建合适的 svg 的提示: https://sarasoueidan.com/blog/svg-tips-for-designers/

关于css - 显示无后,内联 SVG 在 Firefox 中无法正确呈现,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41137750/

相关文章:

javascript - 将内容从一个 div 滑入另一个

internet-explorer - Windows Azure 上使用回发的 Chrome 与 IE 等

javascript - Firefox Addon-SDK 面板滚动条

javascript - Javascript 中的大型 blob 文件

c++ - Windows cpp 目标在较高分辨率下变慢

css - Evince 中的 GTK+ 插入符号颜色 CSS 属性

javascript - HTML,CSS。 mouseout 时将 div 背景颜色恢复为原始颜色(使用 bg img)

html - 独立表th row

opengl - 具有两个不同投影矩阵的深度缓冲区

css - Mozilla 和 webkit 之间的字体渲染