How to change SVG color?


Scalable Vector Graphics (SVG) have gained widespread popularity as a format for producing high-quality vector graphics that can be resized to any extent without any loss in resolution. An added benefit of employing SVG is its capability to alter the color of the graphic to match specific preferences. If you seek to harmonize the hues of your website or fine-tune the chromatic palette for a particular objective, modifying the color of an SVG can be readily accomplished with CSS. This article endeavors to lead you through the steps of altering the color of an SVG, starting with pinpointing specific elements and ending with tweaking the colors themselves. Whether you identify as a web designer, a developer, or an inquisitive learner keen to personalize their SVG graphics, this article pledges to furnish you with all the requisite knowledge to embark on your journey.

Using CSS to Target SVG Elements

CSS, which stands for Cascading Style Sheets, is an influential instrument used to style HTML and can also be applied to alter the styles of SVG graphics. To modify the color of an SVG, you can utilize CSS selectors to aim at the exact elements that require modification. For instance, if you wish to alter the fill color of all the paths in the SVG, you can employ the following CSS regulation −

svg path {
   fill: red;
}

This will change the fill color of all the paths in the SVG to red. You can also use CSS to target specific elements within the SVG using their IDs or classes.

Modifying SVG Colors

It is practicable to modify the chroma of an SVG element to any officially authorized CSS shade, whether it be a customary label for a hue, a hexadecimal system, an RGB, RGBA, HSL, HSLA or other permissible chromatic standard. For instance, you could modify the color inside a path to a conventional hue, such as “verdant,” or a hexadecimal code, such as “#ff0000”. You could also specify an accurate hue by means of RGB or HSL values, like “rgb(255, 0, 0)” or “hsl(0, 100%, 50%)”. Besides adjusting the fill hue of an SVG element, it’s likewise possible to modify the stroke hue by using the stroke property as opposed to the fill property.

Overriding SVG Colors

Observe that certain SVG files may contain inline styles or hardcoded colors which could supersede the CSS regulations you have defined. In such a circumstance, it may be necessary to alter the SVG file to eliminate those styles or colors. You can accomplish this by opening the SVG file using a text editor and seeking the color values that you wish to modify. Once you have located the pertinent styles or colors, you can remove them or adjust them according to your preference. However, you must exercise caution to avoid erasing any vital code or tags that might impact the SVG's functionality.

Approach

Here are the guidelines you can adhere to in order to alter the hue of an SVG −

Discerning the specific SVG element whose color you wish to alter is imperative. This can be accomplished by examining the SVG element via the developer tools present in your web browser.

Formulate a CSS declaration that aims at a specific SVG element or multiple elements you intend to modify the hue of. You may utilize element selectors such as “svg,” “path,” or “rect” to focus on certain elements, or employ class or ID selectors to aim at specific elements with more meticulousness.

In the CSS declaration, assign the fill attribute with the color of your preference to alter the SVG’s fill color. You can employ color names, hexadecimal codes, or RGB values to denote the color.

Electively, you may designate the stroke property to alter the color of an SVG’s stroke, or employ other CSS properties to fashion the SVG.

If you’re using an external CSS file, link to it in the head section of your HTML document using the link element.

Save your changes and refresh the web page to see the updated SVG color.

Example 1

The following example will show to change the color of SVG graphics using CSS

<!DOCTYPE html>
<html>
<head>
   <title>How to change SVG color?</title>
   <style>
      #my-svg path {
         fill: green;
      }
   </style>
</head>
<body>
   <h4>How to change SVG color?</h4>
   <div>
      <p>Before Color Change</p>
      <svg width="100" height="100">
         <path fill="#000000" d="M10 10 H 90 V 90 H 10 L 10 10"></path>
      </svg>
   </div>
   <br>
   <div>
      <p>After Color Change</p>
      <svg id="my-svg" width="100" height="100">
         <path fill="#000000" d="M10 10 H 90 V 90 H 10 L 10 10"></path>
      </svg>
   </div>
</body>
</html>

Example 2

The following example demostrataes the process of dynamically changing of colors of the SVG graphics using JavaScript and CSS.

<!DOCTYPE html>
<html>
<head>
   <title>How to change SVG color?</title>
   <style>
      svg {
         width: 100px;
         height: 100px;
      }
   </style>
</head>
<body>
   <h4>How to change SVG color?</h4>
   <div>
      <p>Before Color Change</p>
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
         <path fill="none" d="M0 0h24v24H0z" />
         <path
         d="M6.1 3.3L4.7 4.7l6.2 6.2L2 17.6l1.4 1.4 4.6-4.6 6.2 6.2 1.4-1.4-6.2-6.2 6.2-6.2-1.4-1.4-4.6 4.6-2.5-2.5zM12 18c-3.3 0-6-2.7-6-6s2.7-6 6-6 6 2.7 6 6-2.7 6-6 6z" />
      </svg>
   </div>
   <br>
   <div>
      <p>After Color Change</p>
      <svg id="my-svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
         <path fill="none" d="M0 0h24v24H0z" />
         <path
         d="M6.1 3.3L4.7 4.7l6.2 6.2L2 17.6l1.4 1.4 4.6-4.6 6.2 6.2 1.4-1.4-6.2-6.2 6.2-6.2-1.4-1.4-4.6 4.6-2.5-2.5zM12 18c-3.3 0-6-2.7-6-6s2.7-6 6-6 6 2.7 6 6-2.7 6-6 6z" />
      </svg>
   </div>
   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
   <script>
      $(document).ready(function () {
         $('#my-svg path').css('fill', 'red');
      });
   </script>
</body>
</html>

Conclusion

In summation, altering the hue of an SVG image can be a straightforward and potent method of personalizing your graphics to suit your particular requirements. Through the utilization of CSS, you can conveniently modify the tints of specific elements in your SVG, or the entire image itself. By following the guidelines presented in this article, you should now possess a thorough comprehension of how to modify the hue of your SVG images and can apply this knowledge to create more aesthetically pleasing designs for your web page or undertaking. Remember that SVG images offer several other advantages over other picture formats, such as their adjustability and flexibility, so integrating them into your designs can significantly elevate your web development prowess. We anticipate that this article has been informative and that you are now equipped to experiment with your own SVG color palettes.

Updated on: 10-Apr-2023

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements