Lunchbox Hands

svg

How to Change SVG Colors: Attributes, CSS and currentColor

Where an SVG actually stores its colors (fill attributes, inline styles, Illustrator class rules, gradient stops, or nothing at all), which one wins, and how to recolor an icon whether it is inlined, in an img tag, or a CSS background.

By ·

You set fill: red in your CSS and the icon stays blue. You change the fill attribute in the file and nothing happens either. SVG colors are not hard, but the same color can live in five different places, they override each other in a fixed order, and how the SVG gets onto the page decides which of them you can reach at all. Once you know which case you are in, the fix is usually one line.

Where the color actually lives

Open the SVG in a text editor and look for the color. It will be in one (or several) of these:

WhereLooks likeUsually from
Presentation attribute<path fill="#ed1c24" stroke="#000">Figma, Sketch, most icon libraries
Inline style<path style="fill:#ed1c24;stroke:none">Inkscape
Rule in a <style> block.cls-1 { fill: #ed1c24; }Adobe Illustrator (Internal CSS styling)
Gradient stop<stop offset="0" stop-color="#ed1c24"/>Any gradient fill (fill="url(#grad)")
Nowhere<path d="M0 0h24v24H0z"/>Font Awesome and other one-color icon sets

That last row catches people out. A shape with no fill anywhere is not transparent: the initial value of fill is black, so an SVG with no color in it at all renders as a black icon. (The initial value of stroke is none, which is why stroke-only icons need an explicit stroke.)

Gradients are the other trap. If a shape says fill="url(#sky)", the color is not on the shape at all. It is on the <stop> elements inside the <linearGradient id="sky">, and that is what you need to edit.

Which one wins

When more than one source sets the same property on the same element, the order is:

  1. Inline style attribute (highest, short of !important)
  2. CSS rules that match the element, whether from the SVG’s own <style> block or, for an inlined SVG, from your page’s stylesheet
  3. Presentation attributes like fill="…", which count as author styles with zero specificity
  4. Inherited value from a parent element
  5. Initial value (black for fill)

Two consequences explain most “my color change did nothing” moments:

  • A fill attribute loses to any CSS rule that matches the element. If an Illustrator file has .cls-1 { fill: #ed1c24 } and the path has class="cls-1" fill="blue", the path is red.
  • Inheritance is weaker than everything above it. svg { fill: red } in your page CSS colors only the shapes that don’t set their own fill. A <path fill="#333"> keeps its gray, because its own presentation attribute beats a value inherited from the parent. To override it you need a rule that matches the path itself, such as .icon path { fill: red }.

How the SVG is placed changes everything

Placed asCan page CSS recolor it?How to change the color
Inline <svg> in the HTML, or a React/Vue componentYescurrentColor, or CSS rules targeting its shapes
<img src="icon.svg">NoEdit the file, or use the CSS mask technique below
CSS background-image: url(icon.svg)NoEdit the file, or switch to a mask
<object> / <iframe>Only with script access to its documentEdit the file

An SVG loaded through <img> or background-image is rendered as a separate, sealed document. Your stylesheet cannot see inside it, color on the parent element does nothing, and currentColor inside it resolves to black. (We measured that last one while writing why SVGs convert to blank or black PNGs.) For these, the color has to be changed in the file itself, which is what the SVG Color Changer does: it lists every color in the file as a swatch, including the implicit black, and rewrites only the color values.

currentColor: one icon, any color

For icons that are inlined, the best fix is to stop hard-coding the color. currentColor is a CSS keyword meaning “the current value of the color property”, that is, the text color:

<button class="danger">
  <svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true">
    <path fill="currentColor" d="M12 2 1 21h22L12 2zm1 15h-2v-2h2v2zm0-4h-2V9h2v4z"/>
  </svg>
  Delete
</button>
.danger { color: #b91c1c; }
.danger:hover { color: #7f1d1d; }

The icon now follows the button’s text color on hover, in dark mode, and anywhere else the text color changes, with no icon-specific CSS. Stroke-based icon sets (Lucide, Heroicons outline, Feather) use the same idea on the root element: fill="none" stroke="currentColor".

To convert an existing icon, replace every hard-coded fill and stroke with currentColor. The Color Changer’s All to currentColor button does it in one step, including adding fill="currentColor" to shapes that had no fill. Then SVG to React turns it into a component. Only do this for one-color icons; a two-tone illustration collapses into a single color.

Recoloring an SVG used as an image or background

If the file has to stay an <img> or a CSS background, there are three options.

Edit the colors in the file. This is the only exact option for multi-color artwork. If you need several variants (brand, white-on-dark, grayscale), export one file per variant.

Use it as a mask. For one-color icons, draw the color with CSS and use the SVG only for its shape:

.icon-search {
  width: 1.25rem;
  height: 1.25rem;
  background-color: currentColor;
  mask: url(/icons/search.svg) center / contain no-repeat;
}

The element is painted in its background-color wherever the SVG is opaque. Because the background here is currentColor, the icon follows the text color just like an inline SVG, and the SVG’s own colors no longer matter. mask works unprefixed in all current browsers.

Put a media query inside the SVG. A <style> block inside the file still runs when the SVG is an image, and prefers-color-scheme works there. This is how SVG favicons switch between light and dark:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  <style>
    path { fill: #111; }
    @media (prefers-color-scheme: dark) { path { fill: #eee; } }
  </style>
  <path d="M4 4h24v24H4z"/>
</svg>

It follows the operating system’s theme, not a theme toggle on your site, because the image cannot see your page’s classes.

What we would avoid is the CSS filter trick (chains of invert(), sepia(), saturate() and hue-rotate() tuned to land near a target color). It only approximates the color, shifts every color in a multi-color image, and breaks when the source color changes.

Illustrator class names collide when inlined

A <style> block inside an inlined SVG is not scoped to that SVG. It applies to the whole page. Illustrator names its classes .cls-1, .cls-2 and so on in every file, so if you inline two Illustrator SVGs on one page, the second file’s .cls-1 { fill: … } recolors shapes in the first. The symptoms look random: an icon turns the wrong color only on pages where another icon appears.

Fixes, from least to most work: in Illustrator’s SVG export options, set Styling to Presentation Attributes instead of Internal CSS; give each file unique class names; or run the file through a tool that inlines the styles. Our SVG Optimizer runs SVGO, whose default preset includes inlineStyles, which moves styles from a <style> block onto the elements when a rule matches only once. Classes reused by several elements may stay in the <style> block, so check the output. For SVGs used as <img> this does not matter, because each image is its own document.

A quick checklist

  1. Find the color. Search the file for fill, stroke, stop-color and <style>. If there is no color at all, the shapes are default black.
  2. Check how the SVG is placed. Inline means CSS can reach it; <img> and backgrounds mean edit the file or use a mask.
  3. For one-color icons, prefer currentColor inline, or a mask for images.
  4. For multi-color artwork, change the values in the file. The SVG Color Changer shows every color with a usage count, highlights where each is used, and keeps the rest of the file byte for byte.
  5. Check contrast for any icon that carries meaning on its own, such as a status icon with no text, with the Contrast Checker. WCAG asks for 3:1 against the background for meaningful graphics.

For the bigger decision of whether an SVG should stay a file, become a PNG, or become a component, see SVG Optimizer vs SVG to PNG vs SVG to React.