How to use external SVG in HTML?

HtmlCssSvg

Html Problem Overview


I try to make an HTML that references to an SVG file, that SVG file is interactive (CSS hover):

  1. If I use <img src="algerie.svg"> I loose the interactivity.

SVG displayed as image embedded in an HTML page

  1. If I open this image in a new tab using dev tool, it becomes interactive.

SVG opened in the browser, showing interactive highlights

  1. If I use:

    <svg viewBox="0 0 512 512">
      <use xlink:href="algerie.svg"></use>
    </svg>
    

Then nothing is shown, and worse, Chrome or Firefox do not detect the file in the network dev tool.

Html Solutions


Solution 1 - Html

You should embed the image by using <object> tag:

<object data="algerie.svg" type="image/svg+xml"></object>

Refer to this question for the details: https://stackoverflow.com/questions/4476526/do-i-use-img-object-or-embed-for-svg-files

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbdelouahabView Question on Stackoverflow
Solution 1 - HtmlNikitaBaksalyarView Answer on Stackoverflow