:root {
--logo-color: red;
--text-color: blue;
}
* {
color: var(--text-color)
}
.logo{
color: var(--logo-color);
}
<svg
class="logo"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<circle cx="12" cy="5" r="3" />
<line x1="12" y1="22" x2="12" y2="8" />
<path d="M5 12H2a10 10 0 0 0 20 0h-3" />
</svg>
I attempted to modify the color of an SVG using the color
property in my CSS file. I selected the .logo
class and anticipated the logo to appear in red. However, it ended up being displayed in blue, as if the generic CSS (0-0-0) had higher precedence than the class-specific styles (0-1-0). And if I remove the color
property from the generic block, then it works normally.
2
Answers
in SVG elements colors must be set by
fill
property.You can also use in CSS
stroke : var(--text-color);
instead of actual svg property
stroke="currentColor"
Beware asterisk (
*
)The culprit in your code is that even though
*
has the weakest specificity, it actually matches everything, not only thesvg
element (that by itself does not "draw" anything), but also all its descendants (that do the actual drawing). So basically your explicithas no effect, because its children (
circle
,line
andpath
) elements are all then again matched by the precedingso the
color: red
that would otherwise be inherited from the parent is there overridden toblue
from*
. In a simplified sample:How to fix – even more force
There are several ways how to cope with this. One could be overriding general child selector in the more specific selector:
svg *
(.logo *
) into your CSSWhy it works
Fact that CSS
color
property affects your strokes is indeed set by SVG’s root nodestroke="currentColor"
attribute, that makes SVG and all its children copy the computedcolor
value intostroke
(unless explicitly told otherwise). It is usually a good practice to do this and not copy colour values tostroke
and/orfill
.Best practice (in general)
Best practice would probably be not using
*
at all and let implicitcolor
value inheritance do its work:*
in general is rarely optimal solution for any task. Besides introducing potential "footguns" like this one, it can even be slightly detrimental for performance.