是否可以设置 background-image
对于 SVG <path>
元素?
例如,如果我设置元素 class="wall"
, CSS 样式 .wall {fill: red;}
有效,但是 .wall{background-image: url(wall.jpg)}
没有,也没有.wall {background-color: red;}
.
最佳答案
您可以通过将背景制作成 pattern 来做到这一点:
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
<image href="wall.jpg" x="0" y="0" width="100" height="100" />
</pattern>
</defs>
根据你的图片调整宽度和高度,然后像这样从路径中引用它:
<path d="M5,50
l0,100 l100,0 l0,-100 l-100,0
M215,100
a50,50 0 1 1 -100,0 50,50 0 1 1 100,0
M265,50
l50,100 l-100,0 l50,-100
z"
fill="url(#img1)" />
关于html - 用背景图像填充 SVG 路径元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59488002/