PureDevTools

SVG to CSS Background Converter

Convert SVG to CSS background-image data URI — URL-encode or Base64, with live preview

All processing happens in your browser. No data is sent to any server.

Options

Encoding Method

URL-encoded SVGs are smaller and human-readable in the source.

Minify SVG before encoding(removes comments, collapses whitespace)

Paste SVG code above to generate the CSS background-image data URI, CSS shorthand, and HTML <img> tag. Use Load Sample to try it out.

You want to use an SVG pattern as a CSS background-image without an extra HTTP request. The SVG needs to be URL-encoded into a data:image/svg+xml,... URI, but manually encoding <, >, ", and # characters is error-prone — miss one # in a color value and the entire background breaks silently.

Why This Converter (Not the SVG Optimizer)

PureDevTools has an SVG Optimizer for minifying SVGs. This tool converts SVG code to a CSS-ready background-image data URI — with URL encoding or Base64 encoding, optional SVG minification, and configurable background-size, background-repeat, and background-position. Generates CSS background-image, background shorthand, and HTML <img> tag. Everything runs in your browser.

What Is an SVG Data URI?

A data URI (also called a data URL) is a scheme that lets you embed file content directly inside a URL string. For SVG files, the data URI looks like this:

data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'...%3E%3C/svg%3E

Or in Base64 format:

data:image/svg+xml;base64,PHN2ZyB4bWxucz0...

You can use this data URI anywhere a URL is expected — in CSS background-image, HTML <img> tags, or even inline styles.

Why Embed SVG as a CSS Background?

No Extra HTTP Requests

Each image file your browser loads requires a separate HTTP request. For small SVG icons and decorative elements, embedding them as CSS data URIs eliminates these requests entirely. This reduces round-trip latency and speeds up page rendering — especially on high-latency connections.

Inline Caching

CSS files are cached aggressively by browsers. When your SVG is embedded in CSS, it inherits the CSS file’s cache lifetime automatically, without needing to configure separate cache headers for image files.

Dynamic Theming

SVG data URIs work seamlessly in CSS custom properties (var(--icon)), making it easy to swap SVG icons based on theme or state without JavaScript.

Privacy-Friendly

Unlike external SVG files hosted on CDNs, embedded data URIs never make requests to third-party servers. This is useful for cookie-consent and privacy policy compliance.

URL-Encoding vs Base64

URL-encoded SVG replaces special characters like <, >, #, and { with percent-encoded equivalents (%3C, %3E, %23, %7B). Double quotes are replaced with single quotes to avoid breaking CSS string delimiters.

Advantages:

Example:

background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'...");

Base64

Base64 encodes the binary representation of the SVG using a 64-character alphabet. The result is always ASCII-safe and universally parseable.

Advantages:

Disadvantages:

Example:

background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...");

SVG Minification

Before encoding, this tool can minify your SVG by:

Minification reduces the encoded size of the data URI, which directly reduces your CSS file size.

CSS background Properties

The tool generates three types of CSS output:

background-image Property

The most specific property — sets only the image source:

background-image: url("data:image/svg+xml,...");

Use this when you want to add a background image to an element that already has other background-* properties set separately.

background Shorthand

The shorthand combines image, position, size, and repeat in one declaration:

background: url("data:image/svg+xml,...") center / contain no-repeat;

The order is: image position / size repeat. This is the most concise way to set all background properties at once.

CSS Class Block

A ready-to-paste CSS class:

.icon {
  background: url("data:image/svg+xml,...") center / contain no-repeat;
}

Common background-size Values

ValueDescription
containScale the image to fit inside the element, maintaining aspect ratio
coverScale the image to fill the element, maintaining aspect ratio (may crop)
autoUse the image’s intrinsic size
100% 100%Stretch to exactly fill the element (may distort)
32px 32pxFixed pixel size
50%Half the width of the element

Practical Use Cases

SVG Icons as CSS Backgrounds

.icon-clock {
  background: url("data:image/svg+xml,...") center / contain no-repeat;
  width: 24px;
  height: 24px;
  display: inline-block;
}

Custom Checkbox or Radio Inputs

input[type="checkbox"]:checked {
  background-image: url("data:image/svg+xml,%3Csvg...%3E");
}

Decorative Patterns with repeat

.patterned-bg {
  background: url("data:image/svg+xml,...") 0 0 / 40px 40px repeat;
}

CSS Custom Properties for Theming

:root {
  --icon-arrow: url("data:image/svg+xml,...");
}

.arrow { background-image: var(--icon-arrow); }

Browser Support and Compatibility

SVG data URIs as CSS backgrounds are supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Internet Explorer 9+. There are no polyfills or feature flags required.

Note: Some older versions of Internet Explorer have a 32KB limit on data URI sizes. For production, ensure your encoded SVG stays well below this limit.

Performance Considerations

For icon sets, consider using an SVG sprite sheet or icon font library instead of individual data URIs.

Related Tools

More CSS Tools