简体   繁体   English

Polymer SVG作为CSS背景图像不起作用

[英]Polymer SVG as CSS background-image not working

I have a polymer web component that contains an element. 我有一个包含元素的聚合物纤维网组件。 This element has a SVG background-image attached to it via CSS (.example:before).The issue is that the SVG is not displaying. 该元素通过CSS(.example:before)附加了SVG背景图像。问题是SVG没有显示。 It works on a standard HTML page but not when inside a polymer element. 它可以在标准HTML页面上运行,但不能在聚合物元素内运行。 I would normally avoid an SVG embedded in the CSS. 我通常会避免在CSS中嵌入SVG。 However, we are using VideoJS (this is not optional) and styling controls (adding icons) that are created by the library with javascript. 但是,我们使用的是VideoJS(这不是可选的)和由库使用javascript创建的样式控件(添加图标)。

Is there an issue with SVG's in CSS with Polymer/Shadow Dom? 带有Polymer / Shadow Dom的CSS中的SVG是否存在问题?

Example below: 下面的例子:

 <polymer-element name="example-element">
    <template>

        <style>
            .example :before {
                background-image: url('data:image/svg+xml;utf8,<svg version="1.1" fill="#ffffff" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="34.004px" height="36.871px" viewBox="0 0 34.004 36.871" enable-background="new 0 0 34.004 36.871" xml:space="preserve"><path d="M5.198,36.871c-0.415,0-1.825-0.033-2.566-0.451l-0.205-0.108C1.579,35.867,0,35.039,0,33.371V3.61 c0-0.888,1.007-2.368,1.87-2.903c0.868-0.538,2.562-1.203,4.619-0.148l25.614,14.258c1.149,0.641,1.877,1.787,1.899,2.99 c0.023,1.274-0.582,2.416-1.619,3.051l-25.389,15.5C6.433,36.699,5.83,36.871,5.198,36.871"/></svg>');
                background-repeat: no-repeat;
                background-position: center;
                background-size: 40%;
                content: "";
                position: absolute;
                left: 0;
                width: 100%;
                height: 100%;
            }
        </style>

        <div class="example"></div>
    </template>
</polymer-element>

Any solutions, workarounds or alternative methods would be appreciated. 任何解决方案,解决方法或替代方法将不胜感激。

Thanks, Ewan 谢谢,伊万

I would like to add a note that the SVG/CSS seems to be altered by the browser. 我想补充说明一下,SVG / CSS似乎已被浏览器更改。

From (in the CSS): 从(在CSS中):

background-image: url('data:image/svg+xml;utf8,<svg version="1.1" fill="#000000" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="34.004px" height="36.871px" viewBox="0 0 34.004 36.871" enable-background="new 0 0 34.004 36.871" xml:space="preserve"><path d="M5.198,36.871c-0.415,0-1.825-0.033-2.566-0.451l-0.205-0.108C1.579,35.867,0,35.039,0,33.371V3.61 c0-0.888,1.007-2.368,1.87-2.903c0.868-0.538,2.562-1.203,4.619-0.148l25.614,14.258c1.149,0.641,1.877,1.787,1.899,2.99 c0.023,1.274-0.582,2.416-1.619,3.051l-25.389,15.5C6.433,36.699,5.83,36.871,5.198,36.871"/></svg>');

To (output by the browser): 到(浏览器输出):

background-image: url('data:image/svg+xml;utf8,<svg version=1.1 fill=#000000 id=Layer_1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink x=0px y=0px width=34.004px height=36.871px viewBox=0 0 34.004 36.871 enable-background=new 0 0 34.004 36.871 xml:space=preserve><path d=M5.198,36.871c-0.415,0-1.825-0.033-2.566-0.451l-0.205-0.108C1.579,35.867,0,35.039,0,33.371V3.61 c0-0.888,1.007-2.368,1.87-2.903c0.868-0.538,2.562-1.203,4.619-0.148l25.614,14.258c1.149,0.641,1.877,1.787,1.899,2.99 c0.023,1.274-0.582,2.416-1.619,3.051l-25.389,15.5C6.433,36.699,5.83,36.871,5.198,36.871/></svg>');

The URL is invalid as a # character indicates the start of a fragment identifier. 该URL无效,因为#字符表示片段标识符的开头。 You need to encode # characters as %23. 您需要将#个字符编码为%23。

You may find simply base64 encoding the entire URL is simpler and easier. 您可能会发现,简单地对整个URL进行base64编码更加简单。

I wrestled with this problem for a long time and the problem ended up being that I needed to URL encode my quotes... 我为这个问题苦苦挣扎了很长时间,最终问题是我需要对引号进行URL编码...

" -> %22 “->%22

' -> %27 '->%27

Notice how in your "To (output by the browser):" all of the quotes have been removed? 请注意,如何在“收件人(浏览器输出):”中删除所有引号? For me it wouldn't work without the quotes, and then when I encoded them it magically started working again. 对我来说,如果没有引号,它将无法正常工作,然后在对它们进行编码时,它神奇地又开始工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM