Modify SVG fill color when being served as Background-Image

后端 未结 16 1031
再見小時候
再見小時候 2020-11-22 06:31

Placing the SVG output directly inline with the page code I am able to simply modify fill colors with CSS like so:

po         


        
相关标签:
16条回答
  • 2020-11-22 07:13

    Yet another approach is to use mask. You then change the background color of the masked element. This has the same effect as changing the fill attribute of the svg.

    HTML:

    <glyph class="star"/>
    <glyph class="heart" />
    <glyph class="heart" style="background-color: green"/>
    <glyph class="heart" style="background-color: blue"/>
    

    CSS:

    glyph {
        display: inline-block;
        width:  24px;
        height: 24px;
    }
    
    glyph.star {
      -webkit-mask: url(star.svg) no-repeat 100% 100%;
      mask: url(star.svg) no-repeat 100% 100%;
      -webkit-mask-size: cover;
      mask-size: cover;
      background-color: yellow;
    }
    
    glyph.heart {
      -webkit-mask: url(heart.svg) no-repeat 100% 100%;
      mask: url(heart.svg) no-repeat 100% 100%;
      -webkit-mask-size: cover;
      mask-size: cover;
      background-color: red;
    }
    

    You will find a full tutorial here: http://codepen.io/noahblon/blog/coloring-svgs-in-css-background-images (not my own). It proposes a variety of approaches (not limited to mask).

    0 讨论(0)
  • 2020-11-22 07:14

    In some (very specific) situations this might be achieved by using a filter. For example, you can change a blue SVG image to purple by rotating the hue 45 degrees using filter: hue-rotate(45deg);. Browser support is minimal but it's still an interesting technique.

    Demo

    0 讨论(0)
  • 2020-11-22 07:15

    You can store the SVG in a variable. Then manipulate the SVG string depending on your needs (i.e., set width, height, color, etc). Then use the result to set the background, e.g.

    $circle-icon-svg: '<svg xmlns="http://www.w3.org/2000/svg"><circle cx="10" cy="10" r="10" /></svg>';
    
    $icon-color: #f00;
    $icon-color-hover: #00f;
    
    @function str-replace($string, $search, $replace: '') {
        $index: str-index($string, $search);
    
        @if $index {
            @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
        }
    
        @return $string;
    }
    
    @function svg-fill ($svg, $color) {
      @return str-replace($svg, '<svg', '<svg fill="#{$color}"');
    }
    
    @function svg-size ($svg, $width, $height) {
      $svg: str-replace($svg, '<svg', '<svg width="#{$width}"');
      $svg: str-replace($svg, '<svg', '<svg height="#{$height}"');
    
      @return $svg;
    }
    
    .icon {
      $icon-svg: svg-size($circle-icon-svg, 20, 20);
    
      width: 20px; height: 20px; background: url('data:image/svg+xml;utf8,#{svg-fill($icon-svg, $icon-color)}');
    
      &:hover {
        background: url('data:image/svg+xml;utf8,#{svg-fill($icon-svg, $icon-color-hover)}');
      }
    }
    

    I have made a demo too, http://sassmeister.com/gist/4cf0265c5d0143a9e734.

    This code makes a few assumptions about the SVG, e.g. that <svg /> element does not have an existing fill colour and that neither width or height properties are set. Since the input is hardcoded in the SCSS document, it is quite easy to enforce these constraints.

    Do not worry about the code duplication. gzip compression makes the difference negligible.

    0 讨论(0)
  • 2020-11-22 07:15

    This is my favorite method, but your browser support must be very progressive. With the mask property you create a mask that is applied to an element. Everywhere the mask is opaque, or solid, the underlying image shows through. Where it’s transparent, the underlying image is masked out, or hidden. The syntax for a CSS mask-image is similar to background-image.look at the codepenmask

    0 讨论(0)
  • 2020-11-22 07:16

    Download your svg as text.

    Modify your svg text using javascript to change the paint/stroke/fill color[s].

    Then embed the modified svg string inline into your css as described here.

    0 讨论(0)
  • 2020-11-22 07:18

    I needed something similar and wanted to stick with CSS. Here are LESS and SCSS mixins as well as plain CSS that can help you with this. Unfortunately, it's browser support is a bit lax. See below for details on browser support.

    LESS mixin:

    .element-color(@color) {
      background-image: url('data:image/svg+xml;utf8,<svg ...><g stroke="@{color}" ... /></g></svg>');
    }
    

    LESS usage:

    .element-color(#fff);
    

    SCSS mixin:

    @mixin element-color($color) {
      background-image: url('data:image/svg+xml;utf8,<svg ...><g stroke="#{$color}" ... /></g></svg>');
    }
    

    SCSS usage:

    @include element-color(#fff);
    

    CSS:

    // color: red
    background-image: url('data:image/svg+xml;utf8,<svg ...><g stroke="red" ... /></g></svg>');
    

    Here is more info on embedding the full SVG code into your CSS file. It also mentioned browser compatibility which is a bit too small for this to be a viable option.

    0 讨论(0)
提交回复
热议问题