Convert SVG to a data URI
Paste SVG markup or drop an .svg file, and copy it back as a data URI, a CSS background rule, or an img tag. Both encodings are measured, so you can see which is shorter.
How to use it#
- Drop an SVG file onto the box above, or paste its markup into the box below it.
- Pick URL-encoded or base64, and the form you need: the bare data URI, a CSS background rule, or an img tag.
- Leave Remove comments and spare whitespace first ticked for a shorter result, or untick it to keep the markup exactly as it is.
- Copy the result from the box at the bottom.
Why SVG does not need base64#
Base64 exists to carry binary data as text, and it makes whatever it carries about a third larger. A PNG needs that. An SVG is already text, so it can go into a data URI almost as it is, which is shorter than base64 for most SVGs and stays readable, so you can still see which icon it is and tweak a colour in place.
Almost as it is, because a few characters mean something in the places a data URI is pasted. Text in a script such as Chinese or Arabic is the exception, since every letter becomes several escapes, and base64 can come out shorter. The counts beside the preview show both encodings for your SVG, so you can choose on the real numbers.
Exactly which characters are escaped, and why#
A hash sign ends a URL and starts a fragment, so an unescaped # in a colour such as #ff0000 cuts the SVG off at that point, which is a common reason a hand-made SVG data URI shows nothing. A percent sign starts an escape, so a literal one is escaped itself. Angle brackets are refused by some browsers inside a URL. A double quote would end the url("…") or the src="…" around it, and an ampersand or a backslash would be read as the start of something else in HTML and CSS.
Everything else is left alone. Double quotes around attribute values become single quotes where that is safe, which saves four characters per attribute over escaping them, and any letter outside plain ASCII is escaped as UTF-8, so the result survives being saved in a file of any encoding.
When inlining an SVG is the right call#
A data URI saves a request, which is worth it for small icons used on one page, such as a chevron in a select box, a checkmark, or a background pattern in a stylesheet that would otherwise wait for a second download. It keeps a component self contained, too, with no separate file to ship alongside it.
It is the wrong call for a large illustration, or for an icon repeated in the HTML of many pages. An inlined SVG is downloaded as part of whatever holds it, so a copy in each page is downloaded with each page, and it cannot be cached on its own. Past a few kilobytes, a separate file usually wins.
Frequently asked questions#
Is my SVG uploaded to encode it?
#
No. The markup is encoded in this tab, and the preview is drawn here as an image. Nothing is sent to a server, so an unreleased icon set stays on your machine.
URL-encoded or base64?
#
URL-encoded, for most SVGs. It is usually shorter and stays readable, and the counts beside the preview show which is shorter for yours. Base64 is the choice when something downstream only accepts base64 data URIs, which some older email tools and build pipelines insist on.
Why does my data URI show nothing?
#
Usually an unescaped # in a colour, which cuts the URI off at that point, or a quote that closes the CSS string early. Both are escaped here. If it still shows nothing, check the SVG has an xmlns attribute, which a standalone SVG needs to be drawn at all. The tool warns when it is missing.
What does removing spare whitespace take out?
#
The XML declaration, comments, and runs of spaces and line breaks. Where the SVG has text in it, one space is kept wherever there was whitespace, because a space between words is content. An SVG that asks for its whitespace to be kept exactly is left untouched.
Can I change the colour of an inlined SVG with CSS?
#
Not through a background image, which is drawn as a picture and cannot be styled from outside. For an icon that should follow the text colour, use the data URI as a CSS mask-image on an element whose background-color is currentColor, or put the SVG markup straight into the HTML.