css - IE 不将 SVG 显示为伪元素 :before or :after 中的内容

标签 css internet-explorer svg internet-explorer-11

此问题是以下线程的扩展。 Is there a way to use SVG as content in a pseudo element :before or :after .

我正在尝试使用伪类在 div 之后附加一个 svg 三 Angular 形。

div{
background-color: black;
width: 48px;
height: 45px;
}

div::before{
content: url('data:image/svg+xml; utf8, <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid meet" viewBox="266.53926701570674 152.66492146596858 44 44"><defs><path d="M267.54 153.66L307.54 193.66L267.54 193.66L267.54 153.66Z" id="d6AN4MEUYO"></path></defs><g><g><use xlink:href="#d6AN4MEUYO" opacity="1" fill="red" fill-opacity="1"></use></g></g></svg>');
right: -47px;
position: relative;

}
<div></div>

上面的代码在 chrome 中运行良好,但在 IE 中它没有加载 svg 内容。

我也尝试过使用 background-image 属性,但它似乎在 IE(版本 > 10)中不起作用。

将此 svg 作为内容附加到伪类的正确方法是什么?

最佳答案

我已经使用 IE 11 对其进行了测试,并且能够产生该问题。伪元素内容属性目前似乎不适用于 SVG 元素。它可能是某种错误或者是 IE 浏览器的默认行为。我会尝试提交有关此问题的反馈。

作为解决方法,我建议您使用 background-image 属性显示 svg 内容,如下所示:

<style>
    .div1:after {
        content: '';
        display: block;
        height: 80px;
        width: 80px;
        background-image: url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20height%3D%2280%22%20width%3D%22160%22%3E%0D%0A%20%20%3Ccircle%20cx%3D%2240%22%20cy%3D%2240%22%20r%3D%2238%22%20stroke%3D%22black%22%20stroke-width%3D%221%22%20fill%3D%22red%22%20%2F%3E%0D%0A%20%20%3Ccircle%20cx%3D%22120%22%20cy%3D%2240%22%20r%3D%2238%22%20stroke%3D%22black%22%20stroke-width%3D%221%22%20fill%3D%22blue%22%20%2F%3E%0D%0A%3C%2Fsvg%3E);
    }
</style>
<div class="div1" style="background-color:green; width:80px;height:80px"></div>

在IE 11浏览器中的结果如下:

enter image description here

编辑:

I am not able to use my svg with background-image property whereas your svg is loading with the property. Is there any problem with my svg?

当我们使用带有背景图像属性的 SVG 时,我们应该确保 reserved URL characters被编码(例如 < === %3C> === %3E )并且还用“%20”替换空格。

因此,请按如下方式对您的 svg 元素进行编码:

background-image: url(data:image/svg+xml,%3Csvg%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20preserveAspectRatio%3D%22xMidYMid%20meet%22%20viewBox%3D%22266.53926701570674%20152.66492146596858%2044%2044%22%3E%3Cdefs%3E%3Cpath%20d%3D%22M267.54%20153.66L307.54%20193.66L267.54%20193.66L267.54%20153.66Z%22%20id%3D%22d6AN4MEUYO%22%3E%3C%2Fpath%3E%3C%2Fdefs%3E%3Cg%3E%3Cg%3E%3Cuse%20xlink%3Ahref%3D%22%23d6AN4MEUYO%22%20opacity%3D%221%22%20fill%3D%22red%22%20fill-opacity%3D%221%22%3E%3C%2Fuse%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E);

然后,结果是这样的:

enter image description here

关于css - IE 不将 SVG 显示为伪元素 :before or :after 中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57055815/

相关文章:

html - 显示 3 个 div,2 个一起显示,第三个在第一个下面

javascript - 当元素位于视口(viewport)中时添加类的问题

html - 如何更改联系表单上的提交按钮字体

html - 负边距 : can they work in IE7 and IE8?

internet-explorer - 如何在 MVC 架构中包含 border-radius.htc 以使 border-radius 在 IE 中工作

css - SVG 动画不适用于 Google Chrome 或 IE

javascript - 如何设置图片的原始大小不超过父容器的宽度

html - IE 7 中的背景图像问题

javascript - 帧中的动画持续时间是什么意思?

html - 如何使通过 "class"响应包含的背景图像