---
title: "How do I change the colour of an SVG icon with CSS?"
description: "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."
url: https://www.danny.engineering/answer/css/how-do-i-change-the-colour-of-an-svg-icon-with-css
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:31:00.000Z
tags: ["CSS"]
---

# How do I change the colour of an SVG icon with CSS?

## 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.


```css
.icon {
  width: 1.5rem;
  height: 1.5rem;
  fill: currentColor;
}

.button:hover .icon {
  color: rebeccapurple;
}
```


## 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.


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


```css
.button {
  color: #1a1a1a;
}

.button:hover {
  color: #c2410c;
}

.icon {
  fill: currentColor;
}
```


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.


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


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.


```html
<svg class="icon" aria-hidden="true">
  <use href="/icons.svg#star" />
</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.


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


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.

## Related reading

- [MDN: fill](https://developer.mozilla.org/en-US/docs/Web/CSS/fill)
- [MDN: mask-image](https://developer.mozilla.org/en-US/docs/Web/CSS/mask-image)
