html

How to Change the Color of an SVG Element?

Scalable Vector Graphics, sometimes known as SVG, is a two-dimensional vector picture format that can be used to generate animations. An SVG element is a container that specifies a new coordinate system. Moreover, the XML format specifies the SVG document.

SVG is responsible for making browser animations more accessible and useful in the modern world. 2D games may be created in an HTML document using an SVG file. It provides several ways to create pathways, circles, rectangles, and other forms. More specifically, it offers event management in the document and is resolution independent.

This post will explain the method for changing the color of an SVG element.

How to Change/Modify the Color of an SVG Element?

To modify the color of an SVG element, add the “<svg>” element and then define the path with the help of the “<path>” element. Then, access these elements in CSS and apply the “fill” property, and set “display”.

For practicality, follow the given instructions below.

Step 1: Insert “<svg>” Element

Insert the “<svg>” element and add the following attributes inside the “<svg>” opening tag:

  • viewBox” is an “svg” property that is utilized for scaling the SVG element to set the coordinates as well as width and height.
  • xml: space” is applied to an element with the value “preserve”, the whitespace in that element’s content must be retained as is by the application that processes it.
  • width” and “height” properties set the size of the element in HTML.
  • enable-background” determines the accumulation of the background image that is managed.

Step 2: Add Path

Insert the “<path>” element. Then, specify the below-listed attributes:

  • The “id” attribute is used to uniquely identify the particular element. To do so, the value of the id attribute is set as “icon
  • d” defines a path to be drawn:
<svg viewBox="0 0 512 512" xml:space="preserve" width="150x" height="150px" enable-background="new 0 0 512 512">
<path id="icon" d="M256,50C142.229,50,50,142.229,50,256c0,113.77,92.229,206,206,206c113.77,0,206-92.23,206-206 C462,142.229,369.77,50,256,50z M256,417c-88.977,0-161-72.008-161-161c0-88.979,72.008-161,161-161c88.977,0,161,72.007,161,161 C417,344.977,344.992,417,256,417z M382.816,265.785c1.711,0.297,2.961,1.781,2.961,3.518v0.093c0,1.72-1.223,3.188-2.914,3.505     c-37.093,6.938-124.97,21.35-134.613,21.35c-13.808,0-25-11.192-25-25c0-9.832,14.79-104.675,21.618-143.081            c0.274-1.542,1.615-2.669,3.181-2.669h0.008c1.709,0,3.164,1.243,3.431,2.932l18.933,119.904L382.816,265.785z"/>
</svg>

Output

Step 4: Apply “fill” property

Access the path with the help of “#icon” and apply the CSS “fill” property and set the value according to your choice:

#icon {
fill: rgb(18, 3, 230);
}

Step 5: Set “display” of “svg” Element

Now, access the svg with the help of the class name as “.svg-alternate”:

.svg-alternate {
display: none;
}

Then, apply the “display” property that is utilized for determining the display behavior of an element.

Step 6: Style “svg” Element

Access the both “svg” class by their name and apply mention properties:

.svg .svg-alternate {
display: block;
width: 150px;
height: 150px;
}

Here:

  • display” is utilized for determining the display behavior of an element.
  • width” and “height” defines the size of the element.

The resultant image shows that the color of the svg image has been changed successfully:

That’s all about changing the color of an SVG element.

Conclusion

To change the color of an SVG element, add the “<svg>” element and then define the path with the help of the “<path>” element. Lastly, access these elements in CSS and apply the “fill” property, and set “display”. This post has demonstrated the method for changing the color of a svg element by applying CSS properties.

About the author

Hafsa Javed