您需要的命名空間外部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衝突)。一般來說,這應該是可能的。 –