Modify SVG fill color when being served as Background-Image

后端 未结 16 1206
再見小時候
再見小時候 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:

    
    
    
    
    

    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).

提交回复
热议问题