我正在创建一个SVG元素,并希望更改其背景颜色 . 根据this question,并且根据W3C recommendations, background-color
不是SVG的标准样式,但必须使用 fill
. 但是, fill
不起作用,the most common solution是在 svg
元素内创建一个 rect
元素,并使该矩形元素的宽度和高度与 svg
的宽度和高度相似 .
因此,以下是建议解决方案的结果:
<svg width="300" height="200">
<rect width="300" height="200" style="fill: rgb(0, 255, 0);></rect>
</svg>
然后我改为:
<svg width="300" height="200">
<rect width="100%" height="100%" style="fill: rgb(0, 255, 0);"></rect>
</svg>
(请注意,我在第二次尝试中将 width
和 height
设置为100%) .
现在我的问题是:即使这样可行,使用W3C标准的宽度和高度百分比?或者它是一个黑客?
谢谢 .
1 回答
是 . 根据https://www.w3.org/TR/SVG/coords.html: