CSS中的内联SVG不起作用

时间:2018-03-23 14:43:56

标签: html css svg

我在CSS文件中使用inline-SVG来填充div的背景。但它没有用。我测试我的SVG代码是好的。您可以通过将svg代码复制到此https://www.rapidtables.com/web/tools/svg-viewer-editor.html

进行测试



#bgr{
  width:500px;
  height:500px;
  background:url('data:image/svg+xml;utf8,<svg width="500" height="500"><path d="M340.8,98.4c50.7,0,91.9,41.3,91.9,92.3c0,26.2-10.9,49.8-28.3,66.6L256,407.1L105,251.6c-15.8-16.6-25.6-39.1-25.6-63.9 c0-51,41.1-92.3,91.9-92.3c38.2,0,70.9,23.4,84.8,56.8C269.8,121.9,302.6,98.4,340.8,98.4" style="fill:red;"/></svg>');
}
&#13;
<div id="bgr"></div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:6)

您需要设置xmlns="http://www.w3.org/2000/svg"。您可以找到更多信息here

#bgr{
  width:500px;
  height:500px;
  background:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg"   width="500" height="500"><path d="M340.8,98.4c50.7,0,91.9,41.3,91.9,92.3c0,26.2-10.9,49.8-28.3,66.6L256,407.1L105,251.6c-15.8-16.6-25.6-39.1-25.6-63.9 c0-51,41.1-92.3,91.9-92.3c38.2,0,70.9,23.4,84.8,56.8C269.8,121.9,302.6,98.4,340.8,98.4" style="fill:red;"/></svg>');
}
<div id="bgr"></div>

答案 1 :(得分:-3)

您可以将其转换为base64 img,其中包含一系列可以执行此操作的在线工具,例如:

http://b64.io/

&#13;
&#13;
#bgr{

  width:500px;
  height:500px;
background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMDAiIGhlaWdodD0iMTAwIiB2ZXJzaW9uPSIxLjEiPiAgIDxyZWN0IHdpZHRoPSIyMDAiIGhlaWdodD0iMTAwIiBzdHJva2U9ImJsYWNrIiBzdHJva2Utd2lkdGg9IjYiIGZpbGw9ImdyZWVuIi8+PC9zdmc+);
  );
}
&#13;
<div id="bgr"></div>
&#13;
&#13;
&#13;