Fix duplicate inline SVG IDs

Give each instance its own gradient ID so nearby graphics keep their own colors.

Asset preparation2 min read

Two SVG files look right when opened separately. Put their markup into the same HTML page, and the blue panel turns orange. Both exports define id="paint", and both rectangles use fill="url(#paint)". The two inline SVGs share one document, so their IDs can collide. HTML ID rules · SVG references.

Reproduce the color swap

Open the working comparison. Each example contains two inline SVGs in one frame. The first gives both gradients the ID paint. A browser can use the same gradient for both panels. The second gives each gradient a unique ID. The left panel stays orange and the right panel is blue.

The browser behavior with duplicate IDs can vary. A correct preview in one arrangement is insufficient. Add both exports to the same page and reverse their order to test whether either graphic depends on its neighbor.

Rename the definition and its references together

Apply a different prefix to each instance. For the cool panel, change both the gradient definition and the fill that points to it.

svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 64">
  <defs>
    <linearGradient id="cool-paint">
      <stop stop-color="#4975ff"/>
      <stop offset="1" stop-color="#57e7ed"/>
    </linearGradient>
  </defs>
  <rect width="128" height="64" rx="10" fill="url(#cool-paint)"/>
</svg>

Check local url(#…) references in fills, strokes, masks, clip paths, filters, markers and styles. Also update matching local href references and title or description IDs referenced by accessibility attributes. A filename prefix helps when each file appears once. A component that renders the same SVG twice needs a different prefix for each rendered instance.

If you load separate SVG files through <img src="…">, their internal IDs stay in separate image documents. Inspect the embedding first. Fix the IDs when the actual template puts the shapes inline in the same document.

Image tools
All guides
Fix duplicate inline SVG IDs | Formatry Media — Formatry