ModernCalcs

SVG to Base64 Converter

Convert SVG markup into a base64 or URL-encoded data URI, with a live preview and ready-to-use CSS.

This doesn't look like valid SVG markup.
No preview
Base64 Data URI
data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0OCIgaGVpZ2h0PSI0OCIgdmlld0JveD0iMCAwIDQ4IDQ4Ij4KICA8Y2lyY2xlIGN4PSIyNCIgY3k9IjI0IiByPSIyMCIgZmlsbD0iIzYzNjZmMSIgLz4KICA8cGF0aCBkPSJNMTYgMjRsNiA2IDEyLTEyIiBzdHJva2U9IndoaXRlIiBzdHJva2Utd2lkdGg9IjMiIGZpbGw9Im5vbmUiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgLz4KPC9zdmc+
URL-Encoded Data URI (smaller)
data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2248%22%20height%3D%2248%22%20viewBox%3D%220%200%2048%2048%22%3E%0A%20%20%3Ccircle%20cx%3D%2224%22%20cy%3D%2224%22%20r%3D%2220%22%20fill%3D%22%236366f1%22%20%2F%3E%0A%20%20%3Cpath%20d%3D%22M16%2024l6%206%2012-12%22%20stroke%3D%22white%22%20stroke-width%3D%223%22%20fill%3D%22none%22%20stroke-linecap%3D%22round%22%20stroke-linejoin%3D%22round%22%20%2F%3E%0A%3C%2Fsvg%3E
CSS background-image
background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0OCIgaGVpZ2h0PSI0OCIgdmlld0JveD0iMCAwIDQ4IDQ4Ij4KICA8Y2lyY2xlIGN4PSIyNCIgY3k9IjI0IiByPSIyMCIgZmlsbD0iIzYzNjZmMSIgLz4KICA8cGF0aCBkPSJNMTYgMjRsNiA2IDEyLTEyIiBzdHJva2U9IndoaXRlIiBzdHJva2Utd2lkdGg9IjMiIGZpbGw9Im5vbmUiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgLz4KPC9zdmc+");

SVG to Base64: Embed Icons Without an Extra HTTP Request

Encoding an SVG as a data URI lets you embed it directly inside CSS or HTML — no separate file, no extra network request. This tool converts pasted or uploaded SVG markup into both a base64 data URI and the smaller URL-encoded alternative, with a live preview and a ready-to-paste CSS background-image declaration.

Formula
data:image/svg+xml;base64,<base64> or data:image/svg+xml,<url-encoded>

URL-encoded is usually smaller than base64 for SVG specifically, since SVG is already text.

Why URL-Encoding Beats Base64 for SVG

Base64 encoding is designed for arbitrary binary data and has a fixed ~33% size overhead regardless of content. SVG is plain text (XML), so URL-encoding — which only escapes the specific characters that would otherwise break a data URI or CSS url() — is typically more compact than converting that same text through base64's binary-oriented scheme.

When Inlining Actually Helps

Inlining trades a network request for a larger inline payload. This is a clear win for small, frequently-reused icons (checkmarks, arrows, small logos) where the HTTP request overhead — even with caching — outweighs a few hundred bytes of inline data. For large or rarely-used SVGs, a regular file reference with proper caching is usually still better.

Escaping Characters for CSS url()

When an SVG data URI is used inside CSS's url("..."), any unescaped quote or parenthesis in the URL-encoded string would prematurely close the url() function or the surrounding quotes. This tool escapes those specific characters (', ", (, )) in the URL-encoded output so it's safe to drop directly into a background-image declaration.

Practical Examples

Inlining a Small Icon

Avoiding a separate icon file request.

  • 1.Paste SVG markup for a checkmark icon
  • 2.Copy the URL-encoded data URI
  • 3.Use as background-image in a .icon-check class

Comparing Encoding Sizes

Choosing the more compact option.

  • 1.Base64 output: ~450 characters
  • 2.URL-encoded output: ~340 characters
  • 3.URL-encoded wins for this SVG

What You Get

  • Base64 data URI: universally compatible
  • URL-encoded data URI: usually smaller
  • Ready-to-use CSS: background-image declaration
  • Live preview on a checkerboard (transparency-aware) background

Good Use Cases

  • Inlining small, reusable icons directly in CSS
  • Avoiding an extra HTTP request for a tiny logo
  • Embedding an SVG in an email template (which often can't reference external files)
  • Quickly checking a data URI's actual rendered output

Frequently Asked Questions

Why convert an SVG to a data URI instead of linking to a file?

A data URI embeds the image directly in your CSS or HTML, eliminating a separate HTTP request. This is useful for small, frequently-used icons where the request overhead outweighs the (small) size cost of inlining.

Base64 or URL-encoded — which should I use?

URL-encoded is typically 20-30% smaller than base64 for SVG specifically (since SVG is text, and base64 adds ~33% overhead encoding text as if it were arbitrary binary). Use URL-encoded when you can, and base64 when a tool or system specifically expects it.

Why does the URL-encoded version escape quotes and parentheses?

Those characters have special meaning inside a CSS url(...) function. Encoding them as %27, %22, %28, %29 avoids breaking the surrounding CSS syntax, since a raw quote or parenthesis in the data URI could prematurely terminate the url() value.

Does this work for any SVG, including ones with embedded fonts or scripts?

It encodes whatever SVG markup you provide, but SVGs embedded via CSS background-image or img src have some restrictions — scripts inside them typically don't execute, and external resource references (fonts, other images) may not resolve depending on the embedding context.

Is my SVG uploaded anywhere?

No, encoding happens entirely in your browser using the native btoa/TextEncoder and encodeURIComponent APIs — no upload, whether you paste markup or select a local file.

Why does my SVG file upload work but pasting the same content sometimes look different?

File upload reads the file's raw bytes as text, which is identical to pasting the same content — if the preview differs, check for a UTF-8 BOM or encoding declaration mismatch in the original file that could affect how special characters are interpreted.