Looking to change the color of an SVG image in CSS? You're not alone. SVGs (Scalable Vector Graphics) are an excellent choice for graphics that need to scale without losing quality. But when it comes to changing their colors, it might not be immediately obvious how to do it. Let's dive into this CSS magic and learn how to make your SVGs pop with the perfect hue.

First, let's understand that SVGs are essentially XML files that can be manipulated like any HTML. This means you can target them with CSS, including their colors. But before we get started, make sure your SVG is either inline in your HTML or referenced as <img> or <object> with an ID or class, making it selectable in CSS.

Using CSS Fill Property
The first and usually simplest way to change an SVG's color is to use the CSS fill property. The fill property applies to the main color of the SVG shape. It's like painting the insides of your shapes with the color of your choice.

Let's say you have an inline SVG like this:
<svg viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"><circle cx="7" cy="7" r="7" fill="#000" /></svg>

To change the color of this circle to blue, you would use:
circle { fill: blue; }
Fill Opacity

Did you know you can also change the opacity of an SVG's fill color? The fill-opacity property allows you to make your SVG semi-transparent. For example, to make our blue circle semi-transparent:
circle { fill: blue; fill-opacity: 0.5; }
Targeting Specific SVG Parts

SVGs are made up of multiple parts, like paths and rects. So, sometimes you might want to target only specific parts of the SVG, like the border or a certain shape. This is where CSS selectors come in handy.
For instance, if you have path elements with different IDs, like:









<path id="path1" d="..." /><path id="path2" d="..." />
You can target each path separately with #path1 and #path2 CSS selectors.
Using CSS Stroke Property
Now, what if you want to change the color of the lines drawn by the SVG? That's where the stroke property comes in. It's similar to the fill property, but it applies to the outlines of your shapes.
Let's apply a red outline to our blue circle:
circle { stroke: red; }
Stroke Width
You can also change the thickness of the outline using the stroke-width property. For instance, to make the red outline thicker:
circle { stroke: red; stroke-width: 3; }
Stroke Linecap and Stroke Linejoin
These properties control how line endings and intersections are rendered. They can give your lines a more professional, consistent look.
Demonstration of usage:
circle { stroke-linecap: round; stroke-linejoin: round; }
Using CSS to Change SVG Colors Dynamically
The methods discussed so far are static changes to your SVG colors. But what if you want to change them dynamically based on user interaction or other conditions? That's where CSS functions like var(), env(), and even JavaScript come into play.
Using CSS Variables
CSS variables allow you to define colors once and reuse them throughout your stylesheets. If you want to change an SVG color, you just need to update the CSS variable. This is very useful for theme switching or responsive design.
For example, define a CSS variable:
:root { --my-color: blue; }
And then use it in your CSS:
circle { fill: var(--my-color); }
Using JavaScript
Sometimes, you might want more advanced interactivity, like changing colors based on user hovering or scrolling. This is where JavaScript comes in. You can write scripts that manipulate the CSS of your SVGs dynamically.
Here's a simple example of changing fill color on hover:
circle:hover { fill: red; }
And there you have it! You've now learned how to change the color of SVG images in CSS. This knowledge opens up a world of possibilities for dynamic and interactive SVG graphics. So go ahead, start playing with colors, and make your web pages truly pop! Experiment with different combinations and effects, and don't forget to share your creations with the community.