How to change the color of an SVG icon with CSS
· 4 min read
The short answer: set the icon's stroke or fill to currentColor, then change the CSS color of the icon or its parent. The rest of this guide explains why that works, when it doesn't, and how to handle hover states, dark mode and icons you can't edit.
The examples use Iconkeep icons, which are line icons drawn with a stroke. The same ideas apply to any SVG.
How SVG color works
An SVG shape has two paint properties:
fillpaints the inside of a shape.strokepaints its outline.
Solid icons draw with fill. Line icons, also called outline icons, draw with stroke and set fill="none". Before you change a color, check which one your icon uses. Setting fill on a line icon fills in every closed shape and turns it into a blob.
Here's a typical line icon from Iconkeep, the heart:
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48"
fill="none" stroke="currentColor" stroke-width="1.5"
stroke-linecap="round" stroke-linejoin="round">
<path d="…" />
</svg>
fill="none" keeps the inside empty, and stroke="currentColor" paints the line.
Method 1: currentColor (recommended)
currentColor is a CSS keyword that means "the value of the color property here". When an icon's stroke is currentColor, the icon simply takes the text color of wherever you put it.
<a class="link" href="/cart">
<svg …stroke="currentColor"…>…</svg>
Cart
</a>
.link { color: #4f7d00; }
.link:hover { color: #16191d; }
The icon and the word "Cart" now share one color, including on hover, and you never style the SVG directly. This is why most icon libraries ship icons with currentColor, and why every SVG you download or copy from Iconkeep uses it.
When the icon has a fixed color, edit the file once and replace the value:
<!-- before -->
<path stroke="#000000" … />
<!-- after -->
<path stroke="currentColor" … />
Method 2: set fill or stroke in CSS
If you'd rather not edit the file, CSS can override the paint for an inline SVG:
.icon path { stroke: #e04f3a; } /* line icons */
.icon-solid path { fill: #e04f3a; } /* solid icons */
CSS rules win over presentation attributes like stroke="#000" in the markup, so this works even on icons with hard-coded colors. It doesn't override inline style="stroke: …" attributes unless you add !important.
Use this method when an icon has several parts and you want different colors on purpose, for example a two-tone icon where one path gets the accent color.
Changing color on hover and focus
With currentColor you only style the parent:
.icon-button { color: #8c949f; transition: color .15s; }
.icon-button:hover,
.icon-button:focus-visible { color: #c8f560; }
Add :focus-visible alongside :hover so keyboard users get the same feedback as mouse users.
Dark mode
Because the icon follows the text color, dark mode needs no extra icon rules. Change the text color and the icons follow:
:root { --text: #16191d; }
@media (prefers-color-scheme: dark) {
:root { --text: #e8ebee; }
}
body { color: var(--text); }
The same works for a manual theme switch with a data-theme attribute instead of the media query.
When the SVG is an img tag or a background image
CSS can't reach inside an SVG loaded as <img src="icon.svg"> or background-image: url(icon.svg). The browser treats the file as a finished picture, and currentColor inside it falls back to black. You have three options:
- Inline the SVG. Paste the SVG code into the HTML. This is the most flexible option and the one this guide assumes. Our guide to using SVG icons in HTML compares it with sprites,
<img>and the other methods. - Use a CSS mask. The SVG file becomes the shape, and the element's background provides the color:
.icon-mask {
width: 24px; height: 24px;
background-color: currentColor;
-webkit-mask: url(/icons/heart.svg) center / contain no-repeat;
mask: url(/icons/heart.svg) center / contain no-repeat;
}
This keeps the icon as a cached file and still follows the text color. It works in all current browsers.
- Save a colored copy. For emails, documents and platforms that only accept images, download the icon already colored. On Iconkeep, pick a color with the color control before you click Download; the SVG and PNG both use it.
Quick comparison
| Method | Works with | Hover and dark mode | Best for |
|---|---|---|---|
currentColor |
Inline SVG | Automatic | Most websites and apps |
CSS fill / stroke |
Inline SVG | Needs its own rules | Multi-color or hard-coded icons |
| CSS mask | SVG files | Automatic | Cached icon files, CSS-only setups |
| Pre-colored download | Anywhere | No | Email, slides, print, social posts |
Common problems
The icon turns into a solid shape. You set fill on a line icon. Set stroke instead and keep fill: none.
Nothing changes. Check that the SVG is inline, not an <img>, and look for an inline style attribute on the paths that overrides your CSS.
The line looks thinner or thicker than expected. Stroke width scales with the icon. A 1.5px stroke on a 48px icon is about 0.75px at 24px. If you show icons small, raise stroke-width or download them with a thicker stroke. Our icon size guidelines have a table of stroke values for each size.
Frequently asked questions
Why doesn't the CSS color property change my SVG icon?
The color property only affects an SVG whose fill or stroke is set to currentColor. If the file has a fixed value such as fill="#000", replace it with currentColor or override fill or stroke in CSS. CSS also cannot reach inside an SVG loaded with an img tag or as a background image.
Should I change fill or stroke?
Change whichever one the icon draws with. Line (outline) icons draw with stroke and have fill="none"; solid icons draw with fill. Iconkeep icons are line icons, so their color comes from the stroke, which is set to currentColor.
How do I change the color of an SVG used in an img tag?
You can't style its inside from the page. Inline the SVG instead, or use the SVG file as a CSS mask on an element and set that element's background-color, which works in all current browsers.
How do I make SVG icons follow dark mode?
Set the icon's stroke or fill to currentColor and give the parent text a color that changes in dark mode, for example with a CSS variable inside a prefers-color-scheme media query. The icon then switches together with the text.