I have an icons.svg file with this source:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<font id="MyIcon" horiz-adv-x="1024">
<font-face units-per-em="1024" ascent="960" descent="-64" />
<missing-glyph horiz-adv-x="1024" />
<glyph unicode=" " horiz-adv-x="512" d="" />
<glyph unicode="" glyph-name="arrow-left" horiz-adv-x="1308" d="..." />
<glyph unicode="" glyph-name="arrow-up" horiz-adv-x="1308" d="..." />
<glyph unicode="" glyph-name="arrow-down" horiz-adv-x="860" d="..." />
</font></defs></svg>
Is there any way to refer this glyph
tags from HTML
? I think something like this:
<svg role="img" title="arrow-up"><use xlink:href="icons.svg#arrow-up"></use></svg>
Is there any way to use SVG
tags in my HTML code?
There is no browser that implements SVG
<font>
or<glyph>
. The best you can do is convert the glyphs to<symbol>
s containing a<path>
, like in a SVG sprite sheet. (This conversion could be even automated with XSLT.) If you set aviewBox
for the symbol with the width matchinghoriz-adv-x
and the height the font'sascent
, you can position the icon use reliably inline:The downside is that you don't get the automatic advance that glyphs would give you. The best you can do is to set a constant height and a
viewBox
for each referencing<svg>
that matches that of the symbol you are using.If you don't use the icons in a inline context where the advance is important, you could maybe set all symbol viewBox widths to a unified value (i. e.
viewBox="0 0 1024 960"
) and then leave off the viewBox from the icon svg, seting instead a constant width/height ratio (i.e.width:1.067em; height:1em
). Some icons will then overflow that box in the horizontal direction.