Manipulating external svg file style properties with CSS

后端 未结 2 339
旧巷少年郎
旧巷少年郎 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.

    <svg xmlns="http://www.w3.org/2000/svg" style="width:0;height:0;visibility:hidden;">
        <symbol viewBox="0 0 1500 828" id="icon-arrow-3pt-down">
            <title>arrow-3pt-down</title>
            <path d="M1500 0H0l738.9 827.7z"/>
        </symbol>
    </svg>
    

    test.html

    <button class="button--large">
        Large button
        <svg class="svg" width="20px" height="20px">
            <use xlink:href="icon-sprite.svg#icon-arrow-3pt-down"></use>
        </svg>
    </button>
    

    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

    0 讨论(0)
  • 2020-12-09 16:27

    This is in my opinion the greatest flaw in svg: sandboxing.

    Svg files are sandboxed: in their own document, which is why a typical 'fill:' style will not apply. Likewise, the css you write in your svg will not apply to the rest of your site.

    Adding css directly to an svg: Not a good solution as you will end up rewriting the css in every svg you use.

    The real solution: An "icon-system". Svg font-face or svg sprites. Read more about them here.

    The reason opacity works: Opacity applies to the svg object/frame itself, not the contents of the svg (which are inaccessible).

    I should also note that no matter how you load those svg's, inline, by reference, in an object, as a background, you will not be able to get inside the sandbox. This is why converting them to a font or using sprites is necessary for using hover, focus, and other effects/transitions.

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