More CSS answers

by Danny
Published 2 min read

CSS can only recolour an SVG that is part of the page. Learn how fill and currentColor work, why img tags block styling, and how mask-image recolours external icons.

The short answer

#

Inline the SVG in your HTML, set its fill (or stroke) to currentColor, and then change the colour with the normal color property. If the icon is loaded through an <img> tag or as a background-image, CSS on your page cannot reach inside it, so use mask-image instead.


_10
.icon {
_10
width: 1.5rem;
_10
height: 1.5rem;
_10
fill: currentColor;
_10
}
_10
_10
.button:hover .icon {
_10
color: rebeccapurple;
_10
}

Why it happens

#

An SVG is a small document of its own. When you paste the <svg> markup straight into your HTML, its shapes become part of the page's DOM, so your stylesheet can select them and set properties like fill and stroke.

When you load the same file with <img src="icon.svg">, background-image or content: url(), the browser treats it like a picture. Your page's CSS does not cascade into it, so fill on the img element does nothing.

There is a second trap with inline SVGs. Many icon files ship with hard-coded colours such as fill="#000" on each <path>. A presentation attribute like that has very low priority, so a rule in your stylesheet overrides it, but an inline style="fill: #000" attribute beats your stylesheet. Icons exported from design tools often contain both.

How to fix it

#

Inline the SVG and use currentColor

currentColor is a keyword that means "whatever the color property is here". Set it once on the icon and the icon follows the text colour of its parent, including hover, focus and dark mode.


_10
<button class="button">
_10
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true">
_10
<path d="M12 2l3 7h7l-5.5 4.5L18 21l-6-4-6 4 1.5-7.5L2 9h7z" />
_10
</svg>
_10
Favourite
_10
</button>


_11
.button {
_11
color: #1a1a1a;
_11
}
_11
_11
.button:hover {
_11
color: #c2410c;
_11
}
_11
_11
.icon {
_11
fill: currentColor;
_11
}

The best fix is to edit the SVG file itself and replace every hard-coded fill or stroke value with currentColor. Then you never need to fight the file in CSS. For outline icons, set stroke: currentColor and fill: none.

Override hard-coded colours

If you cannot edit the file, target the shapes inside the SVG.


_10
.icon path,
_10
.icon circle,
_10
.icon rect {
_10
fill: currentColor;
_10
}

This beats fill attributes. It will not beat inline style attributes unless you use !important, so cleaning the file is still the better option.

Reuse inline icons with a sprite

Pasting the same SVG many times bloats the HTML. A sprite lets you define each icon once with <symbol> and reference it with <use>. currentColor still works because the referenced shapes inherit from the <svg> element that uses them.


_10
<svg class="icon" aria-hidden="true">
_10
<use href="/icons.svg#star" />
_10
</svg>

Use mask-image for external files

When the icon must stay an external file, draw a coloured box and use the SVG as a mask. The shape of the icon cuts out the background colour.


_10
.icon-star {
_10
display: inline-block;
_10
width: 1.5rem;
_10
height: 1.5rem;
_10
background-color: currentColor;
_10
mask: url("/icons/star.svg") no-repeat center / contain;
_10
}

The unprefixed mask property works in all current major browsers. If you support older browsers, add the -webkit-mask version too. Masks only give you one colour per icon, so multi-colour icons need inlining.

Common pitfalls

#
  • Setting fill on an img tag. It has no effect. The file is a separate document.
  • Forgetting stroke icons. Many icon sets draw with strokes, so changing fill makes them look like solid blobs. Change stroke instead.
  • Using filter to recolour. Chains of filter: invert() sepia() hue-rotate() can approximate a colour, but they are fragile and hard to maintain.
  • Missing accessible names. Decorative icons should have aria-hidden="true". Icon-only buttons need a text label, for example with aria-label on the button.
  • No viewBox. Without a viewBox, the icon will not scale when you change its width and height.
#

Was this resource helpful?