URL-Encoded • Base64 • CSS Mask • 100% Client-Side

Online SVG to CSS & Data URI Generator

Convert raw SVG vector code or upload .svg files into URL-safe CSS background-images, Base64 strings, CSS masks, or HTML tags. Zero server uploads, instant minification.

Sample SVG IconsClick to Load
Raw SVG Code
URL-Encoded: 308 BBase64: 378 B⚡ 18.5% lighter
Live Visual Preview
Rendered at 24×24px via CSS background-image
Recommended: Smaller & Gzip-friendly
/* CSS background-image (URL-Encoded - Recommended) */
.icon-element {
  width: 24px;
  height: 24px;
  display: inline-block;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2310b981' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M22 11.08V12a10 10 0 1 1-5.93-9.14'%3E%3C/path%3E%3Cpolyline points='22 4 12 14.01 9 11.01'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

Complete Engineering Guide to Embedding SVGs in CSS: Data URIs, Masking & Performance

Scalable Vector Graphics (SVG) are the gold standard for web iconography, UI badges, logos, and illustrative textures. Because SVGs are defined mathematically via XML paths rather than fixed raster bitmaps, they scale infinitely with zero pixelation on 4K, 5K, and mobile Retina screens.

However, serving hundreds of individual .svg files introduces substantial HTTP network overhead. Even under HTTP/2 multiplexing, each separate request incurs round-trip DNS lookups, TLS renegotiation, and TCP congestion window warming. Inlining SVGs directly into CSS stylesheets using Data URIs eliminates separate round trips entirely.

URL Encoding vs Base64 Encoding: Why Base64 is Wasteful for SVGs

A widespread misconception among web developers is that all Data URIs must use Base64 encoding. While Base64 is mandatory for binary assets (such as PNGs or WebPs), SVGs are plain UTF-8 text documents.

Base64 Overhead
  • Increases raw byte size by ~33% due to 6-bit mapping.
  • Generates pseudo-random strings that compress poorly with Gzip and Brotli.
  • Cannot be visually inspected or edited in CSS source.
UTF-8 URL-Encoding Advantages
  • Preserves the original XML structure with minimal character escaping.
  • Compresses up to 70% better with Brotli dictionary matching.
  • Completely human-readable in browser DevTools.

The Infamous "%23" Hash Bug in CSS Data URIs

The most frequent error developers encounter when creating inline SVG CSS is invisible or broken icons. In 99% of cases, this is caused by unescaped hash symbols (#) used in CSS hex colors (e.g. fill="#4f46e5"). In URI syntax, the hash character designates a fragment identifier anchor, causing Chrome, Safari, and Firefox to truncate the SVG data. Always replace # with %23 in your Data URIs.

Dynamic Theming with CSS mask-image

By using -webkit-mask-image and mask-image, you can use the SVG geometry as an alpha mask, allowing you to change icon colors dynamically on hover or across dark/light modes simply by setting background-color: currentColor!

Frequently Asked Questions (SVG to CSS FAQs)

Embedding SVGs directly into CSS using Data URIs eliminates individual HTTP network requests. This removes round-trip latency, avoids Flash of Unstyled Content (FOUC), guarantees zero Cumulative Layout Shift (CLS), and ensures vector icons render razor-sharp on high-DPI (Retina) displays without requiring external asset management.

Base64 encoding increases binary file size by approximately 33% because 3 bytes are mapped onto 4 characters. In contrast, UTF-8 URL encoding only escapes essential characters (such as '%', '#', '<', '>'), resulting in a significantly smaller payload. Furthermore, modern Gzip and Brotli compression algorithms achieve higher compression ratios on raw text SVGs than on pseudo-random Base64 strings.

In URLs, the '#' character designates a fragment identifier (an anchor). When a browser parses 'background-image: url("data:image/svg+xml,...#fff...")', it treats everything after '#' as a hash anchor rather than image data, causing rendering failure. In URL-encoded SVGs, '#' must be replaced with '%23' to ensure compatibility across Chrome, Safari, and Firefox.

The 'mask-image' technique uses an SVG Data URI as an alpha clipping mask over a container element. By applying 'mask-image: url(...)', you can change the icon's color dynamically on hover or across dark/light themes simply by altering 'background-color: currentColor' without editing the underlying SVG code.

When SVGs are embedded into your primary stylesheet, they share the stylesheet's HTTP cache lifetime. As long as your CSS bundle is cached, the embedded SVG assets are loaded instantly from browser memory with zero network overhead.

Yes, 100% free and completely private. All minification, URL encoding, Base64 synthesis, and preview generation execute client-side in your browser using JavaScript. No graphics or code snippets are ever uploaded to any remote server.

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Developer Tools