SVG to CSS Background: Convert SVGs to CSS Data URIs
Using SVG images as CSS backgrounds normally requires a separate HTTP request for each file. By converting your SVG code into a CSS data URI, you can embed the image directly in your stylesheet — eliminating the network request entirely. This is one of the simplest performance optimizations you can make for icons, patterns, and decorative elements.
Why Inline SVGs in CSS?
Every external image file your page loads requires a separate HTTP request. For sites with dozens of small icons or background patterns, these requests add up. Data URIs solve this by embedding the image data directly into the CSS, so the browser renders it without any additional network round trips.
Benefits of SVG Data URIs
- Fewer HTTP requests: Each inlined SVG is one less file to fetch
- No flash of missing images: The SVG loads with the stylesheet itself
- Simpler deployment: No need to manage separate image asset directories for small icons
- Cache-friendly: The SVG travels with the CSS file, cached together
- Easy theming: Modify SVG colors directly in the data URI string
When to Use This Approach
- Small icons used as CSS backgrounds (arrows, checkmarks, chevrons)
- Repeating background patterns (dots, stripes, waves)
- Decorative elements that are purely presentational
- Bullet-point list markers using
list-style-image - Form element styling (custom select arrows, checkbox marks)
URL Encoding vs. Base64: Which to Choose
Our tool supports two encoding methods, each with different tradeoffs.
URL-Encoded SVGs
URL encoding replaces special characters in the SVG markup with percent-encoded equivalents (e.g., # becomes %23, < becomes %3C). The result looks like this:
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' ...");
Advantages:
- Smaller file size (typically 20-30% smaller than Base64)
- Human-readable — you can still see the SVG structure in the CSS
- Easier to edit inline if you need a quick color change
Base64-Encoded SVGs
Base64 converts the entire SVG binary into an ASCII string:
background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDov...");
Advantages:
- Universal compatibility with all CSS processors and build tools
- No risk of encoding edge cases with unusual SVG content
- Works reliably in email templates where URL encoding can be inconsistent
Quick Comparison
| Factor | URL Encoding | Base64 |
|---|---|---|
| File size | Smaller | ~33% larger |
| Readability | Human-readable | Opaque |
| Compatibility | Excellent | Universal |
| Editability | Easy to tweak | Must decode first |
| Best for | Web stylesheets | Email, legacy tools |
Recommendation: Use URL encoding for web projects where file size matters. Use Base64 when maximum compatibility is required or when working with tools that have trouble with URL-encoded content.
How to Use Our SVG to CSS Tool
- Paste your SVG code into the input area — the raw SVG markup including the
<svg>tags - Choose the encoding method — URL encoding (recommended) or Base64
- Copy the generated CSS — a complete
background-imageproperty ready to use - Paste into your stylesheet and apply it to any element
Example Workflow
Start with an SVG icon:
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#6750A4" stroke-width="2">
<path d="M5 12h14M12 5l7 7-7 7"/>
</svg>
The tool converts it to a CSS-ready data URI that you can apply immediately:
.arrow-icon {
background-image: url("data:image/svg+xml,...");
background-repeat: no-repeat;
background-size: contain;
width: 24px;
height: 24px;
}
Practical Tips
- Always include the
xmlnsattribute in your SVG. Withoutxmlns="http://www.w3.org/2000/svg", some browsers will not render the data URI correctly. - Minify your SVG first. Remove comments, unnecessary whitespace, and editor metadata before converting. Smaller SVG markup means smaller data URIs.
- Replace hex colors with short forms where possible.
#fffinstead of#ffffffsaves a few bytes per occurrence. - Keep data URIs under 10KB. For larger SVGs, a separate file with proper caching is more efficient than an inlined data URI that bloats your stylesheet.
- Use CSS custom properties for theming. While you cannot use CSS variables inside data URIs directly, you can prepare multiple color variants of the same SVG and switch between them with class names.
When Not to Inline SVGs
Data URIs are best for small, frequently used graphics. Avoid this approach for:
- Large illustrations (above 10KB) — these are better served as external files with HTTP caching
- Animated SVGs — complex animations are easier to manage and debug as separate files
- SVGs that change frequently — updating an inlined SVG means rebuilding the CSS
- SVGs that need JavaScript interaction — data URI SVGs in CSS backgrounds cannot respond to DOM events
Frequently Asked Questions
Does inlining SVGs increase my CSS file size? Yes, but the tradeoff is worthwhile for small icons and patterns. The total transfer size often decreases because you eliminate HTTP request overhead. For a 500-byte SVG, the request headers alone can be larger than the image.
Will this work in all browsers? Data URIs for SVG backgrounds are supported in all modern browsers including Chrome, Firefox, Safari, and Edge. Internet Explorer 11 has a 4KB limit on data URIs in certain contexts.
Can I use this for CSS content property too?
Yes. You can use SVG data URIs in the content property of pseudo-elements (::before, ::after) with the url() function, though background-image is more common and flexible.
Do I need to escape quotes in the SVG? The tool handles this automatically. Single quotes within the SVG are preserved, and the outer URL uses double quotes (or vice versa) to prevent conflicts.
Try our free SVG to CSS Background tool to convert your SVGs to CSS data URIs instantly.
Try Ghost Image Hub
The Chrome extension that makes managing your Ghost blog images a breeze.
Learn More