Home › Developer Tools › SVG to CSS Data URI

SVG to CSS Data URI

Paste SVG code or open an .svg file to get a compact, URL-encoded data URI ready for CSS background-image, mask-image or an img tag. The tool minifies the SVG, encodes only the characters that need it, compares the size with Base64 and shows a live preview.

Last updated:

Preview

Size comparison

Advertisement
Advertisement

How to use the SVG to CSS Data URI

  1. Paste your SVG code into the box, or open or drop an .svg file.
  2. Choose the encoding: optimized URL encoding (usually smallest) or Base64.
  3. Pick the output: CSS background-image, CSS mask, plain data URI or an HTML img tag.
  4. Check the preview and size bars to confirm the icon renders and see how much smaller URL encoding is than Base64.
  5. Copy the code into your stylesheet or HTML, or download it.

Why URL-encoded SVG beats Base64

Base64 turns every 3 bytes into 4 characters, so a Base64 data URI is always about 33% larger than the SVG. URL encoding leaves letters, digits, spaces and most punctuation untouched and only escapes a handful of characters, so a typical icon grows by just a few percent. Because the result is still readable text, gzip and Brotli also compress it better.

CharacterEncoded asWhy
#%23Would start a URL fragment and cut the SVG off, a common cause of broken hex colors
%%25Starts a percent-escape
< >%3C %3ENot allowed unescaped in URLs; older browsers reject them
"' or %22Would end url("…"); attributes are switched to single quotes
Line breaksremoved or %0ANot allowed inside a CSS string
{ } | \ ^ ` [ ]percent-encodedUnsafe in URLs
é, emoji and other non-ASCIIUTF-8 percent bytesData URIs are ASCII-only

Background image or mask?

  • background-image draws the SVG with its own colors. Use it for multicolor illustrations, patterns and decorative shapes.
  • mask-image uses the SVG only as a stencil and fills it with the element's background-color. Set background-color: currentColor and the icon follows the text color, hover states and dark mode, which a background image cannot do. Include the -webkit-mask line for older Safari and Chromium versions.

Tips for small, reliable data URIs

  • Run large SVGs through an SVG optimizer first: removing editor data and rounding path coordinates often halves the size.
  • Keep a viewBox so the image scales cleanly with background-size.
  • Write colors as short hex (#fff) or named colors: every # costs three characters once encoded.
  • Reuse the same data URI through a CSS custom property, e.g. --icon: url("…"), instead of repeating it in many rules.

Frequently asked questions

How do I use an SVG as a CSS background image?

Convert the SVG to a data URI and put it in url(): background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' …%3E…%3C/svg%3E"). Paste your SVG above and copy the ready-made rule. The SVG must include xmlns="http://www.w3.org/2000/svg", which the tool adds if it is missing.

Is URL encoding or Base64 better for SVG data URIs?

URL encoding is usually better. SVG is text, so percent-encoding only a few characters keeps it readable and typically 20% to 30% smaller than Base64, which always adds 33%. It also compresses better with gzip or Brotli because the text stays repetitive. Base64 is mainly useful when a system does not accept special characters in URLs.

Which characters must be encoded in an SVG data URI?

At minimum % and # (the hash would start a URL fragment, so fill="#f00" must become %23f00), plus < and > and the quote character used around the URL. Line breaks should be removed or encoded. This tool also encodes { } | \ ^ ` [ ] and non-ASCII characters for safety, and swaps double quotes in attributes for single quotes.

Why doesn't currentColor work in an SVG background image?

An SVG used as a background-image or img is rendered as a separate isolated image, so it cannot inherit CSS like color, fonts or hover states from your page. To recolor an icon with CSS, use it as a mask-image instead and set background-color on the element, which the CSS mask output does.

Why does my SVG data URI not show up?

The usual causes are a missing xmlns attribute, an unencoded # in a color, unescaped double quotes inside url("…"), or invalid XML. External images, fonts and scripts inside the SVG are also blocked when it is used as an image. The preview and render check above flag these problems.

Does a data URI make my page faster?

For small icons it saves an HTTP request and avoids a flash while the icon loads. For large or repeated images it can be slower, because the data URI is downloaded again inside every stylesheet or page that contains it and cannot be cached separately. Keep inline SVGs to a few kilobytes.

Is my SVG uploaded?

No. Minifying, encoding and previewing happen entirely in your browser, so your artwork stays on your device.

Are all the tools on FreeToolCabin tested for quality and accuracy?

Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

Related tools

SVG OptimizerMinify SVG files: strip metadata, comments and editor junk, round numbers, compare sizes.File to Base64Convert any file to a Base64 string or data URI with MIME detection, line wrap and download.Image to Base64Convert images to Base64 data URIs, CSS backgrounds and HTML img tags in your browser.URL Encoder DecoderPercent-encode or decode URLs, query strings and form data.BlurHash GeneratorEncode an image to a BlurHash or ThumbHash placeholder, preview it and copy ready-made code.CSS Shape Divider GeneratorCreate wave, tilt, curve and triangle section dividers and copy the SVG, HTML and CSS.Free Icon Search and DownloadSearch free open-source icons (Lucide, Tabler, Material), recolor, resize and get SVG or PNG.SVG EditorEdit SVG files visually: move shapes, change colors, text and strokes, add shapes, edit the code.Open Graph PreviewPaste OG tags and preview how links look on Facebook, X and LinkedIn.YouTube Embed Code GeneratorCreate responsive YouTube embed code with start time, autoplay, loop and privacy mode.CSS Box Shadow GeneratorDesign layered CSS box shadows with a live preview, presets and one-click Copy CSS.CSS Border Radius GeneratorRound each corner, draw elliptical 8-value radii or random blob shapes and copy the CSS.CSS Button GeneratorDesign CSS buttons with gradients, borders, shadows and hover and active states, then copy the code.CSS Flexbox GeneratorBuild flexbox layouts visually: set container and item properties, then copy the CSS and HTML.Twitter Card GeneratorCreate X / Twitter Card meta tags with Open Graph fallbacks, character counters and a live preview.CSS Grid GeneratorDraw CSS grid layouts: set tracks and gaps, drag to name areas, then copy the CSS and HTML.PX to REM ConverterConvert px to rem or em and back, with a 1-100px table and a whole-CSS converter.Glassmorphism CSS GeneratorDesign frosted-glass UI cards with blur, transparency and border sliders, then copy the CSS.CSS Text Shadow GeneratorLayer CSS text shadows with a live preview and neon, 3D, outline and retro presets.HTML Editor (Live Preview)Write HTML, CSS and JavaScript and see a live, sandboxed preview as you type.HTML to JSX ConverterConvert HTML to valid React JSX: className, htmlFor, style objects, self-closing tags and events.CSS Clip-Path GeneratorDraw CSS clip-path shapes by dragging points: polygons, circle, ellipse and inset. Copy the CSS.CSS Animation GeneratorCreate CSS keyframe animations from 23 presets with live preview, timing controls and Copy CSS.CSS Triangle GeneratorMake CSS triangles in 8 directions with the border trick or clip-path. Set size and color, copy CSS.CSS Loader GeneratorPick from 14 pure CSS spinners and loaders, set color, size and speed, then copy the HTML and CSS.Cubic Bezier GeneratorDrag the control points to design a CSS easing curve, compare it with linear and copy cubic-bezier().CSS Filter GeneratorAdjust blur, brightness, contrast, hue, sepia and drop-shadow on an image and copy the CSS filter.CSS Specificity CalculatorCalculate CSS selector specificity (a, b, c), including :is(), :not(), :has() and :where(), and compare.Google Maps Embed GeneratorTurn an address or coordinates into responsive Google Maps iframe embed code, no API key.Neumorphism CSS GeneratorDesign soft UI neumorphic shapes with live preview and copy the box-shadow CSS.SVG Wave GeneratorMake layered SVG waves for section dividers and export SVG, PNG or a CSS background.SVG Blob GeneratorGenerate random organic SVG blob shapes with gradients and export SVG, PNG or CSS clip-path.CSS Clamp GeneratorTurn min/max sizes and viewport widths into a fluid CSS clamp() value in rem and vw.CSS @font-face GeneratorTurn font files or URLs into ready-to-paste @font-face rules with weights, styles and a preview.CSS Media Query GeneratorBuild @media rules for widths, orientation, dark mode and hover, or get framework breakpoints.CSS Scrollbar GeneratorStyle scrollbars: width, colors, radius and hover, with ::-webkit-scrollbar and scrollbar-color CSS.CSS Sprite GeneratorPack many icons into one sprite sheet PNG and get CSS classes with background-position.CSS Text Gradient GeneratorMake gradient text with background-clip:text: color stops, angle, font preview and fallback CSS.CSS Gradient Animation GeneratorCreate animated gradient backgrounds: pick colors, angle and speed, preview live and copy the CSS.CSS Hover Effects GeneratorPick button, link and image hover effects, tweak colors and timing, and copy the HTML and CSS.CSS InlinerMove CSS from style blocks into inline style attributes so HTML emails render in every client.CSS Transform GeneratorRotate, scale, skew and translate an element with sliders and copy the CSS transform code.Font Pairing ToolPreview Google Font heading and body pairs on real layouts and copy the embed code and CSS.HTML Accessibility CheckerPaste HTML to find WCAG accessibility problems with impact level, the failing element and a fix.HTML Image Map GeneratorDraw clickable rectangle, circle and polygon hotspots on an image and get image map HTML.SVG ViewerOpen or paste SVG code to preview it live with zoom, backgrounds, size info and PNG export.Pathfinding VisualizerAnimate BFS, DFS, Dijkstra, A* and greedy search on a grid with walls, weights and mazes.CSS Toggle Switch GeneratorDesign pure-CSS toggle switches: size, colors, shape and animation. Copy accessible HTML + CSS.CSS Tooltip GeneratorBuild pure CSS tooltips: position, arrow, colors, padding and animation. Copy HTML and CSS.HTML Form BuilderDrag and drop form fields with labels and validation, then copy accessible HTML and CSS code.Iframe GeneratorCreate iframe embed code: size or responsive ratio, border, allow, sandbox, lazy load, live preview.PX to VW ConverterConvert pixels to vw and vh for any viewport size, and vw/vh back to px, with breakpoint tables.SVG Path EditorEdit an SVG path d attribute visually: drag points, edit commands, convert, scale and minify.SVG to JSXTurn SVG code into a clean React component: camelCase attributes, style objects, TypeScript.Tailwind CSS Cheat SheetSearchable Tailwind class to CSS reference with live previews, class decoder and color palette.Web App Manifest GeneratorCreate a PWA manifest.json and every app icon size from one image, downloaded as a ZIP.CSS Background Pattern GeneratorMake pure-CSS stripes, dots, checkerboard, zigzag and grid backgrounds and copy the code.CSS Color Filter GeneratorTurn a HEX color into a CSS filter that recolors black icons and SVGs, with an accuracy score.
See all 52 Web, HTML & CSS Tools →