SVG使用标签时完全变黑且缺少一部分。

huangapple go评论82阅读模式
英文:

SVG using <use> tag appears completely black and missing a part of it

问题

I'm trying to add an SVG to an HTML page with the <use> tag as follows:

<div id="ampoule_container">
    <svg>
        <use href="resources/assets/Ampoule.svg#ampoule"/>
    </svg>
</div>

The problem is that it appears completely black, and a part of it is missing, as shown in this screenshot.

It should look like this: SVG使用<use>标签时完全变黑且缺少一部分。

If I type its path in a browser, it shows up correctly. After checking, no CSS is influencing its color.

My guess is that the <use> tag doesn't support colors, but only shapes. What can I do?

Here is the SVG code made with Adobe Illustrator:

<?xml version="1.0" encoding="UTF-8"?>
<svg id="ampoule" data-name="ampoule" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 180 500">
  <defs>
    <style>...</style>
    <clippath id="clippath-4">...</clippath>
    <linearGradient>...</linearGradient>
    <linearGradient>...</linearGradient>
  </defs>
  <g class="cls-10">
    <g id="...">...</g>
    <g id="...">...</g>
    <g id="...">...</g>
    <g id="...">...</g>
  </g>
</svg>
英文:

I'm trying to add a SVG to an HTML page with the &lt;use&gt; tag as following:

&lt;div id=&quot;ampoule_container&quot;&gt;
    &lt;svg&gt;
        &lt;use href=&quot;resources/assets/Ampoule.svg#ampoule&quot;/&gt;
    &lt;/svg&gt;
&lt;/div&gt;

The problem is that it appears completely black, and a part of it is missing like so:

SVG使用<use>标签时完全变黑且缺少一部分。

It should look like this:

SVG使用<use>标签时完全变黑且缺少一部分。

If I type its path in a navigator it shows up correctly. After checking, no CSS is influencing its color.

My guess is that &lt;use&gt; tag doesn't support colors, but only shapes, what can I do ?

Here is the SVG code made with Adobe Illustrator:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;svg id=&quot;ampoule&quot; data-name=&quot;ampoule&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; viewBox=&quot;0 0 180 500&quot;&gt;
  &lt;defs&gt;
    &lt;style...&gt;
    &lt;clippath id=&quot;clippath-4&quot;&gt;...&lt;/clippath&gt;
    &lt;linearGradient.../&gt;
    &lt;linearGradient.../&gt;
  &lt;/defs&gt;
  &lt;g class=&quot;cls-10&quot;&gt;
    &lt;g id=...&gt;...&lt;/g&gt;
    &lt;g id=...&gt;...&lt;/g&gt;
    &lt;g id=...&gt;...&lt;/g&gt;
    &lt;g id=...&gt;...&lt;/g&gt;
  &lt;/g&gt;
&lt;/svg&gt;

答案1

得分: 0

以下是使用<use>插入的三个SVG,并引用了一个ID。所有SVG都显示一个带有模糊滤镜的橙色圆圈。

我不清楚对此的一般支持情况,但根据我的观察(使用Firefox和Chrome),只有将fill属性定义为属性(fill="orange")或内联样式(style="fill:orange;")的SVG才有效。

在所有SVG中,滤镜都在<defs>中定义。因此,我认为问题不是使用滤镜、蒙版或剪辑路径,而只是在<style>中定义CSS样式时出现问题。

刚刚注意到您的SVG中还有渐变。因此,这是一个在<defs>中定义的带有渐变的圆圈。在Firefox中正常工作,但在Chrome或Opera中不正常工作(但总体来说,这些示例在Opera中都不起作用,但我猜这是与数据URL有关的问题)。

希望这有助于您的理解。

英文:

Here are three SVGs inserted using &lt;use&gt; and with reference to an ID. All SVGs display a orange circle with a blur filter.

I don't know about the general support for this, but as I see it (using Firefox and Chrome), only the SVGs with the fill defined as an attribute (fill=&quot;orange&quot;) or inline style (style=&quot;fill:orange;&quot;) on the circle element works.

In all SVGs the filter is defined in &lt;defs&gt;. So, I don't think that the problem is the use of filters, masks or clip paths, but only an issue when you define CSS styles in &lt;style&gt;.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  display: flex;
  gap: 10px;
}

svg {
  display: block;
  width: 200px;
  height: 200px;
  border: solid thin black;
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;p&gt;Circle with fill attribute:&lt;/p&gt;
  &lt;svg&gt;
    &lt;use href=&quot;data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyBpZD0ic3ZnMDEiIHZpZXdCb3g9IjAgMCAxMDAgMTAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxkZWZzPgogICAgPGZpbHRlciBpZD0iYmx1ciI+CiAgICAgIDxmZUdhdXNzaWFuQmx1ciBzdGREZXZpYXRpb249IjIiIC8+CiAgICA8L2ZpbHRlcj4KICA8L2RlZnM+CiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iMzAiIGZpbGw9Im9yYW5nZSIgZmlsdGVyPSJ1cmwoJyNibHVyJykiLz4KPC9zdmc+CgoK#svg01&quot;/&gt;
  &lt;/svg&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;p&gt;Circle styled with a CSS property in a &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; element:&lt;/p&gt;
  &lt;svg&gt;
    &lt;use href=&quot;data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyBpZD0ic3ZnMDEiIHZpZXdCb3g9IjAgMCAxMDAgMTAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxkZWZzPgogICAgPHN0eWxlIHR5cGU9InRleHQvY3NzIj4KICAgICBjaXJjbGUgeyBmaWxsOiBvcmFuZ2U7IH0KICAgIDwvc3R5bGU+CiAgICA8ZmlsdGVyIGlkPSJibHVyIj4KICAgICAgPGZlR2F1c3NpYW5CbHVyIHN0ZERldmlhdGlvbj0iMiIgLz4KICAgIDwvZmlsdGVyPgogIDwvZGVmcz4KICA8Y2lyY2xlIGN4PSI1MCIgY3k9IjUwIiByPSIzMCIgZmlsdGVyPSJ1cmwoJyNibHVyJykiLz4KPC9zdmc+CgoK#svg01&quot;/&gt;
  &lt;/svg&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;p&gt;Circle styles with CSS property in the style attribute:&lt;/p&gt;
  &lt;svg&gt;
    &lt;use href=&quot;data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyBpZD0ic3ZnMDEiIHZpZXdCb3g9IjAgMCAxMDAgMTAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxkZWZzPgogICAgPGZpbHRlciBpZD0iYmx1ciI+CiAgICAgIDxmZUdhdXNzaWFuQmx1ciBzdGREZXZpYXRpb249IjIiIC8+CiAgICA8L2ZpbHRlcj4KICA8L2RlZnM+CiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iMzAiIHN0eWxlPSJmaWxsOm9yYW5nZTsiIGZpbHRlcj0idXJsKCcjYmx1cicpIi8+Cjwvc3ZnPgoKCg==#svg01&quot;/&gt;
  &lt;/svg&gt;
&lt;/div&gt;

<!-- end snippet -->

Just noticed that you also have gradients in your SVG. So, here is a circle with a gradient defined in &lt;defs&gt;. That works fine in Firefox, but not in Chrome or Opera (but in general the examples does not work in Opera, but I guess that is an issue with data URLs).

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  display: flex;
  gap: 10px;
}

svg {
  display: block;
  width: 200px;
  height: 200px;
  border: solid thin black;
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;p&gt;Circle with fill attribute and gradient:&lt;/p&gt;
  &lt;svg&gt;
    &lt;use href=&quot;data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyBpZD0ic3ZnMDEiIHZpZXdCb3g9IjAgMCAxMDAgMTAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxkZWZzPgogICAgPGZpbHRlciBpZD0iYmx1ciI+CiAgICAgIDxmZUdhdXNzaWFuQmx1ciBzdGREZXZpYXRpb249IjIiIC8+CiAgICA8L2ZpbHRlcj4KICAgIDxsaW5lYXJHcmFkaWVudCBpZD0iZ3JhZGllbnQiIGdyYWRpZW50VHJhbnNmb3JtPSJyb3RhdGUoOTApIj4KICAgICAgPHN0b3Agb2Zmc2V0PSI1JSIgc3RvcC1jb2xvcj0ib3JhbmdlIiAvPgogICAgICA8c3RvcCBvZmZzZXQ9Ijk1JSIgc3RvcC1jb2xvcj0ibWFyb29uIiAvPgogICAgPC9saW5lYXJHcmFkaWVudD4KICA8L2RlZnM+CiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iMzAiIGZpbGw9InVybCgjZ3JhZGllbnQpIiBmaWx0ZXI9InVybCgnI2JsdXInKSIvPgo8L3N2Zz4KCgo=#svg01&quot;/&gt;
  &lt;/svg&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 10:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定