Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Encode SVG as Base64: Steps, Examples, and When It’s Needed

A practical guide to SVG Base64 data URLs, command-line encoding, percent-encoded SVG, and when an external file is the better fit.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To encode an SVG as Base64, convert the SVG file’s bytes to Base64 and prepend data:image/svg+xml;base64,. For example, use base64 < icon.svg in a shell, then place the output after that prefix. Base64 is optional for ordinary SVG markup: a percent-encoded text data URL or a separate .svg file may be easier to inspect, edit, or reuse.

What an SVG Base64 data URL contains

A data URL has this form: data:[<media-type>][;base64],<data>. The comma separates the metadata from the payload. For an SVG, the media type is image/svg+xml; the optional ;base64 indicates that the payload must be Base64-decoded. See the MDN data URL reference.

As an Amazon Associate I earn from qualifying purchases.

Here is a complete example: data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==. Its payload encodes a minimal SVG document. It demonstrates the format, not a requirement to Base64-encode every SVG.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Raw Base64 by itself is only the encoded payload, not a complete data URL. To use it as one, include the MIME type, the ;base64 marker, and the comma.

Encode an SVG file from the command line

  1. Open a terminal in the directory containing the SVG, or provide its full path.
  2. Run base64 < icon.svg. Input redirection encodes the file’s existing bytes without adding a newline, unlike a command that first prints text.
  3. If the utility wraps its output across lines, remove those line breaks when the field receiving the URL requires a single uninterrupted payload.
  4. Prepend data:image/svg+xml;base64, to the encoded output. The result is the data URL to paste into the destination.

MDN documents the base64 utility and the general data URL pattern in its data URL guide. Utility behavior and options can differ across platforms, so check the local command’s help if you need a particular output format.

Encode SVG text in JavaScript

For text in a browser or another JavaScript runtime, first convert the string to the intended UTF-8 bytes, then Base64-encode those bytes. btoa() expects a binary string; passing arbitrary Unicode text directly can fail or produce an unintended result when characters are outside its byte range. MDN covers the browser APIs and this encoding distinction in its btoa() reference.

Use the API documentation for your target runtime when choosing the precise byte-conversion method. The important point is to encode the SVG’s UTF-8 bytes, rather than treating every JavaScript string as if it were already a byte sequence.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Base64 is optional for SVG

SVG is text-based markup, so it can appear in a data URL without Base64. In that form, characters that would conflict with URL syntax or the surrounding HTML or CSS need appropriate escaping. For example:

data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3C/svg%3E

Percent-encoding the markup is valid, but encoding only characters that need escaping can make the URL shorter. The exact escaping also depends on the context that contains the URL: CSS quoting and URL encoding are separate concerns. MDN’s CSS <url> reference shows encoded SVG markup and selective escaping, including %23 for a # in a color value.

Choose the representation that fits the destination

Option Readability and editing Size and reuse When it fits
Base64 data URL The SVG markup is obscured in the payload. Base64 can expand text; compare the complete output for your asset. The URL embeds the payload at each use. Use when the receiving integration specifically expects a Base64 data URL, or when arbitrary bytes must be represented as text.
Percent-encoded SVG data URL The markup remains more recognizable, though URL escapes and surrounding syntax still need care. Its length depends on the markup and escaping. Compare the complete URL with the Base64 version. Useful for short inline SVG markup where the consumer accepts a non-Base64 data URL.
Separate .svg file The original markup remains straightforward to maintain. A single file can be referenced in multiple places; reuse and caching behavior depend on how it is served and consumed. Often simpler for larger or reused SVGs, or when the graphic relies on linked resources.

There is no universal size increase that applies to every SVG. Base64 representation, padding, payload contents, and compression or transport layers affect the comparison. If size matters, compare the complete URLs or files you will actually deliver.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check rendering and resource restrictions

Where SVG is used as an image, such as in an <img> element or CSS background-image, scripts may be disabled and external resources may not load. Those restrictions differ from using an SVG as a document through elements such as <iframe>, <object>, or <embed>. MDN explains the image-context restrictions in its SVG as an image guide.

CSS resource fetching can also be subject to CORS restrictions depending on the property and resource context; see MDN’s CSS URL reference. A data URL embeds its own payload, but an SVG may still refer to other resources. Data URLs do not have relative URL forms, so scrutinize any relative references inside an embedded SVG. The RFC 2397 specification describes the data URL scheme.

Keep data URLs short and compare the complete result

RFC 2397 says, “The ‘data:’ URL scheme is only useful for short values.” Its discussion includes historical SGML and HTML attribute limits as examples; those 1998 figures are not current browser caps. Applications can impose their own length limits, and no single current browser limit applies universally.

For a small, one-off graphic, an inline data URL may be convenient. For a large SVG, one used in several places, or one that depends on linked assets, a separate file is often easier to maintain. Check whether the target accepts the chosen representation and test the finished URL in that context.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.