Home › Guides › SVG animation not working in an img tag

SVG animation not working in an img tag

Updated 4 December 2026 · 9 minute read · The embedding rule behind one of the checker's most frequently triggered warnings

You open the SVG directly and it moves. You drop it into the live preview and it still moves. Then you reference it with <img src="logo.svg"> on a real page and it freezes. Nothing in the file changed — its container did. An <img> tag does not render SVG as a mini web page; it renders it inside a locked-down image context. Understanding exactly what that context permits is the whole fix.

1. Why img is a locked context

Images are untrusted by design. A browser has no way to know where an image file came from, so when it loads an SVG through <img> it treats the document as passive media: it paints the vector graphics and runs declarative animation, but disables every capability that could execute logic, reach the parent page, or make network requests. The SVG becomes as inert as a PNG — it simply happens to be described in XML. This is not a bug or a missing feature; it is the security boundary that lets pages embed third-party images safely.

A direct consequence is that the same URL opened in a browser tab is a first-class document, while the same URL loaded through <img> is a restricted one. "It works when I open the file" therefore proves nothing about how it will behave as an image, which is exactly why the SVG Animation Checker reports script usage as a warning even when the preview plays perfectly.

2. What keeps animating inside img

Both declarative animation engines survive the image context. SMIL elements — <animate>, <animateTransform>, <animateMotion>, <set> — run on the browser's native SMIL engine exactly as they do standalone, including loops, keyTimes timing and event-independent playback. CSS animations defined inside the SVG's own <style> block run too, because that stylesheet belongs to the SVG document itself rather than to the parent page. Hover-driven CSS inside an image cannot react to the mouse — there is no interactive surface — but load-time and infinite animations play normally.

This is why the animated check mark gallery deliberately contains nothing but SMIL and CSS: every example loops correctly whether you open it directly, embed it with <img>, or use it as a CSS background.

3. What the image context blocks

The blocked list is short and absolute. First, all scripting is disabled: a <script> block never executes, so requestAnimationFrame loops, setInterval timers and animation libraries such as GSAP or anime.js simply never start — the element freezes on its initial frame. Second, inline event handlers are dead: onclick, onload and friends are script entry points, and the checker counts every on* attribute as a warning for precisely this reason. Third, external references fail: an SVG that pulls fonts, images or other assets from remote URLs cannot fetch them in the image context, so those pieces render as gaps or defaults; the checker flags every remote href so the problem is visible before deployment. Finally, there is no interactivity and no parent communication: the image cannot be driven by page JavaScript, cannot be styled by page CSS, and cannot bubble events outward.

4. The CSP twist on inline SVG

Developers often reach for inline SVG — pasting the markup directly into the page — specifically because inline SVG runs scripts and shares the page's DOM. But inline placement introduces a different failure mode: the page's Content-Security-Policy now applies to the SVG's contents. A policy with style-src 'self' and no 'unsafe-inline' directive strips the inline <style> block, which removes the @keyframes your CSS animation depends on, leaving a static graphic. Similarly, script-src restrictions can remove the very JavaScript you inlined the SVG to enable.

Notice the inversion: inside <img>, the SVG's own internal styles always apply because it is a separate document, but scripts are categorically banned; inline in the page, scripts and styles are possible but both become subject to the parent CSP. When an SVG animates on a staging page without a CSP and freezes in production where the security headers were tightened, this is the cause.

5. All four embedding methods compared

<img> is the simplest and safest: one request, perfect caching, no collision with page styles, SMIL and internal CSS run, everything interactive is blocked. CSS background-image behaves like <img> for animation purposes — declarative motion plays, scripts do not — with the extra caveat that background images are skipped by some assistive technologies and cannot carry meaningful alt text. <object> and <iframe> load the SVG as a real document, so scripts run and internal interactivity works, subject to the SVG document's own policy; the cost is an extra request, a separate DOM boundary and more layout overhead. Inline SVG gives maximum capability: page CSS can restyle it, page JavaScript can drive it, fragments can be reused with <use>, and there is no additional network round trip — but the markup inflates your HTML, CSP applies, and a reused icon duplicated ten times ships ten times unless you externalize with use references.

6. Converting a scripted animation to declarative

When an <img> embedding is non-negotiable — an icon system, an email-compatible asset, a user-upload field — the fix is to rebuild the motion without JavaScript. A requestAnimationFrame loop that updates cx/cy along a curve becomes an <animateMotion> with an <mpath> referencing the track; the homepage's path-motion sample shows the entire pattern in a dozen lines. A timer that toggles a class for a spinner becomes a CSS @keyframes rule with animation: spin 1.6s linear infinite, remembering transform-box: fill-box so rotation centers correctly. A click handler that calls beginElement() becomes SMIL's declarative trigger begin="button.click", which works in standalone and <object> contexts — and if you must stay inside <img>, convert the interaction to a load-time or looping animation instead, since images have no input events at all.

After the conversion, run the file through the checker before re-embedding: a clean report with SMIL/CSS entries and zero script warnings is proof the animation no longer depends on anything the image context removes.

7. Decision table and checklist

Pre-embedding checklist.
  1. The checker reports no <script> blocks and no on* handlers for anything destined for <img>.
  2. Every animation is SMIL or internal CSS with an explicit duration and a resolved target.
  3. No remote href references — fonts and images are embedded or inlined.
  4. For inline SVG, the production CSP allows the inline styles or scripts the motion requires.
  5. The file has been tested through the actual embedding tag, not only opened standalone.

If the diagnostic list is confusing, the companion guide on why SVG animations fail explains each rule in plain English, and SMIL vs CSS animation helps you pick the declarative engine that survives your target embedding method.