{"id":150,"date":"2026-10-18T21:10:26","date_gmt":"2026-10-18T21:10:26","guid":{"rendered":"https:\/\/refracttools.com\/blog\/?p=150"},"modified":"2026-10-18T21:10:26","modified_gmt":"2026-10-18T21:10:26","slug":"svg-data-uri-css","status":"publish","type":"post","link":"https:\/\/refracttools.com\/blog\/css-techniques\/svg-data-uri-css\/","title":{"rendered":"SVG in CSS: data URIs, encoding and recolouring"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To use an SVG in CSS without a separate file, URL-encode it into a data URI: <code>url(\"data:image\/svg+xml,\u2026\")<\/code>. Swap double quotes for single quotes, and encode <code>#<\/code> as <code>%23<\/code>, <code>&lt;<\/code> as <code>%3C<\/code>, <code>&gt;<\/code> as <code>%3E<\/code> and <code>%<\/code> as <code>%25<\/code>. Keep the <code>xmlns<\/code> attribute, or the image won&#8217;t display. URL-encoding is shorter than Base64 and stays readable, and using the SVG as a <code>mask<\/code> instead of a background lets you recolour it with CSS.<\/p>\n\n\n\n<div class=\"wp-block-group is-style-refract-takeaways is-layout-constrained wp-block-group-is-layout-constrained\">\n<h3 class=\"wp-block-heading\">Key takeaways<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A missing <code>xmlns<\/code>, an unencoded <code>#<\/code> in a colour, or clashing quotes cause most broken SVG backgrounds.<\/li>\n\n\n<li>URL-encoded SVG is shorter than Base64. In my test, 241 characters against 298.<\/li>\n\n\n<li><code>background-image<\/code> SVGs can&#8217;t be recoloured by CSS; <code>mask<\/code> SVGs can, using <code>background-color<\/code>.<\/li>\n\n\n<li>Optimise the SVG first so the data URI is as small as possible.<\/li>\n<\/ul>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How do you put an SVG in a CSS background?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an arrow icon, ready to drop into a stylesheet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.arrow {\n  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;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The SVG itself is unchanged apart from three things: double quotes inside it became single quotes, so they don&#8217;t close the <code>url(\"\u2026\")<\/code>; the <code>#<\/code> in the colour became <code>%23<\/code>, because a raw <code>#<\/code> starts a URL fragment and cuts the image off; and the angle brackets were encoded for maximum compatibility.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why URL-encode instead of Base64?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><div class=\"table-scroll\" tabindex=\"0\" role=\"region\" aria-label=\"Scrollable table\"><table><thead><tr><th>Version<\/th><th>Characters<\/th><th>Gzipped bytes<\/th><\/tr><\/thead><tbody><tr><td>Original SVG<\/td><td>204<\/td><td>\u2013<\/td><\/tr><tr><td>URL-encoded data URI<\/td><td>241<\/td><td>185<\/td><\/tr><tr><td>Base64 data URI<\/td><td>298<\/td><td>258<\/td><\/tr><\/tbody><\/table><\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">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 <a href=\"\/blog\/performance\/base64-images\/\">when to use Base64 images<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Which characters need encoding?<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><div class=\"table-scroll\" tabindex=\"0\" role=\"region\" aria-label=\"Scrollable table\"><table><thead><tr><th>Character<\/th><th>Encode as<\/th><th>Why<\/th><\/tr><\/thead><tbody><tr><td><code>\"<\/code><\/td><td><code>'<\/code> (switch to single quotes)<\/td><td>Would end the url(&#8220;\u2026&#8221;) string<\/td><\/tr><tr><td><code>#<\/code><\/td><td><code>%23<\/code><\/td><td>Starts a URL fragment and cuts off the rest<\/td><\/tr><tr><td><code>%<\/code><\/td><td><code>%25<\/code><\/td><td>Starts an escape sequence<\/td><\/tr><tr><td><code>&lt;<\/code> <code>&gt;<\/code><\/td><td><code>%3C<\/code> <code>%3E<\/code><\/td><td>Safest across browsers and tools<\/td><\/tr><tr><td>Line breaks<\/td><td>A space<\/td><td>Keeps the value on one line<\/td><\/tr><\/tbody><\/table><\/div><\/figure>\n\n\n<aside class=\"tool-card tool-card--inline\"><span class=\"swatch\" style=\"background:linear-gradient(135deg,#2f4bff,#9fb0ff)\" aria-hidden=\"true\"><\/span><div class=\"tool-card__body\"><p class=\"tool-card__eyebrow\">Free tool<\/p><p class=\"tool-card__name\">SVG to CSS converter<\/p><p class=\"tool-card__blurb\">SVG to URL-encoded data URIs for backgrounds, masks and variables.<\/p><\/div><a class=\"btn primary\" href=\"https:\/\/refracttools.com\/svg-to-css\/\">Try it free <span aria-hidden=\"true\">\u2192<\/span><\/a><\/aside>\n\n\n\n<h2 class=\"wp-block-heading\">How do you change the colour of an SVG background?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can&#8217;t, directly. An SVG used as a background image is drawn as a fixed picture, and <code>currentColor<\/code> inside it falls back to black because it can&#8217;t see the page&#8217;s styles. The fix is to use the SVG as a mask. The mask keeps only its shape, and the element&#8217;s <code>background-color<\/code> shows through:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.icon {\n  width: 1em;\n  height: 1em;\n  background-color: currentColor;\n  -webkit-mask: url(\"arrow.svg\") center \/ contain no-repeat;\n          mask: url(\"arrow.svg\") center \/ contain no-repeat;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now the icon follows the surrounding text colour, including hover states and dark mode, and one SVG serves every colour variant. <code>mask<\/code> works in all current browsers; the <code>-webkit-<\/code> line covers older Safari and Chrome.<\/p>\n\n\n\n<div class=\"wp-block-group is-style-refract-callout is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"wp-block-paragraph\"><strong>Reuse:<\/strong> Store the data URI in a custom property, such as <code>--icon-arrow: url(\"data:image\/svg+xml,\u2026\")<\/code>, then use <code>mask: var(--icon-arrow)<\/code> wherever you need it. One definition, many icons.<\/p>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Practical uses for SVG data URIs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Repeating patterns<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A small SVG tile repeated with <code>background-size<\/code> 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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.dots {\n  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\");\n  background-size: 20px 20px;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">List bullets and checkboxes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Custom bullets with <code>list-style-image<\/code> or a <code>::before<\/code> mask, a tick inside a styled checkbox, a chevron in a <code>&lt;select&gt;<\/code>, or the arrow on a &#8220;read more&#8221; link are all good candidates. They are decorative, reused everywhere, and small enough that a separate request would cost more than the bytes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why isn&#8217;t my SVG background showing?<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Missing <code>xmlns<\/code>.<\/strong> Inline SVG in HTML works without it, but a data URI needs <code>xmlns=\"http:\/\/www.w3.org\/2000\/svg\"<\/code> on the root element.<\/li>\n\n\n<li><strong>Unencoded <code>#<\/code>.<\/strong> <code>fill=\"#ff0000\"<\/code> must become <code>fill='%23ff0000'<\/code>.<\/li>\n\n\n<li><strong>Quote clash.<\/strong> Double quotes inside a double-quoted <code>url(\"\u2026\")<\/code> end the string early.<\/li>\n\n\n<li><strong>No size.<\/strong> An SVG without <code>width<\/code>, <code>height<\/code> or a <code>viewBox<\/code> may render at zero size; set <code>background-size<\/code> too.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Should you inline SVG in HTML instead?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inline <code>&lt;svg&gt;<\/code> 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&#8217;t use them for anything that carries meaning. A third option is a normal <code>.svg<\/code> file referenced with <code>url(\"\/icons\/arrow.svg\")<\/code>. 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Convert an SVG<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Paste or drop your SVG into the <a href=\"https:\/\/refracttools.com\/svg-to-css\/\">SVG to CSS converter<\/a> and choose background, mask or custom property. It adds a missing <code>xmlns<\/code>, fixes quotes and encodes the right characters. For the smallest result, run the file through the <a href=\"https:\/\/refracttools.com\/svg-optimiser\/\">SVG optimiser<\/a> first; our guide to <a href=\"\/blog\/image-optimisation\/optimise-svg-files\/\">optimising SVG files<\/a> covers what is safe to remove.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Last checked: October 2026.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Sources: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/URI\/Reference\/Schemes\/data\" rel=\"noopener\" target=\"_blank\">MDN: data: URLs<\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/mask\" rel=\"noopener\" target=\"_blank\">MDN: mask<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to put an SVG in a CSS background as a URL-encoded data URI, which characters to encode, why it beats Base64, and how to recolour icons with mask and currentColor.<\/p>\n","protected":false},"author":1,"featured_media":149,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_refract_tool":"svg-to-css","footnotes":""},"categories":[8],"tags":[17,30],"class_list":["post-150","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css-techniques","tag-performance","tag-svg"],"_links":{"self":[{"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":1,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":163,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions\/163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/media\/149"}],"wp:attachment":[{"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/refracttools.com\/blog\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}