12

我可以在 SVG 中使用带有 defs-section 的线性渐变,例如:

<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">

  <defs>
    <linearGradient id="myLinearGradient1"
                    x1="0%" y1="0%"
                    x2="0%" y2="100%"
                    spreadMethod="pad">
      <stop offset="0%"   stop-color="#00cc00" stop-opacity="1"/>
      <stop offset="100%" stop-color="#006600" stop-opacity="1"/>
    </linearGradient>
  </defs>

  <rect x="0" y="0" width="100" height="100"
     style="fill:url(#myLinearGradient1)" />

</svg>

我可以使用没有 defs-section 的线性渐变吗?我发现这样的事情:

<rect style="fill:lineargradient(foo)">
4

2 回答 2

7

<defs>仅用于结构化目的,其中的元素不会显示,但由于渐变只能在应用于形状或其他元素时可见,因此您可以在文档的任何位置定义它。

但是您仍然必须坚持正确的语法:

<rect style="fill:url(#myLinearGradient1)" ... />
于 2011-10-07T20:19:19.197 回答
2

是的,您确实可以在不需要 defs 元素的情况下拥有渐变;您只需将渐变元素放在代码中的其他任何位置,例如:

<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">

  <linearGradient id="myLinearGradient1"
                x1="0%" y1="0%"
                x2="0%" y2="100%"
                spreadMethod="pad">
    <stop offset="0%"   stop-color="#00cc00" stop-opacity="1"/>
    <stop offset="100%" stop-color="#006600" stop-opacity="1"/>
  </linearGradient>

  <rect x="0" y="0" width="100" height="100"
     style="fill:url(#myLinearGradient1)" />

</svg>
于 2019-02-09T02:39:32.413 回答