我无法使CSS过渡效果在SVG中的use
元素上生效。
在原始SVG上可以正常工作,但在使用use
后不行。请参见下面代码。
这似乎是Chrome特有的问题(两个示例都可以在Firefox中工作)。
.stretched-rect {
width: 50px;
height: 50px;
}
.stretched-rect svg {
width: 100%;
height: 100%;
}
.stretched-rect {
--fill-color: red;
}
.stretched-rect:hover {
--fill-color: blue;
}
Example with original svg (this works):
<div class="stretched-rect">
<svg viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
</svg>
</div>
<br><br>
<!-- Define SVG, so we can use it with `use` -->
<svg style="display: none;">
<symbol id="svg-rect" viewbox="0 0 100 100" preserveAspectRatio="none">
<rect width="100" height="100" rx="15" style="fill: var(--fill-color); transition: 1s fill;"/>
</symbol>
</svg>
Example using "use" (this does not work):
<div class="stretched-rect">
<svg><use xlink:href="#svg-rect"/></svg>
</div>