问题描述
我在CSS中将以下嵌入式SVG定义为背景图像.
I have the following inline SVG defined as a background-image in my css.
div {
border: 1px solid black;
background-image: url("data:image/svg+xml;charset=utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' preserveAspectRatio='none' viewBox='0 0 10 10'> <path d='M2 10 L8 0 L10 0 L10 10' fill='%238A92DF'></path></svg>");
background-repeat: no-repeat;
background-position: center center;
background-size: 100%;
}
它在Chrome,Firefox和Edge中正常运行,但是在Internet Explorer 11中失败.
It works fine in Chrome, Firefox and Edge, but fails in Internet Explorer 11. Why?
推荐答案
您必须使用完整的URL编码svg.
You have to full URL encode your svg.
如果您使用的是VSCode,则有一个名为"URL编码"的扩展将为您完成此操作...或者您可以在网上轻松找到一个扩展名: eric/tools/dencoder/"rel =" noreferrer> https://meyerweb.com/eric/tools/dencoder/
If you're using VSCode, there is a extension called "URL Encode" that'll do this for you... OR you can easily find one online: https://meyerweb.com/eric/tools/dencoder/
请注意,我还删除了版本"属性和; charset = utf8"部分,不确定是否需要,但要进行清除...
Note that I've also removed the "version" attribute and the ";charset=utf8" part, not sure if needed, but to clear things up...
div {
border: 1px solid black;
background-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%2010%2010'%3E%3Cpath%20d%3D'M2%2010%20L8%200%20L10%200%20L10%2010'%20fill%3D'%238A92DF'%3E%3C%2Fpath%3E%3C%2Fsvg%3E");
background-repeat: no-repeat;
background-position: center center;
background-size: 100%;
width: 500px;
height: 500px;
}
<div></div>
这篇关于嵌入式SVG背景在Internet Explorer 11中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!