The short answer
To convert SVG to base64, encode the text of the SVG file with base64 and put the result after the prefix data:image/svg+xml;base64, to form a data URI. This site has no base64 tool, so the conversion happens on your computer with a short snippet below, and the file never leaves your machine. Base64 turns every 3 bytes of the file into 4 text characters, so the result is about a third larger than the SVG file. For a small icon used once in CSS, that trade is often worth it, because the image travels inside the stylesheet with no extra file request. If you are writing the SVG straight into an HTML file, do not encode it at all: paste the SVG markup inline instead.
What converting SVG to base64 actually does
SVG is short for Scalable Vector Graphics. It is an open vector format written as XML text, and it describes shapes as paths, fills and strokes. Base64 is a way to write any bytes as plain text, using only letters, digits, plus, slash and equals.
Converting SVG to base64 means encoding the text of the SVG file into that plain text form, then wrapping it in a data URI so a browser knows what it is. A data URI starts with data:, then the media type image/svg+xml, then ;base64, then a comma, then the encoded text. The browser reads everything after the comma as the image content.
Base64 exists for contexts that cannot accept raw SVG markup, such as a CSS property, an HTML image tag in generated markup, a JSON value or a field that only accepts URLs. It is not a new image format, and it does not change the picture. The same shapes come back when the string is decoded.
Method 1: Python, free
from base64 import b64encode
svg = open("icon.svg", encoding="utf-8").read()
b64 = b64encode(svg.encode("utf-8")).decode("ascii")
uri = "data:image/svg+xml;base64," + b64
print(uri)For the example icon, a 113 byte SVG file becomes 152 base64 characters, about a third larger, and decoding the string gives back the exact same SVG text. Keep the xmlns attribute and the viewBox in the SVG file before you encode it, because the data URI has no other way to know the size or the namespace.
Method 2: JavaScript in the browser
const base64 = btoa(svgString);
const dataUri = "data:image/svg+xml;base64," + base64;
// For text outside the Latin-1 range, use btoa(unescape(encodeURIComponent(svgString))) instead.btoa only handles characters in the Latin-1 range. If the SVG text holds characters outside that range, such as emoji or Chinese characters, btoa throws an error. The comment line in the snippet shows the fix: encode the string first, then pass the result to btoa.
Method 3: command line
base64 -w 0 icon.svgThe command prints the same 152 character string shown in section 2 for the example icon. Add the data:image/svg+xml;base64, prefix in front of it to form the data URI.
Using the data URI in CSS and HTML
.icon {
background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PGNpcmNsZSBjeD0iMTIiIGN5PSIxMiIgcj0iMTAiIGZpbGw9IiNjNzRiMzEiLz48L3N2Zz4=");
}
<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PGNpcmNsZSBjeD0iMTIiIGN5PSIxMiIgcj0iMTAiIGZpbGw9IiNjNzRiMzEiLz48L3N2Zz4=" alt="Red circle icon" width="24" height="24">The example icon is a red circle on a 24 by 24 viewBox, and the base64 string above is the real encoded form of that exact SVG file, checked by decoding it back. When the data URI shows nothing, check the quotes first, then check that the string was copied in full with no line breaks.
The smaller option: URL encoded SVG
.icon {
background-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Ccircle%20cx%3D%2212%22%20cy%3D%2212%22%20r%3D%2210%22%20fill%3D%22%23c74b31%22%2F%3E%3C%2Fsvg%3E");
}Two rules keep the URL encoded form working. Escape every # as %23, because an unescaped # starts a fragment and cuts the URI short. Keep the xmlns attribute and the viewBox, because the data URI has no other way to know the namespace or the size. For the tiny example icon in this guide, the URL encoded form was 214 characters against 178 for base64, so test both when size matters.
When base64 helps and when it hurts
| Use | Base64 data URI | Plain SVG file |
|---|---|---|
| One small icon in CSS | Yes, no extra file request | Also fine, and easier to edit |
| Icon reused on many pages | No, the string repeats in every file | Yes, the browser caches one file |
| Large or detailed SVG | No, base64 adds about a third | Yes, keep it as a file |
| SVG inside JSON or an API payload | Yes, plain text travels safely | No, a file URL may not be allowed |
| Generated HTML with no file hosting | Yes, the image travels with the page | No, there is no file to link to |
| Editing the icon later | No, the string is hard to read | Yes, edit the file directly |
Checking the result before you use it
- Decode the string back to text and compare it with the original SVG file. A single wrong character breaks the image.
- Open the data URI in a browser tab. The icon should appear at the size set by the width and height attributes or by the CSS.
- Check the size. If the string is much larger than the SVG file, the file may hold hidden metadata or a large embedded image, so clean the SVG first.
- Check the quotes. The data URI sits inside url("...") in CSS or src="..." in HTML, so a stray double quote inside the string breaks the page.
- Check the colors. A # that was not encoded as %23 in the URL encoded form breaks the URI, so test the exact string you ship.
- Keep the original SVG file. Edit the file and encode it again, never edit the base64 string by hand.
Sources
Written by Shahab Uddin. Published ; updated . Corrections are welcome through the contact page.