FormatterHQ

SVG to Base64

Convert SVG code or an .svg file into a Base64 data URI, <img> tag, CSS background or Markdown image, with a live preview.

Input XML
Output TEXT
Ready.

How to use the SVG to Base64

  1. Paste SVG markup into the input editor, upload an .svg file, or click Sample.
  2. Pick the Output format: Data URI, Raw Base64, HTML <img> tag, CSS background-image or Markdown image.
  3. Click Convert. With Auto update ticked, the result and preview refresh as you edit the SVG.
  4. Copy the output or download it as a file.

SVG to Base64 data URIs

SVG icons are small, sharp at any size and perfect for embedding. The SVG to Base64 converter takes SVG markup — pasted or uploaded — and encodes it into a data:image/svg+xml;base64,… URI or one of the other output formats, with a preview so you can see the graphic immediately.

Example

SVG input:

<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><circle cx="5" cy="5" r="4" fill="red"/></svg>

Data URI:

data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMCIgaGVpZ2h0PSIxMCI+PGNpcmNsZSBjeD0iNSIgY3k9IjUiIHI9IjQiIGZpbGw9InJlZCIvPjwvc3ZnPg==

The CSS option wraps it as background-image: url("…");, a common way to use icons in stylesheets without extra requests.

Base64 or URL-encoded SVG?

Because SVG is text, there is an alternative to Base64: a URL-encoded data URI such as data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'…. It is often smaller, since Base64 always adds 33%, while URL encoding only affects a few characters, and it compresses better with gzip. Base64 is simpler and more robust, though: no worries about quotes, # characters in colours or line breaks. Choose Base64 when convenience matters and URL encoding when every byte counts.

Things to know about SVG data URIs

  • The SVG must include xmlns="http://www.w3.org/2000/svg". Inline SVG in HTML works without it, but as an image it will not render.
  • currentColor and page CSS do not apply inside an SVG used as an image; the graphic is isolated from the page. Use inline SVG if the icon must change colour with its text.
  • Scripts and external resources inside the SVG are not run or loaded when it is used as an image, which also makes data-URI SVGs safer to display.
  • Minify the SVG first — editors often add metadata, comments and excess precision that inflate the result.

Build tools can do this automatically: many CSS and bundler plugins inline small SVG files as data URIs during the build, which keeps your source files readable while still avoiding extra requests.

Recolouring a Base64 icon with a CSS mask

Since an SVG used as an image ignores currentColor, a common workaround is to use the data URI as a mask and let the element's background provide the colour:

.icon {
  width: 1em;
  height: 1em;
  background-color: currentColor;
  -webkit-mask: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0i…") center / contain no-repeat;
  mask: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0i…") center / contain no-repeat;
}

The shape comes from the SVG and the colour from the text around it, so the icon changes on hover or in dark mode like an icon font would. Only the shape's opacity matters in a mask, so the fill colour inside the SVG is irrelevant. Keep the prefixed -webkit-mask line for older Safari and Chromium versions.

Input handling

The input must contain an <svg> element; other text is rejected with a clear message. You can also upload an .svg file. Tidy the markup with the SVG Formatter or view it with the SVG Viewer; to go the other way, Base64 to Image decodes SVG data URIs too.

The conversion runs in your browser; your graphics are not uploaded.

Frequently asked questions

Is Base64 or URL encoding better for SVG?
URL-encoded SVG is usually smaller and compresses better, while Base64 is simpler and avoids escaping problems. Both work in all modern browsers.
Why does my SVG data URI not display?
The most common cause is a missing xmlns="http://www.w3.org/2000/svg" attribute on the root svg element. It is required when SVG is used as an image.
Can I change the colour of a Base64 SVG with CSS?
Not directly. An SVG used as an image is isolated from page styles, so currentColor and CSS rules do not reach it. Use inline SVG or CSS masks instead.
Does the tool accept SVG files?
Yes. You can paste SVG code or upload an .svg file; both are encoded the same way.

Last updated September 26, 2026

Related tools

SVG to CSS Data URI Encode SVG as an optimized URL-encoded data URI for CSS background-image or mask, with a Base64 version for comparison. SVG to JPG Converter Convert SVG graphics to JPG images with a chosen background colour, scale and quality — directly in your browser. SVG to JSX / React Component Turn SVG markup into a React component: camelCase attributes, className, style objects and {...props}, with optional TypeScript. SVG to PNG Converter Convert SVG to PNG at any size or scale (2×, 4×…) with a transparent or solid background. Paste code or upload an .svg file. SVG to React Native Convert SVG files into react-native-svg components (Svg, Path, G, Circle…) ready to paste into your React Native app. SVG Blob Generator Create random organic SVG blob shapes for backgrounds and illustrations. Adjust complexity and randomness, add a gradient and download the SVG. TSV to Base64 Convert TSV to a Base64 string online. UTF-8 safe; paste text or upload a .tsv file. XML to Base64 Convert XML to a Base64 string online. UTF-8 safe; paste text or upload a .xml file. YAML to Base64 Convert YAML to a Base64 string online. UTF-8 safe; paste text or upload a .yaml file. Base64 Decode Decode Base64 to text online. Handles UTF-8, URL-safe Base64, missing padding and binary data downloads. Base64 Encode Encode text or files to Base64 online. UTF-8 safe, with URL-safe alphabet and MIME line wrapping options. Base64 to ASCII Decode Base64 to ASCII text; bytes outside printable ASCII are shown as \xHH escapes.