简体   繁体   English

嵌入CSS / SCSS数据URI中的SVG是否仍需要进行base64编码?

[英]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.

相关问题 有没有人得到IE8来读取编码为base64的数据URI的DHTML行为? - Has anyone gotten IE8 to read DHTML behaviors encoded as base64'd data URIs? 现代浏览器是否像图像一样支持HTML中的base64编码的JS或CSS块? - Do modern browsers support base64 encoded JS or CSS chunks in HTML like they do for images? CSS 加载 Base64 编码图像作为背景 URL - CSS Loading a Base64 Encoded Image as a Background URL base64编码动画gif作为CSS背景? - base64 encoded animated gif as css background? 某些SVG作为base64无法使用CSS正确显示,但在img标签内的HTML中可以正常工作 - Some SVGs as base64 are not displaying correctly using CSS but works fine in HTML within an img tag 如何动态应用base64编码的数据uri? - How to apply base64 encoded data uri's dynamically? 如何在Outlook电子邮件中显示嵌入式Base64编码的图像 - How can I display embedded Base64 encoded images in Outlook email 有没有一种方法可以使css悬停时将base64编码的背景图像png变成黑白? - Is there a way to make a base64 encoded background image png black and white on hover with css only? webpack样式加载器在样式标签内将css显示为base64编码的字符串 - webpack style-loader displays css as a base64 encoded string inside the style tag Base64编码的SVG图像作为Background-Image CSS属性 - Base64 Encoded SVG Image as Background-Image CSS Property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM