乐闻世界logo
搜索文章和话题

How can i change the color of an svg element

4个答案

1
2
3
4

In SVG (Scalable Vector Graphics), changing the color of elements can be achieved through several different methods. The following are some commonly used approaches:

1. Change Color Directly in SVG Code

You can directly specify the color using the fill attribute on SVG elements. For example, to change the color of a rectangle, you can do the following:

xml
<svg width="100" height="100"> <rect width="100" height="100" fill="blue" /> </svg>

Change the value of the fill attribute from blue to any other valid color value, such as red, #FF0000, or rgb(255,0,0).

2. Change Color Using CSS

The color of SVG elements can also be controlled via CSS. Apply a class to the SVG element and define the style in CSS. For example:

xml
<svg width="100" height="100"> <rect width="100" height="100" class="my-rectangle" /> </svg>

Then, in CSS:

css
.my-rectangle { fill: green; }

Modify the corresponding CSS class to change the color, which provides flexibility for adjusting colors in different states.

3. Change Color Dynamically Using JavaScript

For interactive changes or event-driven updates, use JavaScript to dynamically modify the fill attribute. For example:

html
<svg width="100" height="100"> <rect id="my-rectangle" width="100" height="100" fill="yellow" /> </svg> <script> var rect = document.getElementById('my-rectangle'); rect.addEventListener('click', function() { rect.setAttribute('fill', 'purple'); }); </script>

In this code, clicking the rectangle changes its color from yellow to purple.

4. Change Color Using Inline Styles

You can also directly change the color using inline styles on SVG elements:

xml
<svg width="100" height="100"> <circle cx="50" cy="50" r="40" style="fill: orange;" /> </svg>

Adjust the fill value within the style attribute to alter the circle's color.

Finally, these methods provide effective ways to change SVG element colors. When implementing them, ensure the chosen color values adhere to SVG-supported formats, including color names, hexadecimal codes, or RGB/RGBA representations.

2024年6月29日 12:07 回复

If you want to dynamically change the color:

  1. Open the SVG in a code editor
  2. Add or override the fill attribute for each path element to fill="currentColor"
  3. Now, the SVG will adopt the font color, so you can apply the following CSS:
css
svg { color: "red"; }
2024年6月29日 12:07 回复

To change the color of any SVG element, you can directly edit the SVG code by opening the SVG file in any text editor.

The code may resemble the following:

xml
<?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve"> <g> <path d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223 C181.113,454.921,171.371,464.663,161.629,474.404z"/> /* Some more code goes on */ </g> </svg>

You can observe various XML tags such as paths, circles, and polygons. You can add your own colors using the style attribute. See the example below:

xml
<path fill="#AB7C94" d="M114.26,436.584L99.023,483h301.953l-15.237-46.416H114.26z M161.629,474.404h-49.592l9.594-29.225h69.223 C181.113,454.921,171.371,464.663,161.629,474.404z"/>

Add the style attribute to all relevant tags to achieve the desired color for the SVG.

According to Daniel's comment, we can directly use the fill attribute instead of specifying the fill property within the style attribute.

2024年6月29日 12:07 回复

CSS Filters are supported by all current browsers

How to Change Any SVG Color

  1. Use the <img> tag to embed an SVG image.
html
<img src="dotted-arrow.svg" class="filter-green"/>
  1. To apply a filter for a specific color, use the following Codepen (click here to open Codepen) to convert hexadecimal color codes into CSS filter values:

For example, output #00EE00 as

css
filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%);

Here to generate the filter for any color.

  1. Apply the CSS filter property to this class.
css
.filter-green{ filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%); }
2024年6月29日 12:07 回复

你的答案