I'm setting up a few icons in a library that's using some basic CSS and an SVG Sprite (generated through webpack).
Some of the icons I want to be able to color with multiple colors. My set up looks like:
mail.svg (details of the svg are omitted for simplicity)
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 64 64" width="64" height="64">
<polyline class="primary-stroke" fill="none" stroke-width="2" [more-stuff-here]></polyline>
<path fill="none" stroke-width="2" [more-stuff-here]></path>
<line class="primary-stroke" fill="none" stroke-width="2" [more-stuff-here]></line>
</svg>
My computed CSS (blue
is the primary accent color) looks like:
svg {
fill: currentColor;
stroke: currentColor;
}
.primary-stroke {
stroke: blue;
fill: none;
}
And my HTML looks like:
<svg><use xlink:href="#mail"></svg>
This all works exactly as expected, but now I want to take it a step further. I want to be able to add a class to the element to determine if this instance should contain 1 single color or 2 colors.
My attempt was pretty simple. I just added a single-color
class to the svg
element to look like:
<svg class="single-color"><use xlink:href="#mail"></svg>
And modified the SCSS. The computed CSS looks like:
.single-color .primary-stroke {
stroke: currentColor;
fill: none;
}
But, it definitely does not work. The primary
styles still take effect. I'm new to working with SVGs and I'm not sure if what I'm trying to do is even possible with a sprite?
CodePens demonstrating the issue:
Both examples use the same classes and SVGs.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…