How can I refer to an internal gradient definition inside an SVG sprite symbol?

后端 未结 2 486
眼角桃花
眼角桃花 2021-01-30 17:48

SUMMARY: An SVG sprite contains five icon blocks, one of which references its own gradient definition by ID. It is no longer able to find t

相关标签:
2条回答
  • 2021-01-30 18:12

    Firstly please note the edit to my question - whereupon I discover that the use of display: none to hide the SVG symbols until we need them was the problem.

    I kept fiddling and settled upon this "answer", which is far from perfect, but should still be reliable for any such situation.

    All you need to do is wrap the entire <svg> code in a <div> container which must be displayed but will never affect layout, so I've just done this via mega overkill CSS such as:

    height: 0; width: 0; position: absolute; visibility: hidden;

    And this works great. See the final fiddle: http://jsfiddle.net/Qtq24/5/

    If anyone has a better solution, I'd love to hear it as this feels like a bit of a hacky way of doing it but I guess no more hacky than having to use display: none; anyway.

    0 讨论(0)
  • 2021-01-30 18:24

    Don't use style="display: none;" in SVG. You have it on the root <svg> element. Either visibility:hidden, height/width="0" or <defs> are better alternatives.

    There used to be a bug in Firefox with gradient elements in symbols. That bug was fixed many versions ago now. The original code works as expected.

    <svg width="30" height="21">
    <symbol id="youtube" viewBox="0 0 400 281.641">
      <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="200.4204" y1="2.6162" x2="200.4204" y2="278.9292">
        <stop  offset="0" style="stop-color:#E52D27"/>
        <stop  offset="1" style="stop-color:#BF171D"/>
      </linearGradient>
        <path id="Triangle" fill="#FFFFFF" d="M159.845,191.73l106.152-54.999L159.845,81.348V191.73z"/>
        <path id="The_Sharpness" opacity="0.12" fill-rule="evenodd" clip-rule="evenodd" fill="#420000" d="M159.845,81.348l93.091,62.162
        l13.061-6.778L159.845,81.348z"/>
        <g id="Lozenge">
            <g>
                <path fill="url(#SVGID_1_)" d="M392.928,62.226c0,0-3.839-27.073-15.617-38.995C362.371,7.583,345.626,7.506,337.947,6.59
                c-54.975-3.974-137.441-3.974-137.441-3.974h-0.171c0,0-82.464,0-137.44,3.974c-7.68,0.916-24.419,0.993-39.364,16.641
                C11.753,35.153,7.92,62.226,7.92,62.226s-3.929,31.792-3.929,63.583v29.805c0,31.791,3.929,63.582,3.929,63.582
                s3.833,27.073,15.611,38.995c14.945,15.646,34.575,15.152,43.318,16.792c31.43,3.015,133.571,3.946,133.571,3.946
                s82.552-0.124,137.526-4.099c7.679-0.915,24.424-0.993,39.363-16.64c11.778-11.922,15.617-38.995,15.617-38.995
                s3.923-31.791,3.923-63.582v-29.805C396.851,94.017,392.928,62.226,392.928,62.226z M159.863,191.73l-0.018-110.383
                l106.152,55.384L159.863,191.73z"/>
            </g>
        </g>
    </symbol>
    
    <use xlink:href="#youtube" width="30" height="21" />
    </svg>

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