如何在元素的伪元素之前添加svg作为内容?

Fra*_*ero 10 css svg css3

我试图在:before伪元素的内容中使用svg .

为此,我正在关注这个问题:有没有办法将SVG用作伪元素中的内容:before或:after但是我无法使其工作.

我只有一个简单的SVG:

<svg height="100" width="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的:

示例1:在content属性上插入svg

#square{
   background-color: green;
   width: 100px;
   height: 100px;
}

#square:before{
   display: block;
   content: url("data:image/svg+xml;charset=UTF-8,<svg height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' /></svg>");
   background-size: 28px 28px;
   height: 28px;
   width: 28px;
}
Run Code Online (Sandbox Code Playgroud)
<div id="square"></div>
Run Code Online (Sandbox Code Playgroud)

示例2:使用base64编码

#square{
   background-color: green;
   width: 100px;
   height: 100px;
}

#square:before{
   display: block;
   content: url("data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjEwMCIgd2lkdGg9IjEwMCI+PGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNDAiIHN0cm9rZT0iYmxhY2siIHN0cm9rZS13aWR0aD0iMyIgZmlsbD0icmVkIiAvPjwvc3ZnPg==");
   background-size: 28px 28px;
   height: 28px;
   width: 28px;
}
Run Code Online (Sandbox Code Playgroud)
<div id="square"></div>
Run Code Online (Sandbox Code Playgroud)

如您所见,这些示例中的任何一个都不起作用,所以我确信我遗漏了一些东西.

我做错了什么?

提前致谢!

Nil*_*oct 18

似乎SVG标签需要更多属性.

#square{
   background-color: green;
   width: 100px;
   height: 100px;
}

#square:before{
   display: block;
   content: url("data:image/svg+xml;charset=UTF-8, <svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' /></svg>");
   background-size: 28px 28px;
   height: 28px;
   width: 28px;
}
Run Code Online (Sandbox Code Playgroud)
<div id="square"></div>
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢你!添加`xmlns ='http://www.w3.org/2000/svg'version ='1.1'`似乎是线索! (4认同)