Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate the fill color of SVG with CSS?

Tags:

html

css

svg

CSS allows to change the color of SVG like this

.clr {fill: green;}

But when I apply animation with the same fill attributes nothing seems to work. What should I do?

<svg width="800" height="600" style="background-color:lightblue">

<circle class="clr" cx="610" cy="240" r="4" fill="gold" />

<style>

.clr {animation: col 3s linear infinite;}

@keyframes col {
0%,71% {fill:none}
72% {fill:black}
75%,100% {fill:none}
}

</style>
</svg>
like image 656
CeeJay Avatar asked Aug 12 '17 07:08

CeeJay


People also ask

Can you animate color CSS?

CSS 3 color animations can be used in modern browsers today. For most use cases no animations in unsupported browsers isn't a problem, and, if it is, jQuery UI can be used to polyfill the functionality.

Can we change SVG image color in CSS?

Edit your SVG file, add fill="currentColor" to svg tag and make sure to remove any other fill property from the file. Note that currentColor is a keyword (not a fixed color in use). After that, you can change the color using CSS, by setting the color property of the element or from it's parent.


2 Answers

Its working as expected, I just increased the circle radius and changed its position to show it on 50x and 50y,

.color {animation: col 3s linear infinite;}

@keyframes col {
0%,71% {fill:none}
72% {fill:black}
75%,100% {fill:none}
}
<svg width="800" height="600" style="background-color:lightblue">
  <circle class="color" cx="50" cy="50" r="30" fill="gold" />
</svg>
like image 97
Muhammad Avatar answered Sep 30 '22 02:09

Muhammad


You Just add fill:black in @keyframes section. change it to green like this:

.color {animation: col 3s linear infinite;}

@keyframes col {
0%,71% {fill:none}
72% {fill:green}
75%,100% {fill:none}
}
<svg width="800" height="600" style="background-color:lightblue">
<circle class="color" cx="610" cy="240" r="4" fill="gold" />
</svg>

and don't want to use .color {fill: green;}.

like image 39
Arun V Jose Avatar answered Sep 30 '22 02:09

Arun V Jose