SVG to Data URI & CSS Background Converter
Paste SVG, get the shortest working data URI and snippets for CSS, HTML, Sass and JSX.
Try before you buy.
- Free preview: the sizes, what was fixed, a watermarked preview of the drawing and the first lines of each snippet; the data URI, copying and downloads stay locked.
- Locked until you unlock it: download and copy.
- Unlock: Pro pass, ₹179 for 30 days, a one-time payment that never renews.
Ways to unlock shows how to get the full result.
Printing this result is locked in the free preview.
Your SVG
Paste the code from <svg to </svg>, or drop an .svg file on this panel. Nothing is uploaded.
Data URI
Paste SVG code, or select Example.
background-image at 16, 24, 48 and 96 pxmask-image filled with the mask colour, as currentColor does: only the shape counts, its colours do notSizes
| Form | Bytes | gzip |
|---|
gzip is how a server usually sends CSS and HTML: it shows what the data URI really adds to the download.
What changed
Check
Locked in the free preview. Opens the ways to unlock this result.
Locked in the free preview. Batch runs unlock with a pass.
Locked in the free preview. Query results unlock with a pass.
About the SVG to Data URI & CSS Background Converter
Turn SVG code into a data URI you can put straight into CSS or HTML — background-image: url("data:image/svg+xml,…") — so a small icon, arrow, divider or pattern needs no separate file and no extra request. The converter makes the shortest form that still works: instead of Base64, which makes everything a third larger, it percent-encodes only the characters that have to be encoded (%, #, <, > and the few that CSS, HTML, Sass, Less and JSX read specially), switches the SVG’s attribute quotes so they never clash with the CSS string, and writes every colour in its shortest spelling — #ff0000 becomes red, because # costs three characters once it is encoded as %23.
It also fixes what stops an SVG from showing in a data URI: a missing xmlns, a missing viewBox, HTML entities such as , scripts, editor data and comments. You get ready snippets for CSS background-image, a mask-image icon that takes the text colour, list-style-image, a custom property, an **HTML <img>, Sass, Less and JSX**, with a preview at four sizes and a size comparison. Everything runs in your browser; your SVG is never uploaded. Without a pass the free preview shows the sizes, a watermarked preview and the first lines of each snippet.
How to use it
- Paste SVG code into the box, open an
.svgfile, or select Example. You can also paste a data URI, an<img>tag or a CSS rule that contains one: it is decoded and encoded again. - Choose the Encoding — URL-encoded (shortest for SVG) or Base64 — and the Quotes your CSS uses,
url("…")orurl('…'). - To recolour, choose currentColor (for inline SVG and for the mask snippet) or One colour to build a colour into the image: a CSS background cannot take the colour of the text around it.
- Pick the snippet you need under Output and check the preview at 16 to 96 pixels. With a Pro pass, or once this result is unlocked, Copy and Download give it in full; in the free preview they open the ways to unlock.
- Read What changed to see what was cleaned and fixed, and Check for anything that cannot work inside a data URI, such as linked files or web fonts.
Examples
The Example: an <?xml?> declaration, a comment, Inkscape data, width and height but no viewBox, stroke="#FFFFFF" — 473 bytes
data:image/svg+xml,%3csvg width='24' height='24' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3e%3ccircle … fill='%232563eb'/%3e%3cpath … stroke='white' …/%3e%3c/svg%3e — 296 characters (Base64: 374)
The declaration, the comment and the editor data go, a viewBox is added from width and height, and #FFFFFF is written white: 5 characters instead of 6 for %23fff. After gzip the URL-encoded form is 216 bytes, the Base64 one 308.
A stroke icon (stroke="currentColor"), Output: CSS mask-image
.icon { display: inline-block; width: 1em; height: 1em; background-color: currentColor; -webkit-mask: url("data:…") center / contain no-repeat; mask: url("data:…") center / contain no-repeat; }The icon becomes the shape of a box painted in the text colour, so it follows hover states and dark mode — which a background-image cannot do.
background-image: url("data:image/svg+xml;base64,PHN2Zy…")The SVG inside is decoded and written as a URL-encoded data URI, usually several kilobytes lighter for large icons and always readable.
Common uses
- Icons, arrows, checkmarks and bullets in a stylesheet, without extra files or requests.
- Custom select arrows, checkbox ticks and list bullets in CSS-only components.
- Colourable icons through mask-image and currentColor, for buttons, links and dark mode.
- Sass and Less variables, CSS custom properties and React style objects that carry an icon.
- Decoding a data URI found in someone’s CSS to read or edit the SVG inside it.
Why URL-encoding beats Base64 for SVG
Base64 turns every 3 bytes into 4 characters, so it adds a third, and it hides the repeated markup that gzip compresses well. SVG is already text, so most of it can stay as it is. A data URI is percent-decoded before use (RFC 2397; the WHATWG data: URL processor), so only these must change:
%, written%25, because it starts an escape;#, written%23, because the browser reads everything after it as a fragment and drops it — an unencoded#fffcuts the image off;- the quote mark around the CSS string, so the SVG’s own attributes switch to the other quote;
- line breaks, which URL parsers delete (they become spaces), and every character outside ASCII (encoded as UTF-8).
This converter also encodes < and > (older browsers needed it) and { } [ ] | ^ \ , the back-tick and &, so the same string is safe in Sass and Less (no interpolation), in JSX and HTML attributes (no entities) and in JavaScript template literals. Spaces, =, :, /, commas and parentheses stay readable.
Fixes for an SVG that shows nothing
- The namespace. An SVG pasted into an HTML page draws without
xmlns="http://www.w3.org/2000/svg", but as an image it shows nothing. It is added if missing. - The viewBox. Without one, CSS cannot scale the drawing:
background-size: containor a new width only crops or pads it. When width and height are plain numbers,viewBox="0 0 width height"is added. - Entities. XML knows only
&,<,>,"and'; HTML ones such as are written as numeric references ( ) and a lone&as&. - Scripts and links. Scripts never run in an SVG used as an image, and it never loads other files, so
<script>, event handlers andjavascript:links are removed, and linked images, style sheets and web fonts are reported. - Clean-up. Comments (except licence notices),
<metadata>, editor data from Inkscape, Illustrator or Sketch, unused namespaces and white space between elements are removed; text keeps its spaces.
currentColor, masks and colours
An SVG in a CSS background is a separate image: currentColor inside it means its own color (black unless set), not the colour of your text. To colour an icon from CSS, use the mask-image snippet: the SVG becomes the shape of a box filled with background-color: currentColor, so it follows the text colour, hover states and dark mode. The snippet includes the -webkit- prefixed form for older browsers. To build one colour into the image instead, choose One colour: fills and strokes change, in attributes, style attributes and <style> sheets; none, gradients and patterns are kept.
When not to inline
A data URI is not cached on its own: it travels with every copy of the stylesheet or page that contains it. Inline small images that are used where they are defined; for larger drawings (more than a few kilobytes) or icons used across many pages, a separate file or a sprite is usually faster. If your site sends a Content-Security-Policy, img-src must allow data: for these images and backgrounds to load.
Limitations
- Path data and numbers are not rewritten; tick Optimise with SVGO first for that (it also runs in your browser).
- Recolouring changes fill and stroke colours; gradient stops, filters and embedded bitmaps keep their own colours.
- Text in the SVG can use only the fonts installed on the visitor’s device; for an exact look, convert text to paths in your editor before converting.
- Very large SVGs (hundreds of kilobytes) are slow to preview and make poor data URIs.
- In the free preview, each snippet shows only its first half of lines — the data URI itself is one line, so it stays hidden — and copying and downloads stay locked until the result is unlocked.
Privacy
Everything happens in your browser. What you enter or open here is not uploaded or stored by MySmartCoPilot.
Frequently asked questions
What do I get without a pass?
Without a pass, SVG to Data URI & CSS Background Converter shows the sizes, what was fixed, a watermarked preview of the drawing and the first lines of each snippet; the data URI, copying and downloads stay locked. Until you unlock it, the result can’t be downloaded or copied. A Pro, Premium or Ultimate pass, a one-time payment that never renews, unlocks the full result. The pricing page lists the passes and their prices.
How do I use an SVG as a CSS background?
Convert it here and use the CSS background-image snippet: background-image: url("data:image/svg+xml,…"); with background-size: contain and background-repeat: no-repeat to fit it in the box. Make sure the SVG has a viewBox, or it will not scale.
Should I use Base64 or URL-encoding for SVG?
URL-encoding. SVG is text, so most of it can stay as it is: the result is usually shorter than Base64, which adds a third, and it compresses better with gzip. Base64 is the right choice for binary images such as PNG and JPEG.
Why does my SVG data URI not show up?
The usual causes are a missing xmlns="http://www.w3.org/2000/svg", an unencoded # (a colour such as #fff cuts the URI short), a quote mark that ends the CSS string early, or an XML error such as an unclosed tag. This converter fixes the first three and reports errors with their line and column.
Can I change the colour of a background SVG with CSS?
Not through background-image: the SVG is a separate image and cannot see your page’s colours. Use the mask-image snippet, which paints the icon in currentColor, or build a colour into the SVG with One colour.
Does the data URI work in Sass, Less and React?
Yes. The characters those languages read specially — #{ and @{ interpolation, braces, back-ticks and & — are encoded, so the snippets can be pasted as they are into a Sass or Less variable, a JSX attribute or a template literal.
Is my SVG uploaded anywhere?
No. Parsing, cleaning, the optional SVGO optimisation, the preview and the sizes are all worked out in your browser.