我想使用这种技术并更改SVG颜色,但到目前为止我还不能这样做。我在CSS中使用这个,但我的图像总是黑色的,无论如何。

我的代码:

.change-my-color { 填充:绿色; } svg < > <image class="change-my-color" xlink:href="https://svgur.com/i/AFM.svg" width="96" height="96" src=" ppngback .png" /> < / svg >


当前回答

2020的答案

CSS过滤器适用于所有当前的浏览器

改变任何SVGs的颜色

Add the SVG image using an <img> tag. <img src="dotted-arrow.svg" class="filter-green"/> To filter to a specific color, use the following Codepen (click here to open the codepen) to convert a hexadecimal color code to a CSS filter: For example, output for #00EE00 is filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%); Add the CSS filter into this class. .filter-green{ filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%); }

其他回答

最简单的技巧是在页面加载时使用jQuery改变颜色。

      $(document).ready(function () { 
              $('svg').find('path').attr('fill', '#FFF');
      });

#FFF是你想要设置的颜色代码。

一个很好的方法是使用mixin来控制笔画颜色和填充颜色。我的“svg”被用作图标。

@mixin icon($color, $hoverColor) {
    svg {
        fill: $color;

        circle, line, path {
            fill: $color
        }

        &:hover {
            fill: $hoverColor;

            circle, line, path {
                fill: $hoverColor;
            }
        }
    }
}

然后你可以在你的SCSS文件中执行以下操作:

.container {
    @include icon(white, blue);
}

最短的bootstrap兼容方式,没有JavaScript:

.cameraicon {
    height: 1.6em; /* Set your own icon size */
    mask: url(/camera.svg); /* Path to your image */
   -webkit-mask: url(/camera.svg) no-repeat center;
}

像这样使用它:

<td class="text-center">
    <div class="bg-secondary cameraicon"/><!-- "bg-secondary" sets actual color of your icon -->
</td>

简单地改变SVG文件的颜色:

转到SVG文件,在styles下面,在fill中提到颜色:

<style>.cls-1{fill: #FFFFFF;}</style>

您可以使用字体图标来使用SVG中的任何CSS选项

我正在寻找一种方法来拥有任何CSS选项,如SVG的动画,我最终用我的SVG生成一个字体图标,然后在一个跨度内使用它(如font Awesome),因此任何CSS选项,如着色,都是可用的。

我使用https://icomoon.io将SVG图像转换为字体图标。然后你可以像Font Awesome或MaterialIcon一样在HTML元素中使用它。