SVGSketch Learn

How to Embed SVG in HTML

Four ways to embed SVG, each with different CSS, animation, and accessibility tradeoffs.

Open SVGSketch

Quick answer

For maximum control (CSS theming, animation, JavaScript) inline the SVG: paste the <svg> markup directly into your HTML. For simple static use, <img src="icon.svg" alt=""> is enough. <object> sits in the middle, and CSS background-image is for decorative backdrops.

Steps at a glance

  1. Decide by use case

    Theming/animation needs determine which embed method to use.

  2. Inline for full control

    Paste the <svg> markup directly into HTML.

  3. Use <img> for simple static

    <img src="icon.svg" alt="..."> when no theming is needed.

  4. Use <object> for self-contained interactive SVG

    <object data="icon.svg" type="image/svg+xml">

  5. CSS background for decoration

    background-image: url(icon.svg) for purely decorative backdrops.

  6. Reuse one symbol many times

    Inline a <symbol> once, then place instances with <use href="#icon-name">.

  7. Size it deliberately

    Give inline SVG a viewBox plus CSS width; an <img> uses the file's own width and height.

Step 1: Inline SVG

Copy the <svg>...</svg> markup with Edit > Copy as Code > SVG (Ctrl+Shift+C) and paste it directly into your HTML. Pros: full CSS control over individual paths, currentColor theming, JavaScript event listeners, SMIL animation, accessibility. Cons: HTML payload grows.

For the smallest paste, turn on "Optimize with SVGO" and turn off "Pretty-print output" on an SVG export item. SVGSketch also has a dedicated HTML export format that wraps the SVG in a ready-to-serve page, switched between the two modes by the "Embed SVG inline (vs data URL)" option.

Step 2: <img src="icon.svg">

Reference the SVG file with an <img> element. Pros: caches independently, easy to swap, smaller HTML. Cons: your page CSS cannot reach the SVG at all, so currentColor resolves to the SVG's own color rather than yours, and scripting is off. Pick this for static icons that do not need theming.

The rules here are spec-defined, not browser quirks: SVG 2 requires an image reference to be processed in secure animated mode, which permits declarative animation but forbids scripting, interactivity, and - the one that catches people - external references. An SVG that pulls in a webfont, a stylesheet, or another image will render without them. That is precisely why SVGSketch inlines font outlines into the file on export rather than linking to Google Fonts.

Step 3: `<object>` for self-contained SVG

Use <object data="icon.svg" type="image/svg+xml"> to embed an external SVG as its own document, which keeps scripting and its internal CSS alive. Pros: animation and JavaScript still work, and you can supply fallback content between the tags. Cons: an extra HTTP request, accessibility quirks, and a stylesheet boundary - your page CSS does not cascade into the object, so theming means either styles written inside the SVG or same-origin JavaScript reaching in through contentDocument.

<iframe> and <embed> behave similarly and are worth knowing about, but <object> is the one with a usable fallback story, so reach for it first.

Step 4: CSS background-image

Use background-image: url(icon.svg) for decorative backgrounds. Pros: clean separation of content and decoration. Cons: cannot be themed via CSS variables, no scripting, accessibility-invisible (which is right for purely decorative content).

A data URI does not solve theming: it is still a separate document, so neither your custom properties nor currentColor reach inside it. For a background that follows a CSS variable, use the SVG as a mask instead - mask-image: url(icon.svg) with background-color: var(--icon-color) tints the shape with whatever the token holds. Otherwise, inline the SVG.

Step 5: Reuse one symbol many times

When the same icon appears repeatedly, inline it once as a <symbol> inside a hidden <svg> at the top of the page, then place each instance with <use href="#icon-name">. The geometry is parsed once, every instance still inherits your CSS, and the markup at each call site is a single short element.

Note the boundary: a <use> that points at an external file is loaded in secure static mode, which strips animation and scripting. Sprite reuse is for icons within the current document, not a way to hot-link someone else's SVG.

Step 6: Size it deliberately

Sizing behaves differently per method. Inline SVG has no intrinsic size of its own once it is in your document, so give it width and height in CSS, or a viewBox plus width:100% and height:auto to make it scale with its container. An <img> uses the SVG's own width and height attributes as its intrinsic size, and falls back to 300 by 150 pixels if the file declares none - the usual cause of an icon that arrives mysteriously large.

For anything responsive, make sure the file carries a viewBox. Without one there is no aspect ratio to preserve and no way to scale the artwork to a box.

Step 7: Pick by use case

Inline SVG for icon components, charts, and anything you will style or animate. <img> for static brand assets and content images. <object> for legacy or self-contained interactive SVGs. CSS background for purely decorative artwork.

What each embed method allows

The differences are not browser quirks. SVG 2 defines the processing mode each context uses, and that mode decides what the file is permitted to do.

MethodYour CSS reaches itDeclarative animationScriptingExternal referencesIn the accessibility tree
Inline <svg>
<img src>Via alt
<object data>
CSS background
<use> to external fileInherited

FAQ

Which embed method supports CSS theming?

Only inline SVG, because only then do the paths live in your document and inherit from your cascade. <object> keeps its own document, so page CSS stops at the boundary and theming has to be written inside the SVG or injected with same-origin JavaScript. <img> and CSS backgrounds admit no styling at all; use mask-image with background-color when you need a tintable background.

Can I animate an SVG embedded as <img>?

Yes, as long as the animation is declared inside the file. SVG 2 processes an image reference in secure animated mode, which permits declarative animation - both SMIL and CSS animations and transitions written into the SVG. What it forbids is scripting, so JavaScript-driven animation needs inline or <object> embedding.

Why did my webfont or linked image not load inside an <img> SVG?

Secure animated mode forbids external references, so an SVG loaded through <img> or a CSS background cannot fetch fonts, stylesheets, or other images. Inline the dependency into the file - SVGSketch embeds font outlines on export for exactly this reason - or embed the SVG inline in the page instead.

How do I make an SVG icon match my CSS color?

Set fills to "currentColor" inside the SVG, then inline-embed it. Any color rule on the parent element will tint the icon.

Is inline SVG bad for performance?

Slightly larger HTML, but fewer HTTP requests and better cacheability for the rest of the page. For small icons, inline wins.

Do I need an alt attribute?

For <img>, yes - alt="" for decoration, real text otherwise. For inline SVG, use role="img" with aria-label when it carries meaning, and aria-hidden="true" when it is purely decorative.

Why does my inline SVG ignore its viewBox?

Almost always a capital letter. Inline SVG in an HTML file is parsed by the HTML parser, which lowercases attribute names except for a fixed list it corrects back - viewBox is on that list, but only spelled exactly that way. Hand-typed "viewbox" silently becomes an unknown attribute and the graphic loses its scaling.

Open your SVG in SVGSketch

Start with a blank canvas, import an SVG, or use the editor as a conversion and cleanup step.

Open SVGSketch