Manipulating external svg file style properties with CSS

后端 未结 2 346
旧巷少年郎
旧巷少年郎 2020-12-09 16:01

I am trying to manipulate an external .svg file via CSS.

HTML


    
2条回答
  •  借酒劲吻你
    2020-12-09 16:23

    This is possible providing the SVG is hosted on the same domain (thanks @FabienSnauwaert) and it does not have a fill colour defined on itself, and you do not contain a parent selector within the CSS. For example:

    I have the following files:

    • icon-sprite.svg (my external sprite of SVGs)
    • buttons.scss
    • test.html

    icon-sprite.svg

    I have omitted the other SVGs for clarity.

    
        
            arrow-3pt-down
            
        
    
    

    test.html

    
    

    buttons.scss

    .svg {
        fill: red;
    }
    

    This would not work if I was to use body .svg due to shadow DOM boundaries.

    See this CSS Tricks article for more info

提交回复
热议问题