【问题标题】:SVG not rendered properly when using USE tag使用 USE 标签时 SVG 未正确呈现
【发布时间】:2016-11-11 07:30:22
【问题描述】:

使用 use 标签时,我的 SVG 渲染不正确。尝试使用相同的 SVG 标签而不使用 use 标签,这次我很幸运,它可以正常工作。

不幸的是,我需要 use 标签来正确渲染我的 SVG。我不知道我做错了什么。请参考下面的代码

   <!-- SVG which I'm trying to reuse using `use` tag--->

<svg xmlns="http://www.w3.org/2000/svg" height="17" width="17" style="display:none">
      <symbol id="check">
        <defs>
          <rect id="addColumn_a" width="16" height="16" y=".996" rx="1"/>
          <mask id="addColumn_b" width="16" height="16" x="0" y="0" fill="white">
            <use xlink:href="#addColumn_a"/>
          </mask>
        </defs>
        <g fill="none" fill-rule="evenodd" transform="translate(0 -.996)">
          <rect width="16" height="1" y="6" fill="#404040"/>
          <rect width="16" height="1" y="11" fill="#404040"/>
          <rect width="15" height="1" x="3.028" y="8.565" fill="#404040" transform="rotate(-90 10.528 9.065)"/>
          <rect width="15.348" height="1" x="-2.141" y="8.554" fill="#5C5C5C" transform="rotate(-90 5.533 9.054)"/>
          <use stroke="#404040" stroke-width="2" mask="url(#addColumn_b)" xlink:href="#addColumn_a"/>
          <rect width="16" height="6" x="-5" y="5.996" fill="#19AF5C" opacity=".651" transform="rotate(-90 3 8.996)" rx="1"/>
        </g>
      </symbol>
</svg> 

 <!-- USE Link reference which is not rendering SVG properly--->

<svg height="17" width="17">
      <use xlink:href="#check"></use>
</svg>


 <!-- Same SVG tag just a small change removed `symbol` tag--->


<svg height="17" width="17">
      <defs>
        <rect id="a" width="16" height="16" y=".996" rx="1"/>
        <mask id="b" width="16" height="16" x="0" y="0" fill="white">
          <use xlink:href="#a"/>
        </mask>
      </defs>
      <g fill="none" fill-rule="evenodd" transform="translate(0 -.996)">
        <rect width="16" height="1" y="6" fill="#404040"/>
        <rect width="16" height="1" y="11" fill="#404040"/>
        <rect width="15" height="1" x="3.028" y="8.565" fill="#404040" transform="rotate(-90 10.528 9.065)"/>
        <rect width="15.348" height="1" x="-2.141" y="8.554" fill="#5C5C5C" transform="rotate(-90 5.533 9.054)"/>
        <use stroke="#404040" stroke-width="2" mask="url(#b)" xlink:href="#a"/>
        <rect width="16" height="6" x="-5" y="5.996" fill="#19AF5C" opacity=".651" transform="rotate(-90 3 8.996)" rx="1"/>
      </g>
</svg>

P.S 我使用工具渲染了这个 SVG 代码,不是手写代码

【问题讨论】:

  • “未正确渲染”是什么意思?你指的是外面的粗线吗?

标签: css svg


【解决方案1】:

这看起来像是用 Illustrator 创建的 SVG。

出于某种原因,AI 喜欢创建带有奇怪的额外蒙版和/或剪辑路径的 SVG。它似乎总是给人们带来麻烦。使用不同的程序来创建图标可能是个好主意。例如。 Inkscape 或 Sketch。

你的问题是由于面具和符号之间的一些奇怪的相互作用。我放弃了试图弄清楚,因为重写图标以摆脱蒙版并简化它会更快。

如果您有多个图标要修复,那么我猜这个解决方案并不能真正帮助您。但不管怎样,你去吧:

<svg xmlns="http://www.w3.org/2000/svg" height="17" width="17" style="display:none">
  <symbol id="check2">
    <g fill="none" fill-rule="evenodd">
      <rect width="16" height="1" y="5" fill="#404040"/>
      <rect width="16" height="1" y="10" fill="#404040"/>
      <rect width="1" height="16" x="10" fill="#404040"/>
      <rect width="1" height="16" x="5" fill="#5C5C5C"/>
      <rect width="15" height="15" x="0.5" y="0.5" rx="0.5" stroke="#404040" stroke-width="1"/>
      <rect width="6" height="16" fill="#19AF5C" opacity=".651" rx="1"/>
    </g>
  </symbol>
</svg> 


<svg height="17" width="17">
      <use xlink:href="#check2"></use>
</svg>

【讨论】:

  • 我有多个图标需要修复。无论如何感谢这个解决方案。这对我来说不是一个好主意
  • That looks like an SVG created with Illustrator […] use a different program […] Eg. Illustrator… 第一个似乎是指一些Adobe Illustrator - 这是不同的?
  • 谢谢。这是一个错字。我的意思是我认为是 Inkscape。
猜你喜欢
  • 2014-11-16
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-29
相关资源
最近更新 更多