Toolkit
All tools
Inline SVG encoding · Free

SVG to Data URI Converter

Encode SVG markup as an inline data URI for a stylesheet or an image tag. Both encodings are built live, weighed against each other in bytes, and previewed the way a browser will actually load them.

Encoded in your browser · No upload, no account

Source

SVG markup

466 B of markup

2 MB maximum · read in this tab · never uploaded

Tidy the source first

Removes comments and the XML declaration, drops whitespace between tags, and folds the line breaks exporters leave between attributes. Attributes, path data, and the character data inside text, tspan, title, desc, and style are left exactly as they are. This is not an optimiser: no SVGO-style path rewriting, precision trimming, or attribute pruning happens here.

Removed 97 B · 466 B → 369 B

Preview

Rendered from the data URI

The image below is loaded from the encoded URI itself, so it fails in exactly the ways your page would.

Live preview of the encoded SVG
width
24
height
24
viewBox
0 0 24 24

Namespaced, sized, and light enough to inline. Small enough to sit in a stylesheet or component without noticeably changing its size.

Size

What each encoding costs

Byte size of the source markup and of each encoded data URI
FormBytesvs SVG
Source SVG369–
URL-encoded URI414+12.2%
Base64 URI518+40.4%

Those counts include the URI prefix: 19 bytes for the plain form, 26 for the Base64 one. Ignore the prefix and the pattern is exact: Base64 inflates any payload by +33.3% because three bytes always become four, while URL-encoding charges you +7.0% here, two extra bytes for every byte it had to escape, and nothing at all for the rest.

Comfortable to inline: Small enough to sit in a stylesheet or component without noticeably changing its size.

Ready to paste

Four outputs, all live

Every field below rebuilds as you edit the markup above. Nothing is generated on a server, so nothing has to be fetched again.

Wrapper quotes
Snippets use
URL-encoded data URIRecommended

414 bytes · +12.2% against the markup

data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none"%3E%3Crect x="2.5" y="4" width="19" height="16" rx="3.5" stroke="%237051bd" stroke-width="1.5"/%3E%3Cpath d="M3 16.6 8.6 11l4 4 3-2.6 5.4 4.6" stroke="%237051bd" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/%3E%3Ccircle cx="15.6" cy="8.8" r="1.75" fill="%237051bd"/%3E%3C/svg%3E

Base64 data URI

518 bytes · +25.1% against the URL-encoded form

data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCIgd2lkdGg9IjI0IiBoZWlnaHQ9IjI0IiBmaWxsPSJub25lIj48cmVjdCB4PSIyLjUiIHk9IjQiIHdpZHRoPSIxOSIgaGVpZ2h0PSIxNiIgcng9IjMuNSIgc3Ryb2tlPSIjNzA1MWJkIiBzdHJva2Utd2lkdGg9IjEuNSIvPjxwYXRoIGQ9Ik0zIDE2LjYgOC42IDExbDQgNCAzLTIuNiA1LjQgNC42IiBzdHJva2U9IiM3MDUxYmQiIHN0cm9rZS13aWR0aD0iMS41IiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz48Y2lyY2xlIGN4PSIxNS42IiBjeT0iOC44IiByPSIxLjc1IiBmaWxsPSIjNzA1MWJkIi8+PC9zdmc+

Reach for this when a build step, CMS field, or preprocessor rewrites percent signs and breaks the readable form.

CSS declaration

Uses the URL-encoded URI

background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none"%3E%3Crect x="2.5" y="4" width="19" height="16" rx="3.5" stroke="%237051bd" stroke-width="1.5"/%3E%3Cpath d="M3 16.6 8.6 11l4 4 3-2.6 5.4 4.6" stroke="%237051bd" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/%3E%3Ccircle cx="15.6" cy="8.8" r="1.75" fill="%237051bd"/%3E%3C/svg%3E');

Drop it straight into a rule. Keep the quotes: the URL-encoded payload carries literal spaces, which an unquoted url() will not accept.

HTML image tag

Sized 24 × 24 from the width and height attributes

<img src='data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none"%3E%3Crect x="2.5" y="4" width="19" height="16" rx="3.5" stroke="%237051bd" stroke-width="1.5"/%3E%3Cpath d="M3 16.6 8.6 11l4 4 3-2.6 5.4 4.6" stroke="%237051bd" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/%3E%3Ccircle cx="15.6" cy="8.8" r="1.75" fill="%237051bd"/%3E%3C/svg%3E' width='24' height='24' alt='' />

The alt attribute is empty because an inlined shape is usually decorative. Give it real text whenever the image carries meaning.

Wrapping in single quotes costs nothing here: your markup contains none, so every quote in it passes through the encoder untouched.

Inline when the request is the cost

A 700-byte icon costs more in connection setup and round trips than in bytes. Folding it into the stylesheet that already loads removes a request entirely, which is why inlining wins for small, single-use marks.

Keep it a file when it repeats

An inlined shape is copied into every file that references it and is cached only as part of that file. A logo used across four stylesheets is downloaded four times; a shared .svg is fetched once and reused everywhere.

A data URI is an island

The encoded document cannot reach outside itself. External fonts, linked images, stylesheets, and scripts are all unavailable, and CSS from the host page (including currentColor and custom properties) never crosses the boundary.

How it works

One request saved, one string to get right.

Inlining an SVG trades a network round trip for bytes inside a file you already ship. That bargain is worth taking for small, self-contained shapes and a poor deal for anything reused across pages, so this page shows the exact byte cost of both encodings, renders the result before you commit to it, and flags the two attributes that decide whether an inlined icon appears and at what size.

  1. 01

    Load the markup

    Paste the contents of the .svg file, or drop the file itself onto the panel. It is read inside this tab, and a 2 MB ceiling keeps the encoding instant.

  2. 02

    Read the preview and the warnings

    The artwork is drawn from the encoded URI on a checkerboard, so transparency shows and a missing namespace surfaces before the string reaches your stylesheet.

  3. 03

    Take the form your project needs

    Copy the URL-encoded URI, the Base64 fallback, a finished background-image rule, or a complete image tag with the dimensions already filled in.

Built for stylesheets

Readable output, honest measurements.

Escapes only what would break

Angle brackets, hashes, percent signs, parentheses, ampersands, backslashes and line breaks are percent-encoded. Everything else stays legible, including the spaces, and whichever quote character is not wrapping the URI.

Base64 kept as the fallback

Template engines and CMS fields sometimes eat percent signs. The Base64 URI sits one click away for those pipelines, carrying the flat one-third premium it always costs.

The size gap, measured rather than claimed

A table weighs the markup against both encodings, separates the URI prefix from the payload, and shows why the readable form is almost always the smaller one.

A clean-up pass you can audit

The optional tidy step deletes comments, the XML declaration, and the gaps between tags. Path data, attributes, and character data are never rewritten, and the saving is reported in bytes.

Catches the two classic failures

A missing xmlns stops a data URI rendering at all; a missing viewBox makes an icon land at the wrong size. Both are flagged beside the preview instead of in a console.

Local, and quiet about it

No upload, no queue, no API call. The file is read by the browser itself and encoded from that text in memory, so your unreleased artwork never leaves the machine.

Data URI questions

Encoding, sizing, colour, and limits.

What is an SVG data URI?+

It is the whole image written as a URL. The string opens with data:image/svg+xml, and the rest is the markup itself, escaped so it survives inside a URL. Because the artwork travels in the reference, a browser that has the stylesheet already has the image: no second request, no waiting on a connection for a 600-byte icon.

Should I use URL-encoding or Base64 for an SVG?+

URL-encoding, in nearly every case. SVG is text, so Base64 gains nothing: it turns three bytes into four for a fixed one-third premium regardless of content. Percent-encoding pays two extra bytes only for the handful of characters that must be escaped, typically landing 10–25% above the source. It also stays readable in a diff and compresses better, because gzip and brotli can still see the repeated tag names that Base64 scrambles. Keep Base64 for pipelines that rewrite or double-escape percent signs.

My inlined SVG does not appear at all. What is missing?+

Almost always the namespace. When you write an svg element directly in an HTML page, the parser already knows what it is. A data URI is loaded as a standalone document, so the file has to say so itself with xmlns="http://www.w3.org/2000/svg" on the root element. Without it the browser parses nothing and draws nothing, silently. The other frequent cause is a raw # in a fill colour: unescaped, it turns the rest of the URI into a fragment identifier.

Why does the icon come out at the wrong size?+

Because the encoded document decides its own geometry. With no viewBox and no width or height, a browser falls back to a 300 by 150 box. With width and height but no viewBox, the shape is locked to that size and refuses to scale. With a viewBox alone it scales freely, which is what you usually want for a background; pair it with background-size, or with explicit width and height on the image tag.

Can I recolour a data URI SVG with currentColor or a CSS variable?+

No. The encoded SVG is a separate document, and nothing from the host page reaches inside it: currentColor resolves against that document's own colour property (black, unless the markup sets it), and custom properties are not inherited across the boundary. Three ways round it: edit the fill in the markup before encoding and keep one URI per colour, drive the shape through mask-image so the visible colour comes from background-color, or drop the svg element into the HTML directly where page CSS can style it.

Can the SVG still load an external font, image, or stylesheet?+

No, and that limitation is by design rather than a bug. An SVG loaded through url() or an image tag runs in a restricted mode: no scripts execute, and no external resource is fetched, whether it is a webfont, a linked bitmap, or a stylesheet. Convert text to paths before encoding, and embed any raster artwork as its own nested data URI inside the markup.

Is there a length limit on a data URI?+

Not a practical one in current browsers: Chrome, Firefox and Safari all accept data URIs in stylesheets far beyond a megabyte. The familiar 32 KB figure is an Internet Explorer 8 limit that stopped mattering years ago. The real constraints sit elsewhere: minifiers and source maps get unwieldy, some CMS fields truncate long values, and every kilobyte is re-downloaded with the file it lives in. Treat 4 KB as comfortable and 32 KB as the point where a separate file is the better engineering decision.

What exactly does the tidy option change?+

Four things: comments, the XML declaration, whitespace-only gaps between tags, and the line breaks exporters leave between attributes. It never removes an attribute, rounds a coordinate, merges a path, or renames an id, and it leaves character data inside text, tspan, textPath, title, desc, style and script untouched. A document that asks for xml:space="preserve" keeps every gap between its tags as well, though comments and the XML declaration still go. That makes the pass safe but modest. If you want real reductions, run the file through SVGO first and paste the result here.

More focused tools, ready when you are.

Explore the growing collection for calculations, documents, writing, and everyday work.

Browse all tools