To use an SVG in CSS without a separate file, URL-encode it into a data URI: url("data:image/svg+xml,…"). Swap double quotes for single quotes, and encode # as %23, < as %3C, > as %3E and % as %25. Keep the xmlns attribute, or the image won’t display. URL-encoding is shorter than Base64 and stays readable, and using the SVG as a mask instead of a background lets you recolour it with CSS.
Key takeaways
- A missing
xmlns, an unencoded#in a colour, or clashing quotes cause most broken SVG backgrounds. - URL-encoded SVG is shorter than Base64. In my test, 241 characters against 298.
background-imageSVGs can’t be recoloured by CSS;maskSVGs can, usingbackground-color.- Optimise the SVG first so the data URI is as small as possible.
How do you put an SVG in a CSS background?
Here is an arrow icon, ready to drop into a stylesheet:
.arrow {
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%237b5cff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 12h14'/%3E%3Cpath d='m12 5 7 7-7 7'/%3E%3C/svg%3E") center / 1em no-repeat;
}
The SVG itself is unchanged apart from three things: double quotes inside it became single quotes, so they don’t close the url("…"); the # in the colour became %23, because a raw # starts a URL fragment and cuts the image off; and the angle brackets were encoded for maximum compatibility.
Why URL-encode instead of Base64?
SVG is already text, so it only needs a few characters escaped. Base64 encodes every byte, adding about a third. I measured the arrow above:
| Version | Characters | Gzipped bytes |
|---|---|---|
| Original SVG | 204 | – |
| URL-encoded data URI | 241 | 185 |
| Base64 data URI | 298 | 258 |
The URL-encoded version is about 19% shorter, compresses better because its text is still repetitive, and you can read and edit the colour straight in your CSS. Base64 is only worth it if a tool or CMS rejects the special characters. For raster images, the trade-offs are different; see when to use Base64 images.
Which characters need encoding?
| Character | Encode as | Why |
|---|---|---|
" | ' (switch to single quotes) | Would end the url(“…”) string |
# | %23 | Starts a URL fragment and cuts off the rest |
% | %25 | Starts an escape sequence |
< > | %3C %3E | Safest across browsers and tools |
| Line breaks | A space | Keeps the value on one line |
How do you change the colour of an SVG background?
You can’t, directly. An SVG used as a background image is drawn as a fixed picture, and currentColor inside it falls back to black because it can’t see the page’s styles. The fix is to use the SVG as a mask. The mask keeps only its shape, and the element’s background-color shows through:
.icon {
width: 1em;
height: 1em;
background-color: currentColor;
-webkit-mask: url("arrow.svg") center / contain no-repeat;
mask: url("arrow.svg") center / contain no-repeat;
}
Now the icon follows the surrounding text colour, including hover states and dark mode, and one SVG serves every colour variant. mask works in all current browsers; the -webkit- line covers older Safari and Chrome.
Reuse: Store the data URI in a custom property, such as --icon-arrow: url("data:image/svg+xml,…"), then use mask: var(--icon-arrow) wherever you need it. One definition, many icons.
Practical uses for SVG data URIs
Repeating patterns
A small SVG tile repeated with background-size makes dots, grids, diagonal lines or wave patterns at any scale, for a few hundred bytes. Because it is a vector, the pattern is crisp on high-density screens, unlike a PNG tile, which can blur when scaled.
.dots {
background: #0b1238 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Ccircle cx='2' cy='2' r='1.5' fill='%23ffffff33'/%3E%3C/svg%3E");
background-size: 20px 20px;
}
List bullets and checkboxes
Custom bullets with list-style-image or a ::before mask, a tick inside a styled checkbox, a chevron in a <select>, or the arrow on a “read more” link are all good candidates. They are decorative, reused everywhere, and small enough that a separate request would cost more than the bytes.
Why isn’t my SVG background showing?
- Missing
xmlns. Inline SVG in HTML works without it, but a data URI needsxmlns="http://www.w3.org/2000/svg"on the root element. - Unencoded
#.fill="#ff0000"must becomefill='%23ff0000'. - Quote clash. Double quotes inside a double-quoted
url("…")end the string early. - No size. An SVG without
width,heightor aviewBoxmay render at zero size; setbackground-sizetoo.
Should you inline SVG in HTML instead?
Inline <svg> in your HTML is better for icons that need parts styled individually, animated with CSS, or labelled for screen readers. CSS data URIs are better for decorative icons, patterns, list bullets and custom checkboxes, where the image is purely visual and reused through classes. Decorative CSS images are ignored by screen readers, so don’t use them for anything that carries meaning. A third option is a normal .svg file referenced with url("/icons/arrow.svg"). It is cached separately and shared across pages, which suits larger illustrations or icons used throughout a big site. Data URIs win for small, frequently used decoration where an extra request would cost more than the bytes saved.
Convert an SVG
Paste or drop your SVG into the SVG to CSS converter and choose background, mask or custom property. It adds a missing xmlns, fixes quotes and encodes the right characters. For the smallest result, run the file through the SVG optimiser first; our guide to optimising SVG files covers what is safe to remove.
Last checked: October 2026.
Sources: MDN: data: URLs, MDN: mask.


