Search code examples
svgfavicon

Can pure SVG document specify a favicon.ico?


Can a pure SVG document arriving at a browser, support a favicon.ico specification? My case is machine-generated pure SVG, no <html> or <head> tags available.


Solution

  • Yes, since SVG is XML, you can add the (x)html link element anywhere in the SVG:

    <link xmlns="http://www.w3.org/1999/xhtml" rel="shortcut icon" href="favicon.ico" />
    

    There's absolutely no need to wrap this in foreignObject.
    I recommend putting the link element as a child of a defs element though, just to let the SVG engines know that it can be skipped for the actual SVG rendering.

    Do note that this isn't "pure" SVG; it's an xhtml/svg hybrid — but it should work in all the browsers that support SVG.