What is an image to CSS converter?
Embedding an image in CSS means writing it as a data URI, so the stylesheet carries the picture and the browser makes no extra request. It is how small icons, patterns and decorative shapes end up in background, mask or a custom property. Doing it by hand is fiddly: the SVG must be cleaned, certain characters escaped and the quotes balanced. This tool does it for you. Drop an SVG, or a PNG, JPG, WebP or GIF, and it gives you ready-to-paste CSS with a live preview.
For SVG, the tool removes the XML prolog, the doctype and comments, adds the xmlns the browser needs when it is missing, and collapses whitespace between tags. Then it offers two encodings. "URL-encoded" escapes only the characters a data URI cannot carry (<, >, #, %, braces) and turns double quotes inside tags into single quotes, so the SVG stays readable and is usually shorter. "Base64" is the classic form. The default, "Shortest", picks whichever is smaller. Raster images are always Base64, since their bytes are binary.
Everything runs in your browser. The file you drop is read locally and never uploaded.
How to turn an SVG or image into CSS
- Drop an image or SVG on the dashed area, paste one with Ctrl+V, or press "choose a file". You can also paste SVG text into the "SVG code" box.
- If you loaded a PNG, JPG, WebP or GIF, its name appears as "Image loaded". Press "Paste SVG text instead" to go back to the text box.
- In "Write it as", choose
background,background-image,mask, "CSS variable", "data URI only" or "img tag". - For SVG, pick "SVG encoding": "Shortest", "URL-encoded" or "Base64".
- For
backgroundandmask, set "Repeat", "Size" and "Position". They do not apply to the other forms. - Check the preview, which uses the same URI you will copy. Read the size line, such as "209 bytes (URL-encoded SVG)".
- Copy the CSS from the output box. A warning appears when the result is large.
Examples
A small SVG as a background
Input SVG:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path fill="#ff0000" d="M12 2l3 7h-6z"/></svg>With the default options, the output is:
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'%3E%3Cpath fill='%23ff0000' d='M12 2l3 7h-6z'/%3E%3C/svg%3E") no-repeat center / contain;The # of the color became %23, the angle brackets became %3C and %3E, and the attribute quotes became single quotes so the whole URI fits inside url("..."). The size line reads 209 bytes.
URL-encoded versus Base64
For the same SVG, the URI alone is 162 characters URL-encoded and 198 characters in Base64, so "Shortest" picks URL-encoded. Base64 starts with data:image/svg+xml;base64,PHN2ZyB4bWxucz0i.... Base64 only wins on SVGs full of characters that must be escaped. For a PNG you do not choose: a 3-byte file with bytes 1, 2, 3 would be data:image/png;base64,AQID, and real images follow the same rule.
A mask and a custom property
With "Write it as" set to mask, you get two lines, the prefixed one first:
-webkit-mask: url("data:image/svg+xml,...") no-repeat center / contain;
mask: url("data:image/svg+xml,...") no-repeat center / contain;A mask uses the shape of the image and ignores its colors, so you color the element with background-color. Choosing "CSS variable" gives --image: url("data:image/svg+xml,...");, which you can reuse as background: var(--image). "img tag" gives <img src="data:image/svg+xml,..." alt="">.
Common mistakes
- Pasting only part of the SVG. If there is no
<svg>element, the tool says no SVG was found. Paste the whole code, from the opening tag to the closing tag. - Dropping a file that is not an image. A PDF or a text file is rejected. Use SVG, PNG, JPG, WebP, GIF or AVIF.
- Inlining a large image. Above about 8 KB, the tool warns you. Inline CSS cannot be cached apart from the stylesheet and it delays rendering, so a separate file is usually better.
- Expecting colors from a mask. A mask shows the alpha of the image, not its colors.
- Forgetting the size. With
sizeset toauto, a vector with only aviewBoxand nowidthorheightmay render at an unexpected size. Keepcontainorcover. - Using the URI inside single quotes in CSS. The SVG already contains single quotes, so the tool wraps the URI in double quotes. Keep them.
Frequently asked questions
Is a data URI always better than a separate file?
No. It saves a request, which helps for tiny icons, but it grows the stylesheet, blocks rendering and cannot be cached by itself. Use it for small assets only.
Why is URL-encoded shorter than Base64?
Base64 makes any data about a third longer, while URL encoding changes only a few characters. SVG is already text, so encoding it as text is a better fit.
Are my images uploaded?
No. Reading, encoding and the preview all happen in your browser.
Can I convert the SVG to a React component instead?
Yes, with SVG to JSX. To work with a raw Base64 string, see the image to Base64 tool, and to turn the CSS into a style object, use the React style converter.