【发布时间】:2022-01-02 10:04:55
【问题描述】:
我的 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 文件中。
如何将渐变应用到此图标?
谢谢!
【问题讨论】:
标签: css svg linear-gradients