Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple svg with same IDs

Can i put multiple svgs in a html page and use the same IDs in all of them?

<div>
  <svg height="0" width="0">
    <clipPath id="svgPath"> ........        
  </svg>
  <svg height="0" width="0">
    <clipPath id="svgPath"> ........        
  </svg>
  <svg height="0" width="0">
    <clipPath id="svgPath"> ........        
  </svg>
</div>
like image 922
Elfy Avatar asked May 03 '16 09:05

Elfy


People also ask

Do svg IDS need to be unique?

An id on an <svg> tag assigns an identifier to the svg element. The identifier must be unique across the page.

Can I have multiple svg images in a single file?

Yes. What is the issue your having? @JeremyA. West Added XML version, encoding, and SVG Doctype for the external file.

How do I use svg in Vue?

At the end of the day, Vue uses dynamic HTML templating, meaning the most straightforward way to use SVG with Vue is exactly how you might do it with standard HTML: By placing the <svg> inline. By using an <img> tag that links to an external SVG file with the src attribute.

Are svg vector files?

What is an SVG file? Scalable Vector Graphics (SVG) is a web-friendly vector file format. As opposed to pixel-based raster files like JPEGs, vector files store images via mathematical formulas based on points and lines on a grid.


2 Answers

Since the specs define the id attribute as unique per document, you should refactor the IDs or use an alternative, e.g. embedding via img or object tag.

<img src="my.svg" height="100" alt="alternative Text">

<object type="image/svg+xml" data="my.svg" width="100" height="100"></object>
like image 111
Quasimodo's clone Avatar answered Sep 26 '22 04:09

Quasimodo's clone


If you need the SVGs to be inline you should consider using an SVG injector, which changes the IDs to unique strings when inserting the SVGs into the HTML document

SVGInject adds a random string to the end of the ID for elements defined in the <defs> section. For example, svgPath may become something like svgPath-Dcs83KsE. Other SVG injectors add a running number to the ID.

Both methods were developed so the same SVG can be injected multiple times into a HTML document without creating an ID conflict. But of course it also works for different SVGs with conflicting IDs.

like image 26
Waruyama Avatar answered Sep 24 '22 04:09

Waruyama