Logo

SVG to Base64

Encode SVG as Base64 or as a shorter percent-encoded Data URI — we show you which wins

Upload SVG

Drag & drop or click to upload

SVG files

Data URL (full)

Includes the MIME type prefix — paste straight into an HTML or CSS src

Upload a file to see the Base64 output…
Raw Base64

The string only, no prefix — for APIs and databases that add their own header

Upload a file to see the Base64 output…
CSS background-image

A finished CSS rule, ready to paste into a stylesheet

Upload a file to see the Base64 output…
HTML <img> tag

Copy this straight into your markup

Upload a file to see the Base64 output…
URI-encoded (smaller for SVG)

Percent-encoding instead of Base64 — usually 20–30% shorter for SVG

Upload a file to see the Base64 output…
Your file never leaves this browser. Nothing is uploaded to a server.

Base64 Is Usually the Wrong Answer for SVG

You encode an icon, paste the string into your stylesheet, and it is enormous. Every tutorial says Base64, so you assume that is just the cost. It is not. Base64 adds a flat third to any file, and an SVG to Base64 conversion pays that tax for no reason.

SVG is already text. Percent-encoding it instead usually comes out 20 to 30% shorter. This page produces both and tells you the exact difference for your file.

We still give you the Base64 output, because some build tools and CMS fields demand it. Just know what it costs.

How to Encode SVG for CSS Without Wasting Bytes

  1. Minify with SVGO first. Design tools export a shocking amount of junk.
  2. Check for xmlns. Without it the Data URI renders blank.
  3. Drop the file in. Both encodings appear together.
  4. Read the comparison line. It names the winner in characters.
  5. Copy the shorter one. Usually the URI-encoded version.

Step one is where the real savings live. A logo exported from Figma often carries editor metadata, empty groups, and coordinates to twelve decimal places. SVGO regularly halves it.

SVG to Data URI: What Actually Gets Escaped

Percent-encoding only touches the characters that would break a URL. For SVG that is a short list:

CharacterBecomesWhy it matters
%%25The escape character itself — must go first
#%23Starts a fragment and cuts the URL in half
< >%3C %3EEvery tag in the file
{ }%7B %7DAppears in embedded CSS
"'Swapped, not escaped — avoids closing the url() early

The hash is the one that bites hardest. Any hex colour inside your markup contains one, so an unescaped file breaks the moment it has a fill="#333" in it.

A caveat on the quote swap: single quotes are valid XML, but if your SVG already contains single quotes inside attribute values, check the output before shipping.

What Breaks Inside an Inline SVG Background

Browsers treat an SVG loaded as an image as a sealed document. That protects you, and it also removes features you might be expecting.

  • No scripts. Anything JavaScript-driven simply will not run.
  • No external references. Web fonts, linked images, and outside stylesheets are blocked.
  • No CSS inheritance. currentColor cannot reach inside, so the icon will not follow your text colour.

That last point is the usual disappointment. If your icon has to change colour with a theme, a Data URI background is the wrong technique — use an inline SVG element or a CSS mask instead.

Building React components from your icons? The SVG to JSX converter turns the same file into a component whose colours you can control with props.

When to Reach for Base64 Anyway

Percent-encoding wins on size, but it is not always available. Three cases where Base64 is right:

  • Strict CMS fields. Some editors mangle raw angle brackets and percent signs.
  • Non-CSS contexts. APIs and JSON payloads usually expect the Base64 form.
  • Very small files. Below a few hundred bytes the difference is not worth the thought.

For a raster image, this whole comparison is moot — percent-encoding binary data is far worse than Base64. Use the PNG to Base64 encoder or the general image encoder for those.

Decoding an SVG String

Going the other way? The Base64 image decoder recognises SVG from its payload, which starts with PHN2Zy or PD94bWw depending on whether the file opens with an XML declaration.

One warning we repeat often: never host an SVG from an untrusted source on your own domain. In an image tag it is harmless, but served directly it becomes a same-origin document that can run scripts.

Frequently Asked Questions

Why is percent-encoding shorter than Base64 for an SVG?

+
Because an SVG is already text. Base64 always costs a flat 33% no matter what you feed it, while percent-encoding only escapes the handful of characters that break inside a URL. For typical markup that lands 20 to 30% shorter.

Do I really have to escape the # character?

+
Inside CSS, yes, always. A raw # starts a fragment identifier and truncates your URL at that point, so any hex colour in the markup silently breaks the image. We escape it as %23 in the URI-encoded output.

Why does the output swap double quotes for single quotes?

+
The CSS url() wrapper uses double quotes, so a double quote inside the SVG would close it early. Single quotes are legal XML for attribute values and avoid the collision entirely. It is a small trick that saves escaping every quote in the file.

My SVG shows up blank as a Data URI but fine as a file. What is missing?

+
Almost certainly the xmlns attribute. A standalone SVG file gets away without it in some contexts, but a Data URI is parsed strictly as XML. Add xmlns="http://www.w3.org/2000/svg" to the root element.

Can I recolour the SVG with CSS once it is a background image?

+
No, and this trips up a lot of icon systems. A background image is opaque to the page — currentColor and fill rules cannot reach inside it. Use an inline SVG element or a CSS mask if the colour has to change.

Why will my SVG animation not run inside a background-image?

+
Browsers treat an SVG loaded as an image as a sealed document. Scripts are blocked, external resources are blocked, and most interactive features are disabled. CSS and SMIL animations declared inside the file usually still run, but nothing driven by JavaScript will.

Is a Base64 SVG safe to use in an img tag?

+
Yes, because the image context blocks scripts. The dangerous pattern is different: serving an untrusted SVG directly from your own domain, where it becomes a same-origin document that can run whatever it likes.

Does gzip cancel out the Base64 penalty anyway?

+
Partly, and more here than with photos. SVG text compresses well, so gzip recovers a good share of the padding. Percent-encoding still wins, because it compresses just as well and started smaller.

My SVG contains a % sign and the URI output breaks. Why?

+
Percent is the escape character itself, so it has to be escaped first as %25. We handle that automatically, but hand-written encodings often miss it. If you are escaping by hand, do the percent signs before anything else.

Should I run the file through SVGO before encoding?

+
We would, every time. Design tools export enormous amounts of junk — editor metadata, empty groups, coordinates carried to twelve decimal places. SVGO routinely halves the file, and half the file is half the string.

The Short Version

Minify, then encode, then take the shorter output. For SVG to Base64 that usually means not using Base64 at all — the percent-encoded version does the same job with fewer characters.

And remember what an image-context SVG gives up: no scripts, no external assets, no colour inheritance. If you need those, inline the element instead.

Related Tools You Might Like

Advertisement