CSS: Using raw svg in the URL parameter of a background-image in IE

IE does appear to support using utf8 in a data URI, it’s simply being more fussy about it. See this Codepen Blog Post for more details but here are the highlights:

The author points to RFC2397 and highlights:

data:[<mediatype>][;base64],<data>

The <mediatype> is an Internet media type specification (with optional parameters.) The appearance of “;base64” means that the data is encoded as base64. Without “;base64”, the data (as a sequence of octets) is represented using ASCII encoding for octets inside the range of safe URL characters and using the standard %xx hex encoding of URLs for octets outside that range. If <mediatype> is omitted, it defaults to text/plain;charset=US-ASCII. As a shorthand, “text/plain” can be omitted but the charset parameter supplied.

  • Most browsers are lenient about the charset= string, but it’s required for Internet Explorer. That means you need to use ;charset=utf8, instead of ;utf8,.
  • From above, “Without “;base64″, the data (as a sequence of octets) is represented using ASCII encoding for octets inside the range of safe URL characters and using the standard %xx hex encoding of URLs for octets outside that range.” Which means you will have to percent-encode characters that aren’t URL-safe. For example, <svg> to %3Csvg%3E. You can minimize the amount of percent encoding that needs to be done by using single quotes ' instead of double quotes ".

Leave a Comment