An SVG data URI places vector markup directly inside a CSS value instead of referring to a separate image file. This can be convenient for small icons, self-contained components, list markers, and masks that should travel with a stylesheet.
Paste SVG markup, choose an encoding and destination property, then copy the generated declaration. The preview and byte comparison update in the browser as the input changes.
How to Use the SVG to CSS Converter
Generate a data URI and wrap it in the CSS form your component needs.
- Paste the source into SVG markup and check Preview.
- Under Encoding, choose URL-encoded (smaller) or Base64.
- Under CSS property, choose background-image, mask-image (recolor with background-color), list-style-image, content (::before / ::after), or Data URI only.
- Review the generated value in CSS and the SVG-to-data-URI byte comparison below it.
- Select Copy to copy the current CSS declaration or bare data URI.
How SVG Data URIs Work in CSS
A data URI combines a media type and the resource data in one string. This tool produces an image/svg+xml URI, then places it inside url("...") for the selected CSS property. Data URI only returns the URI without url() or a property name.
For example, a background result has this shape:
.icon {
background-image: url("data:image/svg+xml,%3Csvg...");
}
Embedding avoids a separate image URL and makes a small component easier to copy. It also moves the bytes into the stylesheet, where they cannot be cached or updated independently from that CSS. Repeating the same long URI in many rules can cost more than serving one reusable image asset.
The background-image output sets only the image; size, position, repeat behavior, and element dimensions remain your responsibility. The preview uses a centered, contained, non-repeating background, but those preview styles are not included in the copied background declaration.
URL-Encoded SVG Compared with Base64
The URL-encoded option first trims the SVG, removes an initial XML declaration and comments, removes whitespace between tags, and collapses remaining whitespace runs. It adds the SVG namespace when the opening element does not contain one, changes double quotes to single quotes, and percent-encodes characters that are unsafe in the generated URI.
That light cleanup often makes text-based SVG data smaller and partly readable in CSS. It is not a general SVG optimizer: it does not simplify paths, combine shapes, remove unused definitions, or calculate whether an attribute is redundant. Because whitespace is collapsed globally, inspect SVGs whose text content intentionally depends on repeated whitespace.
The Base64 option encodes the trimmed original markup rather than applying the URL option's comment and whitespace cleanup. The tool still checks that the input resolves to an SVG root before producing the Base64 result. Base64 uses a restricted character set but typically expands the source data, so compare the displayed byte counts instead of assuming it is smaller.
The status compares the browser byte size of the original textarea value with the complete generated data URI. It does not include the surrounding CSS declaration, selector, or stylesheet compression. Production transfer size can differ after HTTP compression.
Backgrounds, Masks, Markers, and Generated Content
Choose mask-image (recolor with background-color) when the SVG shape should take on a CSS color. The generated output includes both -webkit-mask and mask, centers and contains the image without repeating it, and sets background-color: currentColor. The element still needs dimensions for the mask to be visible.
The mask output treats SVG transparency as the masking shape, so it is better suited to monochrome icons than artwork whose internal colors must remain. currentColor inherits from the element's color; override background-color if a different fill is required.
list-style-image uses the data URI as a list marker, but marker sizing and alignment are controlled by browser list rendering and provide less flexibility than a dedicated pseudo-element. content (::before / ::after) emits a content: url(...) declaration intended for generated content. It does not create the ::before or ::after selector or set content on an ordinary element for you.
For a regular image that needs independent caching, responsive selection, accessibility text, or frequent reuse, an external SVG file or inline SVG element may be more maintainable. Use the HTML preview to test surrounding markup and styles, or the image resizer when the source is a raster image rather than SVG.
Validation, Security, and Browser-Local Processing
The converter requires input that starts with an <svg> element after its limited initial cleanup. It reports an error otherwise and clears the output and preview. This is only a root-element check; the tool does not parse the document for complete XML validity or verify every SVG attribute.
It also does not sanitize SVG. Markup can contain links, references, metadata, or active features whose treatment depends on where and how the SVG is later embedded. Only convert trusted artwork, and apply your application's content security policy and SVG sanitization rules before publishing user-supplied markup.
Conversion and preview generation run locally in your browser; the pasted markup is not uploaded for processing. Even so, inspect confidential design assets before sharing the copied CSS, because the data URI contains recoverable SVG source rather than encryption.
A site's Content Security Policy can restrict data: images. If the generated rule does not render in an application, check the relevant img-src directive and the destination's CSS handling. Do not weaken a production policy broadly just to accommodate one icon; an external same-origin asset may be the cleaner choice.
Frequently Asked Questions
Is URL-encoded SVG smaller than Base64?
It often is for compact SVG markup, and this tool labels it as the smaller option. The actual result depends on the source and subsequent stylesheet compression. Use the displayed data-URI byte count for this conversion, then measure the final compressed asset when transfer size matters.
Does the converter optimize SVG paths?
No. URL encoding removes an initial XML declaration, comments, and excess whitespace, adjusts quotes, and adds a missing namespace. It does not rewrite path data, merge elements, remove unused definitions, or perform the broader transformations of an SVG optimizer.
Why does the mask output include -webkit-mask?
The generated mask result includes a prefixed declaration followed by the standard mask shorthand. Both use the same centered, contained, non-repeating data URI. It also sets background-color: currentColor, which supplies the visible color through the mask.
Can I change the color of the generated icon?
The mask option is intended for that use because its visible color comes from background-color, initially set to currentColor. A background image keeps the colors written inside the SVG markup. The converter does not rewrite SVG fill or stroke attributes for you.
Is the generated SVG data URI safe to use?
Encoding is not sanitization. The tool checks for an SVG root but does not audit the markup for unsafe references or active content, so use trusted SVG or sanitize it according to the destination context. The data URI also exposes the original artwork to anyone who can read the CSS.
References: W3C: Scalable Vector Graphics (SVG) 2 · MDN: Data URLs.