Format Code Now
/

SVG to Base64 Converter

ED-70

Paste SVG code and get a data URI in Base64 or in URL-encoded form. The size line shows both lengths, and URL-encoding is usually the shorter one for SVG.

Runs in your browser. Nothing is uploaded.

SVG code (paste it, or open a .svg file)

Data URI

How to use it

  1. Paste your SVG code, or open a .svg file. It must start with the svg tag, with an optional XML declaration first.
  2. Pick Base64 or URL-encoded, then pick plain data URI, CSS background-image or HTML img tag.
  3. Read the size lines to see which form is shorter for your SVG.
  4. Copy the result. The preview shows how a browser draws the SVG as an image.

Base64 or URL-encoded

Base64 turns the SVG into a block of letters and digits, about a third larger than the SVG. URL-encoded keeps the markup readable and escapes only the characters that would break a URI, a CSS string or an HTML attribute: percent, hash, angle brackets, double quote, ampersand and backslash, plus control and non-ASCII characters. Because most SVG is plain text with few of those characters, the URL-encoded form is usually shorter. The tool prints both lengths so you can see it for your own file.

What the options change in your SVG

Collapse whitespace turns runs of spaces and line breaks into one space, and is skipped automatically when the SVG has a script, a foreignObject or xml:space. In the URL-encoded form, attribute values in double quotes that contain no quote are switched to single quotes, which avoids the longer %22 escapes; choose Keep double quotes to leave them as %22. Encode spaces as %20 gives a stricter URL. The SVG is not optimized or validated beyond the starting check.

Why the xmlns attribute matters

Used as an image, in img or in CSS, an SVG must declare its namespace, xmlns="http://www.w3.org/2000/svg", on the svg tag. Without it the browser will not draw it. When the attribute is missing the tool says so and leaves your markup alone. Tick Add xmlns if missing if you want it inserted. SVG code that you paste inline into an HTML page does not need it.

Error messages and what to do

MessageWhat it meansFix
There is no SVG to convert.The text is empty after removing a byte order mark.Paste the SVG code.
This does not look like SVG: the text must start with <svg> (an XML declaration, comments and a doctype may come first).The text does not begin with an svg element.Remove anything before the svg tag, or paste the whole file.
That is already a data URI. To get the picture back from one, use the Base64 to image converter.You pasted an encoded result instead of SVG code.Paste the original SVG, or decode the URI with the Base64 to Image converter.

Questions people ask

How do I convert SVG to Base64?

Paste the SVG code into the left box and keep the Base64 encoding selected. The output is a data URI starting with data:image/svg+xml;base64, that you can use in an img src or a CSS url(). Press Copy to take it.

Is URL-encoded SVG better than Base64?

Often it is shorter, and it stays readable, but it depends on the file. The tool shows the length of both forms for your SVG, so you can pick the shorter. Base64 is the safer choice when the place you paste into cannot handle raw angle brackets.

How do I use an SVG data URI in CSS?

Choose CSS background-image to get a ready rule such as background-image: url("data:image/svg+xml,...");. Paste it inside a selector. The URI is written so it is safe inside double quotes.

Why does my SVG data URI show nothing?

The most common cause is a missing xmlns="http://www.w3.org/2000/svg" on the svg tag. The tool warns when it is missing and can add it for you. Also check that the SVG code itself is complete and valid.

Does it shrink or clean my SVG?

No. It collapses whitespace and, for the URL-encoded form, swaps safe double quotes for single quotes. It does not remove comments, metadata or unused elements. Run an SVG optimizer first if you want the smallest possible result.

How do I turn the data URI back into an SVG?

Paste the data URI into the Base64 to Image converter on this site. It decodes both Base64 and percent-encoded SVG data URIs, shows the picture and lets you download it as an .svg file.

About this tool

Writes your SVG text as a data URI in two ways. Base64 encodes the UTF-8 bytes. URL-encoded leaves the text readable and escapes only what would break a URI, a CSS string or an HTML attribute: % # < > " & backslash, control characters and non-ASCII characters. By default runs of whitespace are collapsed to one space (skipped when the SVG has a script, a foreignObject or xml:space), and in the URL-encoded form double-quoted attribute values that hold no quote become single-quoted. The SVG is not optimized or validated: the tool only checks that the text starts like SVG. Without an xmlns on the svg tag browsers will not draw the image, and the option to add it changes your markup, so it is off by default.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.