SVG to Base64: How to Encode a Vector Logo and When It Makes Sense
How to convert an SVG logo to Base64, build a data URI, and check the result, plus what Base64 does and does not change about your vector artwork.
Contents
The short answer
Converting an SVG to Base64 rewrites the file's text as a string of plain ASCII characters. You can then embed that string directly in HTML, CSS, JSON or other text-based places through a data URI, so the logo doesn't need to be requested as a separate file. The encoding is lossless and fully reversible. Decoding the string gives back exactly the SVG markup you started with.
Base64 is a transport format. It is not a design format. It does not vectorize anything, improve the artwork, compress it or protect it. Whatever the original SVG contained, whether clean vector paths, live text or an embedded raster image, the encoded version contains exactly the same thing.
What changes and what stays the same
| Aspect | Plain SVG file | Base64-encoded SVG |
| Content | Readable XML markup | Same markup, rewritten as Base64 characters |
| Vector quality | Scales without loss if built from paths | Identical, because encoding does not alter the artwork |
| Size | Original byte count | Roughly one third larger, since every 3 bytes become 4 characters |
| Editability | Open and edit in a vector editor or text editor | Must be decoded back to SVG before editing |
| Caching | Browser can cache it as a separate file | Cached only as part of the HTML or CSS that contains it |
| Typical use | Logo delivery, web images, print handoff | Inline embedding in CSS, HTML, JSON configs or single-file builds |
Vector paths, live text and embedded rasters
Before you encode a logo, check what the SVG actually contains. A .svg extension doesn't guarantee vector artwork. Some SVG files are wrappers around an embedded PNG or JPEG, which stays a raster image and will pixelate when scaled up. Base64-encoding that file just puts one more layer of encoding around the same bitmap. Open the file in a text editor and look for an image element carrying its own embedded data. Its presence means at least part of the logo is raster.
Live text is the other common problem. If the logo's wordmark is set in a font that is referenced by name and not converted to outlines, it will render in whatever fallback font the viewing system has once it is embedded as a data URI. For a portable logo, convert text to outlines in your vector editor before exporting.
How to convert an SVG logo to Base64
Starting materials: a final, exported SVG of the logo, plus a command line, a short script, or a converter tool you trust. If the logo is confidential or unreleased, prefer local methods over online converters, because uploading the file hands its contents to a third party.
- 1. Prepare the source SVG: outline all text, remove hidden layers and unused definitions, and confirm that no linked external images or fonts remain.
- 2. Optimize the markup if you like, by removing editor metadata, comments and excess decimal precision. Base64 adds size, so it helps to start small.
- 3. Make sure the root svg element declares the SVG namespace and has a viewBox, so the logo scales correctly wherever it is embedded.
- 4. Encode the file into a single line with no line breaks, using one of the methods below.
- 5. Add the data URI prefix: data:image/svg+xml;base64, followed directly by the encoded string.
- 6. Embed the result where you need it and inspect it as described in the final check.
Command line on Linux (the -w 0 flag stops line wrapping):
base64 -w 0 logo.svg > logo.b64
Command line on macOS:
base64 -i logo.svg -o logo.b64
Python:
import base64
with open("logo.svg", "rb") as f:
encoded = base64.b64encode(f.read()).decode("ascii")
data_uri = "data:image/svg+xml;base64," + encoded
print(data_uri)
Node.js:
const fs = require("fs");
const encoded = fs.readFileSync("logo.svg").toString("base64");
const dataUri = `data:image/svg+xml;base64,${encoded}`;
console.log(dataUri);
In the browser, the built-in btoa function only accepts Latin-1 characters. An SVG that contains non-ASCII text, such as a title or description in another script, needs to be converted to UTF-8 bytes first, or btoa will throw an error.
const bytes = new TextEncoder().encode(svgString);
const binary = Array.from(bytes, b => String.fromCharCode(b)).join("");
const dataUri = "data:image/svg+xml;base64," + btoa(binary);
Using the encoded logo
The data URI goes wherever a normal image address would go. The most common places are an img element's src attribute and a CSS background-image value:
<img src="data:image/svg+xml;base64,PHN2Zy..." alt="Company logo">
.logo {
background-image: url("data:image/svg+xml;base64,PHN2Zy...");
}
When an SVG is loaded as an image this way, browsers do not run its scripts or load external resources it references. Any linked font, stylesheet or image inside the file will simply be missing. That restriction is why outlining text and embedding everything matters in the preparation step. Also, a site with a strict Content Security Policy may block data URIs for images unless the policy allows them.
Final check
- Decode the string back to a file and compare it with the original. The two should match exactly.
- Render the data URI in a browser at a small size and a large size to confirm the edges stay sharp. Blurring points to embedded raster content.
- Confirm the wordmark shows the intended letterforms and not a fallback font.
- Check that colors match the brand specification, since missing styles or an unresolved external stylesheet can change fills.
- Confirm the string has no line breaks or stray whitespace, which can break the URI in some contexts.
When Base64 is the wrong choice
Base64 suits small logos and icons that must travel inside a single text file, such as a CSS bundle, a self-contained HTML page or a JSON configuration. For a logo that appears on many pages, a separate SVG file is usually the better choice, because the browser can cache it once. Base64 also adds size. SVG is already text, so it can instead be embedded as a URL-encoded data URI, with characters such as the hash sign, angle brackets and quotation marks percent-encoded. That approach avoids the Base64 overhead and often compresses better on the server. Which option ends up smaller for a particular logo should be measured, not assumed.
For logo delivery to clients, printers or other designers, send the plain SVG along with any other formats they need. A Base64 string is awkward to open, preview or edit, and recipients have to decode it before they can work with the artwork.
Rights are unaffected by encoding
Encoding a logo doesn't change who owns it or how it may be used. Being able to download or embed a logo file is a different thing from having permission to modify the artwork, redistribute the fonts used in it, or use the trademark. If the logo isn't yours, check the owner's brand guidelines or license before embedding it.
Evidence gaps
This guide draws on general, verifiable properties of the Base64 encoding and the SVG and data URI formats. No tool-specific test results, file-size measurements, or compatibility data for particular browsers, email clients or design applications were available. Check those details against your own target environment before relying on an embedded logo in production.