Modify SVG fill color when being served as Background-Image

后端 未结 16 1034
再見小時候
再見小時候 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: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: '';
    
    $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, '

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

    This code makes a few assumptions about the SVG, e.g. that 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.

提交回复
热议问题