gpt4 book ai didi

css - 如何通过 CSS 更改 SVG 路径的填充颜色

转载 作者:行者123 更新时间:2023-11-28 11:38:15 25 4
gpt4 key购买 nike

我知道我们可以改变 fill如果/当原始 SVG 添加到 HTML 页面时,使用 CSS 的 SVG 路径的颜色。

但是,我不知道是否可以更改 fill如果在 <img> 中引用 SVG,则为颜色标签。 - 是否可以?如果是,怎么办?

示例:这 fill示例不起作用,因为我在 img 中引用 svg标签。

<html>
...
<style>
#foo{
fill: #ccc;
}
</style>
<body>
<img id="foo" src="myicon.svg" />
</body>
</html>

最佳答案

您不能通过 CSS 影响源位于另一个文件中的 SVG 图像。你只能影响文件本身或将 SVG 内嵌在 HTML 中,然后样式会起作用。

SVG 内联示例:

<!DOCTYPE html>
<html>
<body>
<style>
#foo{
fill: #ccc;
}
</style>

<svg id="foo" width="300" height="200">
<polygon points="100,10 40,180 190,60 10,60 160,180"
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
</svg>

</body>
</html>

关于css - 如何通过 CSS 更改 SVG 路径的填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23231907/

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