Skip to main content
CalcChef

SVG to CSS Background Converter

Convert SVG markup to a CSS background-image data URI, URL-encoded or Base64. Adds a missing xmlns, rejects non-SVG input, gives url() or inline-style output. How it works

Use the SVG to CSS Background Converter

Paste one SVG element or open an .svg file, up to 100,000 characters. The xmlns attribute is added to the root element if it is missing.

Output Format
Encoding

Enter SVG code above to generate CSS background data URI.

About the SVG to CSS Background Converter

To use an SVG as a CSS background, percent-encode its markup after data:image/svg+xml;charset=utf-8, and put it inside background-image: url('…'). This converter does that for one SVG element of up to 100,000 characters, adds the xmlns attribute to the root when it is missing, and stops with a message when the markup is not an SVG. A Base64 option writes data:image/svg+xml;base64, instead, and the page shows the length of both.

What the converter does with each kind of input

InputResult
Text that is not SVG: helloThis is not an SVG document. The markup must start with an <svg> element (an XML declaration or comment before it is fine).
An unclosed root: <svg viewBox="0 0 1 1">The <svg> element is not closed. The markup must end with </svg>.
A root with xmlns="http://example.com"The root xmlns is "http://example.com". An SVG image needs xmlns="http://www.w3.org/2000/svg".
A closed root without xmlnsAccepted; xmlns is added to the root
A closed root with the SVG xmlnsAccepted unchanged

The messages are the ones the tool shows. An XML declaration, comments and a DOCTYPE before the root are dropped before the check.

How to use this tool

  1. Paste one SVG element, from <svg to </svg>, or open or drop a .svg file.
  2. Choose url() output (a declaration for a stylesheet) or Inline Style output (the same declaration inside a style="…" attribute), and URL-encoded (the default) or Base64.
  3. Check the preview, then copy the CSS declaration or the full .element example.

Frequently asked questions

How do I use an SVG as a CSS background image?
Percent-encode the SVG markup, put data:image/svg+xml;charset=utf-8, in front of it and wrap the result in url() in a background-image declaration. Paste your SVG here and the tool builds that line, escaping the single and double quote characters so either quote style works.
Why does my SVG background not show?
The usual causes are a missing or wrong xmlns on the root svg element, which MDN says is required on the outermost svg element of an SVG document, and markup that is cut off before the closing tag. The tool adds a missing xmlns and rejects a wrong one or an unclosed root. Colors set by the page, such as currentColor, do not reach inside an SVG used as an image, so set fill and stroke in the markup.
Does the converter output base64?
Yes, when you pick Base64 under Encoding; URL-encoded is the default. The page prints both lengths. For the red circle example the URL-encoded URI is 219 characters and the Base64 one 170, because this encoder turns every space, quote and angle bracket into three characters. The URL-encoded form keeps tag names and colors readable, so you can still edit a fill by hand. RFC 2397 allows a data: URL to carry either form.
What happens if my SVG is too long?
Above 100,000 characters the tool shows the length of your markup and asks you to convert up to 100,000 characters at a time, and builds no declaration. Large SVGs with embedded images are better kept as a separate .svg file; RFC 2397 says the data: scheme is only useful for short values.
What is the .element rule in the full example?
A placeholder: .element is not a class the tool made for you. It uses the same URI with no-repeat, contain and center, so rename the selector to your own.

Related Tools

View all tools