Using values="var(--color1);var(--color2)"
in an SVG <animate>
tag works in Firefox but not Chrome (see the snippet below). Is there a way to make it work in Chrome?
:root {
--color1: #7F0000;
--color2: #007F00;
}
<svg width="100" viewBox="-50 -50 100 100">
<circle cx="0" cy="0" r="25" fill="var(--color1)">
<animate attributeName="fill" dur="3s" values="var(--color1);var(--color2);var(--color1)" repeatCount="indefinite" />
</circle>
</svg>
3
Answers
Easiest workaround is a native JavaScript Web Component
something like:
getComputedStyle(this).getPropertyValue("--color1")
to read CSS valuesYou could replace the SMIL animation with an animation done with Web Animations API.
I believe that the easiest way to do this is with pure CSS. I added a "id" attribute to the circle so we can target it with a CSS selector. I then used CSS keyframes and animation to add the color change effect. I also switched the svg fill attribute out for a CSS style fill so we can avoid the weirdness of accessing CSS var() outside of CSS.