ToolHub

SVG to Data URI

Inline SVG for CSS and HTML

253 bytes
Preview of the SVG

URL-encoding an SVG keeps it readable and usually smaller than base64, which is ideal for CSS backgrounds. Base64 is handy when a tool needs a single opaque token. Paste markup or upload a .svg file. Everything runs in your browser.

Overview

Turn SVG markup into a data URI

A data URI lets you embed an image directly inside your CSS or HTML instead of linking to a separate file. For small SVG icons this is a great trade: you remove an extra network request, the icon loads instantly with the page, and there is no flash of a missing image.

ToolHub SVG to Data URI converts your SVG markup into a ready-to-use data:image/svg+xml URI. It offers two encodings: a compact URL-encoded form that only escapes the characters that must be escaped, and a base64 form for tools that expect a single opaque token. You get a live preview, the raw data URI, and a copy-paste CSS snippet. Everything runs in your browser.

Step-by-step

How to create an SVG data URI

  1. 1

    Paste SVG or upload a file

    Drop your SVG markup into the input box, or use the upload button to load a .svg file from your device.
  2. 2

    Pick an encoding

    URL-encoded is smaller and stays human-readable. Base64 is a safe single-string fallback when a tool dislikes raw angle brackets.
  3. 3

    Check the live preview

    The rendered SVG appears immediately so you can confirm the icon looks right before you ship it.
  4. 4

    Copy the URI or CSS

    Grab the data URI for an img src, or copy the ready-made background-image snippet straight into your stylesheet.

Background

URL-encoding versus base64

SVG is just text, which means you do not have to base64 it like a PNG or JPEG. You can keep the markup readable and simply escape the few characters that would break a URI. This usually produces a smaller result than base64, which inflates data by about a third.

Why URL-encoding is often smaller

Base64 turns every 3 bytes into 4 characters, a fixed 33 percent overhead. URL-encoding only expands the characters that actually need escaping, such as <, >, #, and %. For typical icon SVGs the URL-encoded version wins on size, and it has the bonus of staying legible in your CSS.

How we keep it compact

The tool first collapses the whitespace between tags, then swaps double quotes for single quotes (which do not need escaping inside the URI) and encodes only the reserved characters. The result is a tidy data:image/svg+xml, URI with no wasted bytes.

When base64 still makes sense

Some build tools, email clients, or older browsers handle a single base64 blob more reliably than escaped markup. If a URL-encoded URI ever renders oddly, switch to the base64 tab and use data:image/svg+xml;base64, instead.

Use cases

Where SVG data URIs shine

CSS background icons

Inline a chevron, checkmark, or arrow with background-image so it loads with the stylesheet and needs no extra request.

Custom list bullets and markers

Use list-style-image or a ::before background to give lists a branded icon without shipping image files.

Inline img sources

Set an img src to the data URI for a tiny logo or badge that should appear the instant the page renders.

Email-safe graphics

Embed simple shapes directly in markup where external images are blocked or stripped.

Self-contained components

Ship a UI component as a single file with its icons baked in, with no asset paths to wire up.

Form control styling

Replace default checkbox, radio, or select arrow graphics with a crisp inline SVG that scales on any display.

Tips and best practices

  • Keep data URIs for small assets. Large or detailed SVGs are better as cached external files since an inlined copy cannot be cached separately.
  • Prefer URL-encoded over base64 for CSS backgrounds. It is smaller and you can still read the markup.
  • Minify the SVG before encoding. Removing editor metadata and extra whitespace shrinks the URI noticeably.
  • Always wrap the URL value in quotes inside CSS, for example url("data:..."), so special characters never break parsing.
  • Set explicit width and height (or a viewBox) on the SVG so it renders at a predictable size when inlined.

Common questions

Why is my URL-encoded URI smaller than base64?

Because base64 adds a flat 33 percent overhead to everything, while URL-encoding only escapes a handful of reserved characters. For text formats like SVG, escaping is usually the leaner choice.

The preview is blank. What went wrong?

Make sure your markup contains a real <svg> root with an xmlns attribute. A missing namespace or a syntax error will stop the browser from rendering it.

Can I use this in an img tag?

Yes. Copy the data URI and set it as the src of an img element. Both the URL-encoded and base64 forms work as an image source.

Does inlining hurt performance?

For small icons it usually helps by removing a request. For large graphics it can hurt, because the bytes ride along on every page that uses the stylesheet and cannot be cached on their own. Inline small, link large.

100% private

Privacy and security

Your SVG is encoded entirely in your browser using built-in encodeURIComponent, btoa, and FileReader. Nothing you paste or upload is sent to a server, logged, or stored.

Related tools

Frequently asked questions

Should I use URL-encoded or base64?

Both are offered. URL-encoding usually produces a smaller result for SVG, but a base64 option is also available.

Where do I use the result?

Paste it into a CSS background-image, an img src, or anywhere a URL is accepted. A ready CSS snippet is included.

Is my SVG uploaded anywhere?

No. The conversion runs entirely in your browser.