[英]Do SVGs embedded in CSS/SCSS data URIs still need to be base64 encoded?
A while back, we started including SVGs as background-image css. 不久之前,我们开始将SVG包含为背景图像CSS。 At the time, due to compatibility issues with IE we found just using SVGs as
当时,由于IE的兼容性问题,我们发现仅将SVG用作
data:image/svg+xml;charset=UTF-8,<svg ...> ... </svg>
So we had to base64 them: 因此,我们必须将它们设置为base64:
data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL...
Now that we have dropped support for IE < 11 does this still need to be done, or can I start simply using the SVG in the data URI as in the first example? 现在我们已经放弃了对IE <11的支持,还是需要这样做,还是可以像在第一个示例中一样,开始在数据URI中简单地使用SVG?
To continue the discussion from the comments: 要继续从评论中进行讨论:
A compromise that is both readable and shorter than base64 is to URL encode the SVG string, but with a few tricks to avoid encoding more than necessary. URL编码 SVG字符串是一种既易读又比base64短的折衷方案,但是有一些技巧可以避免不必要的编码。 Here is a blog post explaining this technique:
这是一篇博客文章,介绍了此技术:
https://codepen.io/tigt/post/optimizing-svgs-in-data-uris https://codepen.io/tigt/post/optimizing-svgs-in-data-uris
..and tools to do the job: ..和完成这项工作的工具:
https://codepen.io/jakob-e/pen/doMoML https://codepen.io/jakob-e/pen/doMoML
https://github.com/tigt/mini-svg-data-uri https://github.com/tigt/mini-svg-data-uri
function specialHexEncode(match) {
switch (match) { // Browsers tolerate these characters, and they're frequent
case '%20': return ' ';
case '%3D': return '=';
case '%3A': return ':';
case '%2F': return '/';
default: return match.toLowerCase(); // Compresses better
}
}
var result = svg
.replace(/\s+/g, ' ') // Collapse whitespace
.replace(/"/g, "'"); // Swap quotes
result = encodeURIComponent(result) // Encode everything..
.replace(/%[\dA-F]{2}/g, specialHexEncode) // ..except a few special characters
return 'data:image/svg+xml,' + result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.