简体   繁体   English

如何使用CSS更改.svg图像的颜色?

[英]How to change the color of an .svg image using css?

I am working with Sass and images that are in .svg format, I am using them as if they were .jpg (or any other) images. 我正在使用Sass和.svg格式的图像,正在使用它们,就好像它们是.jpg(或任何其他)图像。

This is the icon (a capture): the icon .jpg 这是图标(捕获): 图标.jpg

I want to know how can I make it so that its color can change using css? 我想知道如何制作它,以便可以使用CSS改变颜色?

I am using the .svg image like this: 我正在使用.svg图像,如下所示:

 .text::before { content: " "; display: block; background: url("./../Iconos/Icono\\ some-icon.svg"); background-repeat: no-repeat; background-position: center; cursor: pointer; width: 35px; height: 35px; float: right; } 
 <div class="text"> <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Doloribus assumenda error reiciendis soluta rerum corporis, illo aspernatur laboriosam accusamus nisi fugiat amet possimus cumque eum id enim dolor repudiandae expedita.</p> </div> 

if you use like this you won´t be able to change the color, you can use better in your html like this, 如果您这样使用,将无法更改颜色,可以在html中使用更好的颜色,

<img src="image.svg" /> 

and convert the svg img in code in order to have access to svg path and modify in CSS 并在代码中转换svg img以便可以访问svg路径并在CSS中进行修改

 svg path {
    fill: #783cbc;
}

You can convert with this function in javascript 您可以在javascript中使用此功能进行转换

  $('img.svg').each(function(){
         var $img = jQuery(this);
         var imgID = $img.attr('id');
         var imgClass = $img.attr('class');
         var imgURL = $img.attr('src');

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

            // Add replaced image's ID to the new SVG
            if(typeof imgID !== 'undefined') {
               $svg = $svg.attr('id', imgID);
            }
            // Add replaced image's classes to the new SVG
            if(typeof imgClass !== 'undefined') {
               $svg = $svg.attr('class', imgClass+' replaced-svg');
            }

            // Remove any invalid XML tags as per http://validator.w3.org
            $svg = $svg.removeAttr('xmlns:a');

            // Check if the viewport is set, if the viewport is not set the SVG wont't scale.
            if(!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
               $svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'))
            }

            // Replace image with new SVG
            $img.replaceWith($svg);

         }, 'xml');

      });

But maybe what you want to do is make a font with the icons in order to change the color easily, in that case there are several options to do that, you can do it with https://icomoon.io/ or if you are using grunt you can use https://github.com/sapegin/grunt-webfont 但是也许您想做的就是用图标制作字体,以便轻松更改颜色,在这种情况下,有几种选择,您可以使用https://icomoon.io/进行操作,或者使用grunt可以使用https://github.com/sapegin/grunt-webfont

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM