您需要的命名空间外部SVG文件......我已经添加填充来呈现圆形,否则这将是透明的:
<svg xmlns="http://www.w3.org/2000/svg" >
<symbol id="shape" width="200" height="200" viewbox="0 0 200 200">
<circle cx="100" cy="100" r="100" fill="currentColor" />
</symbol>
<text y="20">Symbol above will not render unless referenced by use element</text>
</svg>
然后当你引用它,你需要使用正确的命名空间的XLink:
svg.defs-only {
display:block; position: absolute;
height:0; width:0; margin: 0; padding: 0;
border: none; overflow: hidden;
}
svg {
color: orange;
stroke: red;
}
.purple {
color: purple;
stroke: black;
}
<svg class="defs-only" xmlns="http://www.w3.org/2000/svg" >
<symbol id="shape" width="50" height="50" viewbox="0 0 50 50">
<circle cx="25" cy="25" r="20" fill="currentColor" stroke="inherit" />
</symbol>
</svg>
<svg xmlns:xlink="http://www.w3.org/1999/xlink">
<use xlink:href="#shape" x="10" y="10" />
<use xlink:href="#shape" x="80" y="10" class="purple" />
</svg>
如果您正在引用外部文件,则需要将文件名放在#
image.svg#shape
确保您获得正确的路径。
请注意,并非所有浏览器都支持片段标识符 - 特别是IE和Edge--您需要为这些浏览器使用像svg4everybody一样的javascript填充。
解决方法 - 使用SVG内联只
堆栈片段彼此隔离(以便在一个文档中的ID不会与其他文档的ID冲突)。一般来说,这应该是可能的。 –