Reputation: 7529
I am looking for a way to change the color of an SVG icon (without fill
since it's not supported on IE11) and I saw that Github does it using the color
property.
I got this SVG from Github (basically the star button). If I go on Github and inspect it using developer tools and then set a color (color: red
) on it, I can get it to change color.
However if I copy it on my page and try to do the same it does not work:
.octicon-star {
color:red;
}
<svg class="octicon octicon-star v-align-text-bottom" viewBox="0 0 14 16" version="1.1" width="14" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M14 6l-4.9-.64L7 1 4.9 5.36 0 6l3.6 3.26L2.67 14 7 11.67 11.33 14l-.93-4.74L14 6z"></path></svg>
Any ideas?
Upvotes: 4
Views: 730
Reputation: 2898
You are looking for fill: CurrentColor
. It changes the fill
of the svg according to the value of color
. Here's an article on CSS-Tricks on this.
Edit: Of course this uses fill
aswell, but it's the method github is using.
.octicon-star {
color: green;
}
svg {
fill: currentColor;
}
<svg class="octicon octicon-star v-align-text-bottom" viewBox="0 0 14 16" version="1.1" width="14" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M14 6l-4.9-.64L7 1 4.9 5.36 0 6l3.6 3.26L2.67 14 7 11.67 11.33 14l-.93-4.74L14 6z"></path></svg>
Upvotes: 8