![]() |
SVG stands for Scalable Vector Graphics and is a vector image format for two-dimensional images that may be used to generate animations. The XML format defines the SVG document. You can change the color of an SVG using the below methods: Table of Content Using the setAttribute()The setAttribute() method is used to alter the SVG element’s fill attribute to dynamically change its color by passing the value for this attribute as the second parameter to this method. Syntax:element.setAttribute('attributeName', 'value');
Example: The below code implements the setAttribute() method to change color of SVG in JavaScript. HTML
Output: Using style.fill propertyIn this approach, the color of the SVG element can be changed by using style.fill property in JavaScript by assigning it a new color value using the assignment operator. Syntax:element.style.fill = 'value';
Example: The below code uses the style.fill property to change color of the SVG onclick in JavaScript. HTML
Output: |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |