html - 如何在 SVG 中使用 CSS 背景 url

标签 html css svg

我正在尝试链接 background: url(#myid); , id 将是一个 SVG 元素。

<svg id="myid"></svg>

这可能吗?我做错了什么吗?

.spesial {
  width: 100px;
  height: 100px;
  background-image: url(#test);
}
<svg id="test" xmlns="http://www.w3.org/2000/svg" width=100 height=100 viewbox="0 0 1500 1500">
  <metadata>Copyright (C) 2015 by original authors @ fontello.com</metadata>
  <path transform="translate(0 500)" stroke="#a4e" d="m929 11v428q-18-20-39-37-149-114-238-188-28-24-46-38t-48-27-57-13h-2q-26 0-57 13t-48 27-46 38q-88 74-238 188-21 17-39 37v-428q0-8 6-13t12-5h822q7 0 12 5t6 13z m0 586v14t-1 7-1 7-3 5-5 4-8 2h-822q-7 0-12-6t-6-12q0-94 82-159 108-85 224-177 4-2 20-16t25-21 25-18 28-15 24-5h2q11 0 24 5t28 15 25 18 25 21 20 16q116 92 224 177 30 24 56 65t26 73z m71 21v-607q0-37-26-63t-63-27h-822q-36 0-63 27t-26 63v607q0 37 26 63t63 26h822q37 0 63-26t26-63z"
  />
</svg>
<div class="spesial">
  Should there not be a background image here?
</div>

我知道我可以设置链接中的所有数据,但那样我就不能再使用它了。 <知识库> URL
<image href="pic.svg"那么我的 CSS 样式就必须是内联的。

最佳答案

您还可以将 svg 作为 base64 编码直接包含在 css 中并重用 css-class。

.spesial { background: url("data:image/svg+xml;utf8,<svg...

https://jsfiddle.net/2wjax28a/

关于html - 如何在 SVG 中使用 CSS 背景 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29818823/

相关文章:

html - 表格行上的边框而不是 td - 缺少 td

javascript - SlideToggle Specific Div onClick of Checkbox 还没有起作用?

c++ - 如何在 c++/winrt UWP 应用程序中使用 SVG 图像?

javascript - 用颜色部分填充形状的边框

css - 如何将 SVG 中的所有路径移动到同一点

html - 为 Bootstrap 行中的元素添加左右边距

Javascript 显示来自方法 elementsbyTagName 的所有(元素、标签)

html - 复选框的自定义图片?

jquery - 选项卡内容中的内容自动设置为显示 :none; so not showing

javascript - 自定义网络字体提供商