Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display external SVG with <use> tag and href or xlink:href attribute?

Tags:

html

css

image

svg

The SVG is not displayed correctly when it is loaded from <use> element. I've added an <img> element after it to illustrate the expected result:

<svg>
  <use xlink:href="file.svg" href="file.svg"></use>
</svg>
<img src="file.svg" />

Current output:

current output

Working fiddle: demo

Thanks to anyone who can point out my mistake.

like image 347
seb Avatar asked Feb 01 '26 22:02

seb


1 Answers

  1. You have this error: Unsafe attempt to load URL https:.....svg from frame with URL https://...... Domains, protocols and ports must match

  2. You need to use the id of the svg object you want to use

  <svg viewBox="0 0 14 16" width="50">
    <use xlink:href="sof.svg#id" fill="red"/>
  </svg>

Please take a look at this example: https://codepen.io/enxaneta/project/editor/91143c063e6b9abf1b5c43a14a9937ea

like image 188
enxaneta Avatar answered Feb 03 '26 13:02

enxaneta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!