最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you import svg file with svg tag? - Stack Overflow

programmeradmin0浏览0评论

I would like to know how to use svg file and use it as tag. For example, I have a svg file like

<svg version="1.1" xmlns="" width="512" height="512" viewBox="0 0 512 512">
<title></title>
<g id="ioon-ignore">
</g>
<path d="M144 288h-32v224h32c8.8 0 16-7.2 16-16v-192c0-8.8-7.2-16-16-16z"></path>
</svg>

I am trying to require it with javascript (node, electron) and render in html tag.

// For examle,
<svg src="./mySvg.svg" />

I don't want to use <img> tag or etc.

I would like to know how to use svg file and use it as tag. For example, I have a svg file like

<svg version="1.1" xmlns="http://www.w3/2000/svg" width="512" height="512" viewBox="0 0 512 512">
<title></title>
<g id="ioon-ignore">
</g>
<path d="M144 288h-32v224h32c8.8 0 16-7.2 16-16v-192c0-8.8-7.2-16-16-16z"></path>
</svg>

I am trying to require it with javascript (node, electron) and render in html tag.

// For examle,
<svg src="./mySvg.svg" />

I don't want to use <img> tag or etc.

Share Improve this question edited Aug 6, 2017 at 20:58 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Aug 6, 2017 at 2:50 ryanryan 5433 gold badges8 silver badges17 bronze badges 2
  • maybe put a rect tag inside svg tag and give it a background img using css? – Tianhao Zhou Commented Aug 6, 2017 at 3:22
  • What's the reason for not wanting to use <img>? That's the proper way of doing it. While Paul's answer below works, <img> would be the proper way of doing it. – Thomas W Commented Aug 6, 2017 at 5:38
Add a ment  | 

1 Answer 1

Reset to default 4

If you are excluding using <img> (HTML) or <image> (SVG), and presumably <object> as well, then the only other option is <use>:

<svg width="512" height="512" viewBox="0 0 512 512">
  <use xlink:href="#ioon-ignore"/>
</svg>
发布评论

评论列表(0)

  1. 暂无评论