gpt4 book ai didi

html - 用背景图像填充 SVG 路径元素

转载 作者:IT老高 更新时间:2023-10-28 11:09:40 25 4
gpt4 key购买 nike

是否可以设置 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)" />

Working example

关于html - 用背景图像填充 SVG 路径元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3796025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com