background-image rules using data URIs. This eliminates the need for separate SVG files and reduces HTTP requests, making it ideal for icons, patterns, and decorative elements.
Use Cases
- Embedding icons as CSS backgrounds (no separate SVG files)
- Creating CSS-only patterns (stripes, dots, gradients)
- Reducing HTTP requests by inlining small SVG assets
- Dynamic styling - Modify SVG fill/stroke via CSS variables
- Single-file components - Keep SVG and CSS together
- Email templates - Inline SVG for maximum email client compatibility
How It Works
The tool takes SVG markup and generates a CSS rule with adata: URI:
- Parse SVG: Validates that input contains
<svg>element - Encode SVG:
- Base64 (default): Converts SVG to base64 string
- URL-encoded: Percent-encodes SVG with optimized character set
- Output CSS: Generates
.iconclass withbackground-imageproperty
URL-encoded data URIs are often smaller than Base64 for simple SVGs because Base64 has ~33% overhead, while URL encoding only encodes special characters.
Input Format
Any valid SVG markup:Output Format
Default (Base64)
URL-Encoded
Actions
Default (Base64)
Encodes SVG as base64 data URI. Best for:- Complex SVGs with many special characters
- When browser compatibility is critical (older email clients)
- Minified/compressed SVGs (base64 is consistent size)
URL-Encoded
Percent-encodes only special characters. Best for:- Simple SVGs (icons, shapes)
- Debugging (human-readable in DevTools)
- Smaller file size for basic SVGs
Examples
Technical Details
Located in
lib/tools/engine.ts:932-944Base64 Encoding (Default)
- Uses
encodeURIComponent+unescape+btoafor UTF-8 → Base64 - Overhead: ~33% size increase (3 bytes → 4 characters)
URL Encoding (Optimized)
- Encodes only special characters (
<>"#%{}|^[]) - Optimizes common SVG characters (
:,=,/) to reduce size
Size Comparison
| SVG Size | Base64 Size | URL-Encoded Size | Winner |
|---|---|---|---|
| 100 bytes | 136 bytes (+36%) | 110 bytes (+10%) | URL |
| 500 bytes | 668 bytes (+34%) | 530 bytes (+6%) | URL |
| 2 KB (minified) | 2.7 KB (+35%) | 2.1 KB (+5%) | URL |
| 5 KB (complex) | 6.7 KB (+34%) | 5.3 KB (+6%) | URL |
Rule of thumb: Use URL-encoded for simple SVGs. Use Base64 for complex SVGs or when targeting older browsers/email clients.
Usage Patterns
As Icon Background
As Repeating Pattern
With CSS Variables (Dynamic Colors)
Modify SVG colors using CSS custom properties:Mask-Image Alternative
For dynamic colors, usemask-image instead of background-image:
Browser Compatibility
- Data URIs: Supported in all modern browsers and IE8+
- URL-encoded SVG: Supported in all modern browsers
- Base64 SVG: Widest compatibility (use for email templates)
Optimization Tips
- Minify SVG first: Remove unnecessary attributes, comments, metadata
- Remove XML declaration:
<?xml ...?>is optional for inline SVG - Use shorthand attributes:
stroke-width="2"→stroke-width=2 - Combine paths: Use single
<path>instead of multiple shapes when possible - Avoid gradients/filters: They increase size significantly
Related Tools
- Base64 String Encode/Decode - Encode/decode base64
- URL Encode/Decode - URL encoding utilities
- HTML Beautify/Minify - Minify SVG before encoding
- CSS Beautify/Minify - Format generated CSS