SVG Icon Color Not Changing With CSS: Fix the fill and stroke

PublishedToolSVG Color Replacer

Contents
  1. The Problem
  2. The Fix
  3. Common Pitfalls
  4. The Takeaway
  5. Frequently Asked Questions

If you set color on an SVG icon with CSS and nothing changes, it's usually because the color value is already hardcoded in the icon file as an attribute like fill="#3b82f6". CSS color doesn't automatically override that value. This article explains why inheritance breaks down and how to change the fill and stroke values inside the file itself.

Summary ① SVG fill/stroke attributes are not automatically tied to CSS color. They inherit it only when fill: currentColor is explicitly set in the SVG or CSS. ② An SVG loaded with an <img> tag receives no CSS from the page, so you can't change its color that way in the first place. ③ The most reliable method is to replace the fill/stroke values inside the SVG code with the color code you want.

The Problem

It's common to download an icon set, add it to your site, and set .icon { color: red; } in CSS to change the color on hover, only to see the icon stay exactly the same. When you open it in DevTools, you find a color value already baked into each element, like <path fill="#3b82f6">.

The reason is that SVG fill/stroke are paint properties separate from text color. The initial value of fill is not currentColor but black, so for it to inherit the parent's color, the SVG has to explicitly declare fill="currentColor". Most downloaded icons have a specific color (blue, for example) already hardcoded as the fill value by the original author, so there's no room for another color to get in.

There's a more fundamental problem when you load the icon as an image tag, like <img src="icon.svg">. The browser treats an SVG loaded with <img> as a completely separate document, so the parent page's CSS can't pass down even currentColor. In this case, no amount of CSS tweaking will change the color.

The Fix

To fix it with CSS alone, without touching the original SVG code, first paste the icon directly into the page HTML as <svg>...</svg> instead of <img> (inline SVG). Then delete the fill="#3b82f6" attribute on each element or change it to fill="currentColor". Only then will the CSS .icon { color: red; } inherit and apply.

If you have one or two elements, you can edit them by hand. But if there are several different fill values mixed together (for example, when the border color and fill color differ) or you have a lot of icons, changing them one by one is tedious. In that case, including when you use the icons as <img> tags, it's more reliable to replace the fill/stroke attribute values in the SVG code itself with the color you want all at once.

Before you check with our tool, understanding how it works when you do it manually in a text editor makes it easier to verify the result. Find the fill="..." strings in the SVG code and replace them with the color code you want. To change the lines (borders) too, change the stroke="..." values as well. Just don't touch attributes whose value is none or transparent, since those mean "no paint" to begin with.

SVG Color Replacer

Paste your SVG code, pick a color, and swap it right away.

Common Pitfalls

SymptomCause
I changed it with the tool, but it doesn't show on the live siteAn SVG loaded with an <img> tag only updates when you replace the file itself. Download the changed code and overwrite the original file
Only the line (border) color stays the sameUnless you turn on the "Also change stroke color" checkbox, stroke attributes are left untouched
Only some paths don't change colorThose elements either had no fill attribute to begin with or have a value of none. They had no color originally, so this is expected
A gradient turns into a solid colorValues that referenced a gradient, like fill="url(#gradientA)", are also string-replaced, so they get overwritten with a solid color
CSS .icon { fill: red } doesn't workIf the icon has an inline style="fill:...", it has higher priority than external CSS and wins

The Takeaway

SVG icon colors usually don't change with CSS because the fill/stroke attributes set the color directly and break color inheritance. With <img> tags, it's because CSS can't get inside at all. You can switch to inline SVG and use fill: currentColor, but if you want to change several color values to the color you want all at once, replacing the values in the code is faster.

SVG Color Replacer

Paste your SVG code, pick a color, and swap it right away.

Frequently Asked Questions

Can I change the color of an icon used with an <img> tag?

An SVG loaded with <img> doesn't receive page CSS, so you can't change it with CSS. Instead, change the fill/stroke values in the SVG file itself to the color you want, then swap in that file as the <img> source.

Does it support icons that use gradients or patterns?

Because it replaces the values themselves with new colors, anything that referenced a gradient definition, such as fill="url(#gradientA)", also becomes the solid color you choose. If you want to keep the gradient effect, you need to edit the color values in the gradient defined in <defs> directly.

Why do only some pieces of an icon not change color?

Those elements either have no fill attribute to begin with or have a value of none/transparent. They're the parts that were never filled (empty space, outline-only pieces), so there's nothing to recolor.

This article was adapted from our Korean original. Read the Korean original

Related articles

  1. SVG Filter Primitives and feColorMatrix: Formulas ExplainedHow feColorMatrix recalculates RGBA with a 4×5 matrix, and how feGaussianBlur and feComponentTransfer build blur, brightness, contrast, and invert effects.
  2. SVG Filter vs CSS Filter: Where the Effect Gets SavedOne test shows the difference: CSS filter effects disappear when the file leaves the page, while SVG filters stay saved inside the file itself.
  3. SVG background patterns: tile with CSS background-repeatLearn how SVG tiles repeat with CSS background-repeat, how to code dot and grid patterns, and how to use data-URI backgrounds.
  4. Repeating Background Patterns: Why the File Size Stays TinyWhy CSS and SVG repeating background patterns are a few hundred bytes, not megabytes, and how tile size sets the spacing of dots, grid lines and stripes.
  5. SVG Wave Background: Why It Looks Different per ScreenSee why SVG wave backgrounds flatten on wide screens and sharpen on mobile, and how to predict the curve with a quick width-to-wave-count check.
  6. Tailwind color class converter: why your HEX doesn't matchSee why brand HEX values never line up with Tailwind's 242 palette colors, and how redmean color distance picks the closest class. Worked math included.

Related tools