如何将线性渐变应用于 <use> 标签中的 SVG <symbol>?

How to apply linear gradient to SVG <symbol> in <use> tag?

我的 HTML 文件中有这些行:

<div class="account-container">
 <svg id="icon-account" style="width: 5rem; height: 5rem;">
  <use href="/icons.svg#icon-account" />
 </svg>
</div>

icons.svg 看起来像这样:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <defs>

  <linearGradient id="gradient1" x1="21.3635" y1="1.72727" x2="21.3639" y2="41"     gradientUnits="userSpaceOnUse">
   <stop stop-color="#7DC2C9"/>
   <stop offset="1" stop-color="#446B73"/>
  </linearGradient>

  <symbol id="icon-account" viewBox="0 0 42 42" >
   <path d="M6.09091 34.3314C10.9277 29.4164 15.2241 27.245 21 27.1818C27.3352 27.4877 31.7332 29.4904 36.2727 33.9136M41 21C41 32.0457 32.0457 41 21 41C9.95431 41 1 32.0457 1 21C1 9.95431 9.95431 1 21 1C32.0457 1 41 9.95431 41 21ZM26.4545 15.1818C26.4545 18.1943 24.0125 20.6364 21 20.6364C17.9875 20.6364 15.5455 18.1943 15.5455 15.1818C15.5455 12.1694 17.9875 9.72727 21 9.72727C24.0125 9.72727 26.4545 12.1694 26.4545 15.1818Z" fill="none"/>
  </symbol>

 </defs>
</svg>

我想给图标应用#gradient1。在 SVG 文件的路径上或 CSS 中使用 stroke=url(#gradient1) 设置笔划不起作用,图标根本不呈现。

在 HTML 文档中包含以下 SVG defs 本身允许引用渐变然后它起作用:

<svg
  version="1.1"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
>
  <defs>
    <linearGradient
      id="gradient1"
      x1="21.3635"
      y1="1.72727"
      x2="21.3639"
      y2="41"
      gradientUnits="userSpaceOnUse"
    >
      <stop stop-color="#7DC2C9" />
      <stop offset="1" stop-color="#446B73" />
    </linearGradient>
  </defs>
</svg>

但是,我想避免用 SVG 定义污染 HTML,并将所有 SVG 相关代码保存在单独的 icons.svg 文件中。

如何将渐变应用于此图标?

谢谢!

似乎是一个 Chrome 错误,可能是臭名昭著的 Issue 109212: SVG (filter | fill […]) from external files not applied from 2012; found in related question: Gradient in defs not showing up in SVG sprite in Chrome.

<svg style="width: 5rem; height: 5rem;">
 <use href='data:image/svg+xml,<svg version="1.1" 
  xmlns="http://www.w3.org/2000/svg">
  <defs>
   <linearGradient id="gr">
    <stop offset=".2" stop-color="red" />
    <stop offset=".8" stop-color="blue" />
   </linearGradient>
   <symbol id="icon" viewBox="0 0 8 8" >
    <path d="M0 0 L 8,0 8,8 0,8 Z M 2,2 L 6,2 6,6 2,6 Z" 
     fill="url(%23gr)"
     stroke="gold" />
   </symbol>
  </defs>
 </svg>#icon' />
</svg>

本示例代码使用dataURI模拟外部资源。在 Firefox 中,它会正确呈现金色描边和红蓝渐变填充,但在 Chrome 中只有描边可见。


对于奇偶校验,具有 defs 的相同 SVG 直接放置在 HTML 中,适用于两种浏览器:

<svg style="width: 5rem; height: 5rem;">
 <use href='#icon' />
</svg>

<svg version="1.1" 
  xmlns="http://www.w3.org/2000/svg">
  <defs>
   <linearGradient id="gr">
    <stop offset=".2" stop-color="red" />
    <stop offset=".8" stop-color="blue" />
   </linearGradient>
   <symbol id="icon" viewBox="0 0 8 8" >
    <path d="M0 0 L 8,0 8,8 0,8 Z M 2,2 L 6,2 6,6 2,6 Z" 
     fill="url(#gr)"
     stroke="gold" />
   </symbol>
  </defs>
 </svg>

并且独立的 SVG 加载到图像中,证明在相同的真实 SVG 中的引用(不是 SVG-in-HTML)在两种浏览器中都有效。

<img src="data:image/svg+xml;charset=utf-8,
<svg xmlns='http://www.w3.org/2000/svg' version='1.1'>
 <defs>
  <linearGradient id='gr'>
   <stop offset='.2' stop-color='red'/>
   <stop offset='.8' stop-color='blue'/>
  </linearGradient>
  <symbol id='icon' viewBox='0 0 8 8'>
   <path d='M0 0 L 8,0 8,8 0,8 Z M 2,2 L 6,2 6,6 2,6 Z'
    fill='url(%23gr)'
    stroke='gold'/>
  </symbol>
 </defs>
 <use href='%23icon'/>
</svg>"></img>