我想使用这种技术并更改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 >


当前回答

带有背景色的框元素上的SVG掩码将导致:

body{ overflow:hidden; } .icon { --size: 70px; display: inline-block; width: var(--size); height: var(--size); transition: .12s; -webkit-mask-size: cover; mask-size: cover; } .icon-bike { background: black; animation: 4s frames infinite linear; -webkit-mask-image: url(https://image.flaticon.com/icons/svg/89/89139.svg); mask-image: url(https://image.flaticon.com/icons/svg/89/89139.svg); } @keyframes frames { 0% { transform:translatex(100vw) } 25% { background: red; } 75% { background: lime; } 100% { transform:translatex(-100%) } } <i class="icon icon-bike" style="--size:150px"></i>


注意- Internet Explorer浏览器不支持SVG掩码

其他回答

如果你想使用CSS来改变颜色,你也可以使用这样的在线工具:使用CSS过滤器改变SVG颜色

如果使用一些技巧,可以用CSS改变SVG着色。 我为此写了一个小脚本。

浏览包含SVG图像的元素列表 将SVG文件加载为XML 只获取SVG部分 改变路径颜色 将src替换为修改后的SVG图像作为内联图像

$('img.svg-changeable').each(function () {
  var $e = $(this);
  var imgURL = $e.prop('src');

  $.get(imgURL, function (data) {
    // Get the SVG tag, ignore the rest
    var $svg = $(data).find('svg');

    // Change the color
    $svg.find('path').attr('fill', '#000');

    $e.prop('src', "data:image/svg+xml;base64," + window.btoa($svg.prop('outerHTML')));
  });

});

上面的代码可能无法正常工作。我已经为带有SVG背景图像的元素实现了这一点,其工作原理几乎类似于此。

但是无论如何,您必须修改这个脚本以适应您的情况。

你不应该直接在SVG图像上设置颜色,如果你想编程SVG的颜色。

在2021年,您可以使用以下CSS内容进行颜色更改。

html { --iron-icon-fill-color-1: green; --iron-icon-fill-color-2: red; --iron-icon-stroke-color: white; } svg#icon-green { fill: var(--iron-icon-fill-color-1, currentcolor); stroke: var(--iron-icon-stroke-color, none); } svg#icon-red { fill: var(--iron-icon-fill-color-2, currentcolor); stroke: var(--iron-icon-stroke-color, none); } svg#icon { fill: var(--iron-icon-fill-color-x, currentcolor); stroke: white; } <html> <body> <svg id="icon-green" xmlns="http://www.w3.org/2000/svg" width="40px" height="40px" style="vertical-align:text-bottom" viewbox="0 0 40 40"> <circle cx="20" cy="20" r="18" stroke-width="3"/> <path d="M 10,10 30,30 M 10,30 30,10" fill="none" stroke-width="6"/> </svg> <svg id="icon-red" xmlns="http://www.w3.org/2000/svg" width="40px" height="40px" style="vertical-align:text-bottom" viewbox="0 0 40 40"> <circle cx="20" cy="20" r="18" stroke-width="3"/> <path d="M 10,10 30,30 M 10,30 30,10" fill="none" stroke-width="6"/> </svg> <svg id="icon" xmlns="http://www.w3.org/2000/svg" width="40px" height="40px" style="vertical-align:text-bottom" viewbox="0 0 40 40"> <circle cx="20" cy="20" r="18" stroke-width="3"/> <path d="M 10,10 30,30 M 10,30 30,10" fill="none" stroke-width="6"/> </svg> </body> </html>

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

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

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

最简单的方法是使用https://icomoon.io/app/#/select之类的服务从SVG创建字体。上传你的SVG,点击“生成字体”,包括字体文件和CSS内容到你的侧,只是使用和样式它像任何其他文本。我总是这样使用它,因为它使造型更容易。

但正如@CodeMouse92评论的文章中提到的,图标字体会破坏屏幕阅读器(而且可能不利于SEO)。所以还是坚持使用svg吧。