Why does svg referencing an element with clip-path not work?

后端 未结 2 824
自闭症患者
自闭症患者 2021-01-03 04:11

When implementing an SVG sprite, an element is created and svg elements are referenced via the element. The containing

相关标签:
2条回答
  • 2021-01-03 04:26

    Use <svg style="width:0; height:0;"> instead of <svg style="display: none;"> to hide the sprite.

    <!-- SVG element  -->
    
    <svg id="svg-test" style="width:0; height:0;">
      <clipPath id="my-clip-1">
        <circle id="circle-1" cx="50" cy="50" r="50" />
      </clipPath>
      <path id="svg-test-reference" clip-path="url(#my-clip-1)" d="M10-39.288h80v80H10z" />
    </svg>
    
    <!-- Reference SVG <path> by ID with Use -->
    
    <svg class="svg-item" viewBox="0 0 100 100">
      <use xlink:href="#svg-test-reference" />
    </svg>
    

    Live example on Codepen.io

    0 讨论(0)
  • 2021-01-03 04:36

    In my case, using only "width" and "height" equals to zero, left a void area where should be the image. However, using display:content; instead display:none; works fine, no void area and no image.

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