我一直在尝试将我的SVG图标外部化到一个文件中,并使用类似于
简而言之,在标记中使用
我设置了一个演示问题的plunker: http://plnkr.co/edit/feKvZ7?p=preview 简化后的标记如下:
这是在不同浏览器中呈现线性渐变符号的不同结果:
<svg><use xlink:href="file.svg#icon" /></svg>
的标记引用它们。理论上,这个方法非常好,但是不同的浏览器在渲染时会出现问题。当在文件内使用<use>
引用符号并直接打开SVG文件的URL时,所有浏览器都可以正确地渲染SVG。简而言之,在标记中使用
<svg><use/></svg>
引用符号时,是否有跨浏览器的方法来使SVG linearGradient
作为元素填充工作?我设置了一个演示问题的plunker: http://plnkr.co/edit/feKvZ7?p=preview 简化后的标记如下:
<!DOCTYPE html>
<html>
<body>
<h1>SVG sprite test</h1>
<svg width="100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<use xlink:href="icon.svg#icon" />
</svg>
</body>
</html>
而SVG文件看起来像这样:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="gradient">
<stop offset="0" stop-color="black" />
<stop offset="1" stop-color="white" />
</linearGradient>
</defs>
<symbol id="icon" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" stroke="black" fill="url(#gradient)" />
</symbol>
<use id="iconuse" xlink:href="#icon" width="100" height="100" />
</svg>
![Different results of rendering linear gradients for symbols in different browser](https://istack.dev59.com/yZSN9.webp)