SVG Data URIs in CSS: URL-Encoding vs Base64, Done Right
When to inline SVG icons as data URIs, why URL-encoding usually beats Base64, which characters must be escaped, and how to recolour icons with CSS masks.

Why inline an SVG at all
Small graphics such as arrows, checkmarks, dividers and form icons are often only a few hundred bytes. Requesting each one as a separate file costs a round trip and a cache entry, and the icon can flash in late. A data URI embeds the file's content directly inside the stylesheet or HTML, so it arrives together with the code that uses it. For icons that appear on almost every page, that trade is usually worth it. For large illustrations it is not, because the data is repeated wherever it is used and cannot be cached separately.
Two encodings, very different results
A data URI starts with data:image/svg+xml and then carries the file either Base64 encoded or URL-encoded. Base64 turns every three bytes into four characters, so the result is about 33 percent larger than the source. It also scrambles the repeated patterns of the markup, which makes gzip compression less effective when the stylesheet is served compressed.
URL-encoding leaves the markup readable and only escapes the characters that would otherwise break the URI or the CSS around it. That makes it smaller in most cases, often by 15 to 25 percent before compression and more after. The catch is that the escaping must be correct. The percent sign must become %25, and the hash must become %23, or everything after a colour like #2563EB is treated as a fragment and the icon renders black or not at all. Angle brackets and curly braces should be escaped for safety, and non-ASCII text must be encoded as UTF-8 bytes. Double quotes are a special case: swapping them for single quotes inside the SVG is cleaner than escaping them, and it lets the whole URI sit inside url("...").
Clean the file first
Design tools export plenty of data the browser never needs: XML declarations, editor comments, metadata, private namespaces and coordinates with six decimal places. Removing these and rounding numbers to two decimals usually halves an exported icon without any visible change. Keep the viewBox, because it makes the icon scale, and add the xmlns attribute if it is missing, since browsers require it when an SVG is used as an image rather than inline markup.
Recolouring with a mask
A background image cannot inherit the text colour, which is awkward for icons in buttons that change colour on hover or in dark mode. The trick is to use the SVG as a mask instead. The mask supplies only the shape, and the element's background-color fills it. Set that to currentColor and the icon follows the text colour automatically. This works for single-colour icons; multi-colour artwork should keep its own fills.
A short workflow
Open the SVG to Data URI and Base64 Encoder and drop your exported file. Leave the default minify settings on, check the preview on light and dark backgrounds, and raise the precision if any curve shifts. If the icon should follow text colour, set its main colour to currentColor and copy the mask snippet. Otherwise copy the background-image line. Compare the byte and gzip sizes of both encodings for your actual file before choosing; for a few e-mail clients and older tools Base64 remains the only accepted form.
The decode tab is handy when you inherit a stylesheet full of long data URIs. Paste one, see the icon, and get readable SVG back so you can edit it or move it to a proper file.
When not to inline
Inline only what is small and used widely. Anything over a few kilobytes, anything that changes often, and anything used on a single page is better served as a normal cached file. Data URIs in CSS also block rendering until the stylesheet is parsed, so keep the critical stylesheet lean.



