SVG to Data URI: URL-Encoded or Base64?
Last updated: · 7 min read
Small icons such as a check mark, a select arrow or a list bullet do not have to be served as separate files; they can be embedded straight into your CSS. What makes this possible is the data: URL scheme defined in RFC 2397: the content of the image is written into the address itself and the browser sends no extra request. For SVG there are two ways to encode that content, and they do not give the same result. Base64 can carry any kind of data but inflates it by about a third; SVG is text, so escaping a handful of characters with percent signs is enough. The SVG to Data URI Converter produces both forms, gives you ready-made CSS and HTML lines and reports the sizes in bytes. This guide explains which characters are escaped and why, how the two encodings differ, and when a data URI is not appropriate. The outputs in the example are the tool's real results.
Formula
Rules:
1. The code must start with an <svg …> tag; an XML declaration or DOCTYPE before it and all comments are removed.
2. Whitespace between tags is removed (it is kept in SVGs that contain text elements) and runs of whitespace collapse to a single space.
3. If the root element has no xmlns, xmlns="http://www.w3.org/2000/svg" is added and a note is appended to the output.
4. With the URL-encoded option, the double quotes around attribute values are changed to single quotes unless the value itself contains a single quote. Then only these characters are escaped as %XX: % # < > " { } | \ ^ ` &, control characters and non-ASCII characters (as UTF-8 bytes). Spaces, =, :, / and single quotes are left alone. Prefix: data:image/svg+xml,
5. With the Base64 option, the cleaned SVG is converted to UTF-8 bytes and the bytes to Base64. Prefix: data:image/svg+xml;base64,
6. The output lists the data URI, a ready background-image: url("…"); line, an <img src="…" alt=""> tag and both sizes in bytes.
Input:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#0a7" d="M12 2 2 22h20z"/></svg>
Output (URL-encoded):
data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%230a7' d='M12 2 2 22h20z'/%3E%3C/svg%3E
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%230a7' d='M12 2 2 22h20z'/%3E%3C/svg%3E");
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%230a7' d='M12 2 2 22h20z'/%3E%3C/svg%3E" alt="">
Original SVG size: 104 bytes
Data URI size: 137 bytes
Output (Base64):
data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZmlsbD0iIzBhNyIgZD0iTTEyIDIgMiAyMmgyMHoiLz48L3N2Zz4=
background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZmlsbD0iIzBhNyIgZD0iTTEyIDIgMiAyMmgyMHoiLz48L3N2Zz4=");
<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZmlsbD0iIzBhNyIgZD0iTTEyIDIgMiAyMmgyMHoiLz48L3N2Zz4=" alt="">
Original SVG size: 104 bytes
Data URI size: 166 bytesThe syntax in RFC 2397 is data:[<mediatype>][;base64],<data>. Without ";base64", the data is written as ASCII for safe characters and as %XX escapes for everything else. # has to be escaped because, under RFC 3986, it starts the fragment; % is the escape character itself. < > and " are not among the characters RFC 3986 allows in a URI. & is escaped so that it is not mistaken for a character reference when the line is pasted into an HTML attribute. Base64 writes every 3 bytes as 4 characters (RFC 4648), which is where the size increase comes from.
What the inputs mean
- SVG
- SVG code that starts with an <svg …> tag. A leading XML declaration and comments are allowed. Up to 1,000,000 characters are processed.
- Encoding
- "URL-encoded (smallest)" escapes only the characters that must be escaped and gives a readable result; "Base64" converts the whole content to Base64. Choose Base64 if the URI goes somewhere that cannot take quotes or spaces.
Worked example: Encoding a triangle icon both ways
- The input is an SVG of 104 bytes. With the URL-encoded option, double quotes are first changed to single quotes: xmlns='http://www.w3.org/2000/svg'. The result therefore fits inside a double-quoted url("…") or src="…" without further escaping.
- < becomes %3C and > becomes %3E: the result starts with %3Csvg and ends with %3C/svg%3E.
- The # in fill="#0a7" becomes %23: fill='%230a7'. Left unescaped, the rest of the address would count as a fragment and the image would not be drawn.
- Spaces, slashes, colons and equals signs are left as they are, which keeps the result readable. The tool's size lines read: Original SVG size: 104 bytes and Data URI size: 137 bytes.
- With the Base64 option the quotes are not touched. The 104 bytes form 35 groups of three and give 140 Base64 characters; together with the 26-character prefix data:image/svg+xml;base64, the result is: Data URI size: 166 bytes.
Result: The same 104-byte SVG takes 137 bytes URL-encoded and 166 bytes as Base64. Both are larger than the original file; the URL-encoded form is shorter and can still be edited by hand.
🔗 SVG to Data URI Converter (CSS)
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
A data URI removes a network request, but not for free. The embedded image is not cached as a file of its own; it travels with the CSS or HTML that contains it. If several rules use the same icon, store the address once in a CSS custom property (--icon: url("…")) and reference that, otherwise it is repeated at every use. RFC 2397 itself says the scheme is only useful for short values: icons of a few hundred bytes are good candidates, while large illustrations and images used across many pages are better kept as separate files. There is a second limit. An SVG used as an image (img or background-image) is not part of the document. Under SVG 2, scripts do not run in that case, external resources are not loaded and there is no interactivity; the page's CSS does not reach inside either. currentColor therefore does not follow the text colour of the page, and the colour has to be set inside the SVG before encoding. This is general information.
Common mistakes
- Writing a data URI by hand without escaping #. In an address containing fill='#0a7', everything after the # counts as a fragment and the image breaks; the correct form is fill='%230a7'.
- Putting the URL-encoded result into an unquoted url(). According to CSS, spaces, single quotes, double quotes and parentheses must be escaped in an unquoted url(); use the double-quoted line exactly as the tool prints it.
- Using the URL-encoded result in a srcset attribute. srcset candidates are separated by spaces and commas, so an address that contains both is split there. Choose Base64 for such places.
- Expecting currentColor or the page's web font to work in the embedded SVG. An SVG used as an image sees neither the page's styles nor external resources.
- Embedding a large drawing. The data URI enlarges the style sheet that contains it, and the whole style sheet is downloaded again whenever it changes.
- Pasting a fragment that does not start with <svg. The tool reports "No valid SVG found: the code must start with an <svg …> tag."
Frequently asked questions
Should I use URL encoding or Base64?
For SVG, URL encoding in most cases: the result is shorter, stays readable, and the colour can be changed by hand. Base64 suits binary data such as PNG, or places where the address cannot contain quotes and spaces (srcset, for instance). In the example the difference is 137 bytes against 166.
Why is xmlns added?
According to SVG 2, a standalone SVG document must declare the namespace, whereas for SVG written inline in HTML the HTML parser supplies it. That is why SVG copied from a page often has no xmlns. A data URI is processed like a standalone document, so the tool adds the missing declaration and says so in a note.
Why is Base64 larger?
Under RFC 4648, Base64 writes each 24-bit group (3 bytes) as 4 characters, which means an increase of roughly 33 per cent whatever the content. With URL encoding, only the escaped characters grow to three characters, and in a typical SVG those are a minority.
Can I change the colour of an embedded icon with CSS?
Not directly; the page's CSS does not reach inside an SVG used as an image. In practice a separate data URI with a different fill value is generated for each colour. With the URL-encoded form you can also do this by hand: the %230a7 part is the colour code.
Is there a size limit for data URIs?
RFC 2397 sets no fixed limit, but it states that the scheme is only useful for short values and that applications may impose their own limits. The tool processes input of up to 1,000,000 characters; in practice it makes sense to restrict embedding to small icons.
Is my SVG uploaded to a server?
No, the encoding runs in your browser.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.
Sources
- IETF RFC 2397: The "data" URL scheme
- IETF RFC 3986: Uniform Resource Identifier (URI): Generic Syntax
- IETF RFC 4648: The Base16, Base32, and Base64 Data Encodings
- W3C CSS Values and Units Module Level 4: Resource Locators: the <url> type
- W3C SVG 2: Namespace
- W3C SVG 2: Processing modes for referenced SVG (SVG as an image)