Inline SVG background not working in Internet Explorer 11

我是研究僧i 提交于 2019-12-12 14:43:06

问题


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%;
}

It works fine in Chrome, Firefox and Edge, but fails in Internet Explorer 11. Why?

JSfiddle here.


回答1:


You have to full URL encode your svg.

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/

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>


来源:https://stackoverflow.com/questions/37550476/inline-svg-background-not-working-in-internet-explorer-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!