SVG as a CSS data URI: escaping rules, URL encoding versus Base64
A data URI puts the file inside the stylesheet. background-image: url("data:image/svg+xml,...") removes a request, which is attractive for small icons used across a page. The catch is escaping: CSS parses the url() token before the data URI is decoded, so characters that are legal in SVG can terminate the CSS string or the URI early.
The essential escapes are few. # must become %23, because a hash starts a fragment and everything after it is dropped. < and > should become %3C and %3E for safety across parsers. A double quote must be encoded or replaced with a single quote, and % itself must become %25 so that an existing escape is not misread. Whitespace is collapsed to single spaces.
| Character | Encode as | Why |
|---|---|---|
| # | %23 | Starts a fragment; truncates the URI |
| < | %3C | Some parsers reject raw angle brackets |
| > | %3E | Same as above |
| " | ' or %22 | Ends a double-quoted url() early |
| % | %25 | Prevents an accidental escape sequence |
| newline | a single space | Keeps one CSS token |
URL encoding or Base64
Base64 encodes three bytes into four characters, so it grows the payload by about 33%. URL encoding leaves most of an SVG readable and grows it by much less, often 5 to 15% for markup with many quotes and hashes. Gzip then compresses the URL-encoded form better because it still contains repeated tag names. Base64 has its place when the string passes through a tool that rewrites punctuation, but for CSS it is usually the larger choice.
A useful check is to compare the two outputs for the same icon. On a 24 px stroke icon, the URL-encoded rule is commonly 15 to 25% shorter than the Base64 rule. The difference is small in absolute terms for one icon and large when a stylesheet inlines forty of them.
Sizing and colour
A background image does not inherit currentColor the way an inline SVG does. If the icon must change colour with the text, use a CSS mask instead: mask-image: url("data:image/svg+xml,...") with background-color: currentColor. The same data URI works, and the icon follows the text colour in both themes. This is the usual approach for icon buttons that must invert in dark mode.
Set background-size: contain and an explicit width and height on the element. Without a size, a background image is drawn at its intrinsic size, which for an SVG without width and height is 300 by 150 CSS pixels. Add background-repeat: no-repeat unless a repeating pattern is intended.
Keep the readable SVG source next to the generated CSS. A data URI is impossible to edit by hand, and a reviewer cannot tell what changed in a diff. Generate it in a build step and commit both.
Some build tools rewrite punctuation inside long strings, and a data URI is exactly the kind of string they break. If a generated rule stops working after a preprocessor change, compare the emitted CSS with the source: a quote turned into an entity or a hash split across lines is enough to truncate the image.
Sources
Questions
Why does my data URI show nothing?
The two most common causes are an unescaped # and an unescaped quote. A hash begins a fragment identifier, so the browser truncates the data at that point; a double quote inside a double-quoted url() ends the string early. Encode # as %23 and either encode quotes or switch to single quotes inside.
Is Base64 better than URL encoding?
For SVG, URL encoding is usually smaller, because the markup stays readable and compresses well. Base64 adds about a third to the payload. Base64 is convenient when the string must pass through a system that mangles punctuation, such as some CSS preprocessors or JSON fields.
Can a data URI SVG load an external font or image?
No. A data URI image is treated as its own document with no base URL you control, and browsers block external loads from it in most contexts. Embed fonts as paths or accept the system font stack; embed images as nested data URIs only when they are small.
The CSS tab of the optimizer produces the escaped data URI and the complete rule, ready to paste.
Open the optimizerWritten against the W3C SVG specifications, MDN and the React and WCAG documentation; every sample in the examples can be pasted into the optimizer linked above.
Back to the guides