用背景图像填充 SVG 路径元素 [英] Fill SVG path element with a background-image
本文介绍了用背景图像填充 SVG 路径元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以为 SVG
元素设置 background-image
元素?
Is it possible to set a background-image
for an SVG <path>
element?
例如,如果我设置元素 class="wall"
,CSS 样式 .wall {fill: red;}
有效,但 .wall{background-image: url(wall.jpg)}
没有,.wall {background-color: red;}
.
For instance, if I set the element class="wall"
, the CSS style .wall {fill: red;}
works, but .wall{background-image: url(wall.jpg)}
does not, neither .wall {background-color: red;}
.
推荐答案
您可以通过将背景制作成 模式:
You can do it by making the background into a 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>
根据你的图片调整宽度和高度,然后像这样从路径中引用它:
Adjust the width and height according to your image, then reference it from the path like this:
<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)" />
这篇关于用背景图像填充 SVG 路径元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文