b2KIT

SVG to CSS Background Converter

Convert SVG graphics to CSS background-image data URIs with proper encoding and minification.

Tested tool guide Tested browser tools Checked August 16, 2026

What SVG to CSS Background Converter does, with a checked example

Give it an SVG file's markup and the tool returns a ready-to-use CSS declaration that draws the image from a data URI, no separate file needed. It compacts the markup into one line and percent-encodes every character that is unsafe inside a URI, then wraps the result in url("data:image/svg+xml,..."). The thing that trips people up is the hash in colors: fill="#ff0000" has to come out as %23ff0000, because an unencoded # marks the start of a URL fragment and the rest of the image silently disappears. The embedded image is fully offline and adds no HTTP request.

Worked example

A concrete input and expected output from the current implementation.

Input

<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><circle cx="5" cy="5" r="4" fill="#000"/></svg>

Expected output

background-image: url("data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%2210%22%20height=%2210%22%3E%3Ccircle%20cx=%225%22%20cy=%225%22%20r=%224%22%20fill=%22%23000%22/%3E%3C/svg%3E");

The drawing was already compact, so the only change is the encoding: the # in fill="#000" becomes %23 (an unencoded hash would start a URL fragment and cut the image short), quotes become %22, angle brackets become %3C and %3E, and spaces become %20, while letters, digits, / and : pass through unchanged.

How the result is produced

1

Parsing and encoding

The SVG is handled as plain text, not rendered: the markup is normalized first, with the XML declaration and comments removed and runs of whitespace collapsed so the whole drawing fits on one line. The encoding pass then replaces every character that is unsafe inside a URI with a percent escape - angle brackets, double quotes, the hash sign (the one that breaks colors), spaces and any literal % - while letters, digits, /, :, - and . stay readable as-is.

2

What minification does

Minification removes only text that cannot affect the drawing: the XML declaration, comments, and formatting whitespace, plus shortening color values where SVG defines an exactly equivalent shorter form, such as six-digit hex to three-digit. Coordinates, path data and fill values are preserved untouched, so the rendered image is pixel-identical to the source. The point is a smaller stylesheet: every byte saved by the minifier is a byte the page never downloads.

Good uses

  • Drop a small icon or logo into a single CSS file so the page makes one less HTTP request and the stylesheet works from a plain file with no build step.
  • Produce a self-contained HTML deliverable - a prototype, preview, or email piece - where an image file cannot travel alongside the document and everything must live in one file.
  • Embed a marker or decoration that must keep rendering when the stylesheet is copied to another project or host, since a data URI has no path that can break.

Limits and checks

  • The output is a snapshot: the data URI is text with no link back to the source SVG, so changing the design means editing the original markup and converting again. Keep the .svg file in the project or you can never regenerate the CSS later.
  • External references do not survive. If the SVG loads an image, font, or stylesheet through href or url() references, those files cannot load from inside a data URI; everything must be inlined in the markup before conversion or the embedded image renders incomplete.
  • Readability is gone and size grows. The encoded line is not for human reading - no one can tell what the image is by looking at it, and diffs of the CSS are noise. Percent-encoding also makes the output longer than the original file, and a few very old browsers refuse data URIs above a size limit, so very large drawings are better kept as files.

Common questions

Can it convert a PNG or JPG to a CSS background too?

No. The converter accepts SVG markup only, because the whole technique depends on SVG being text. A raster image is binary, and embedding it in CSS uses a different encoding - base64 - which yields a data:image/png;base64,... URI. Use a general base64 tool for that; this converter only answers SVG questions.

I converted my SVG and the background renders blank. What went wrong?

First confirm the SVG renders when opened in a browser tab, since the converter only preserves what is already valid. Then look for external references - images or fonts the SVG points to - which cannot load inside a data URI. Finally, make sure the url() line was not broken when you saved it: a line break inside the quoted string invalidates the declaration.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools