internet explorer - CSS: Using raw svg in the URL parameter of a background-image in IE -
so, i'm trying this:
div { width: 100px; height: 100px; background-position: center center; background-repeat: no-repeat; background-size: 100px 100px; background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100' style='fill:red; stroke:none'><rect x='0' y='40' width='100' height='20' /><rect x='40' y='0' width='20' height='100' /></svg>"); }
see here: http://jsfiddle.net/trxkcx41/4/
this works beautifully in current versions chrome, safari (os x , ios), , firefox. however, svg doesn't appear @ in ie 9, 10, or 11.
is because not supported in ie, or because i'm doing wrong?
[update solution]
thanks hungerstar, i've got working. summarize recommendation, needed make following changes:
change data type
data:image/svg+xml;utf8
data:image/svg+xml;charset=utf8
(i.e.,charset=
required)url encode svg. minimize url-encoding, use
'
instead of"
enclose attributes. so, in case,<
,>
needed encoded.
to, ultimately, css looked this:
background-image: url("data:image/svg+xml;charset=utf8,%3csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' style='fill:red; stroke:none' %3e%3crect x='0' y='40' width='100' height='20' /%3e%3crect x='40' y='0' width='20' height='100' /%3e%3c/svg%3e");
ie appear support using utf8
in data uri, it's being more fussy it. see codepen blog post more details here highlights:
the author points rfc2397 , highlights:
data:[<mediatype>][;base64],<data>
the <mediatype> internet media type specification (with optional parameters.) appearance of ";base64" means data encoded base64. without ";base64", data (as sequence of octets) represented using ascii encoding octets inside range of safe url characters , using standard %xx hex encoding of urls octets outside range. if <mediatype> omitted, defaults text/plain;charset=us-ascii. shorthand, "text/plain" can omitted charset parameter supplied.
- most browsers lenient charset= string, it's required internet explorer. means need use
;charset=utf8,
instead of;utf8,
. - from above, "without ";base64", data (as sequence of octets) represented using ascii encoding octets inside range of safe url characters , using standard %xx hex encoding of urls octets outside range." means have percent-encode characters aren't url-safe. example,
<svg>
%3csvg%3e
. can minimize amount of percent encoding needs done using single quotes'
instead of double quotes"
.
Comments
Post a Comment