Linear gradients in SVG without defs

I can use linear gradient in SVG with defs-section like:

<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>

Is it possible to use a linear gradient without defs section? I find something like this:

<rect style="fill:lineargradient(foo)">
+5
source share
1 answer

<defs> required only for structuring purposes, the elements in it are not displayed, but since the gradient can only be seen when applied to a form or other element, you can define it anywhere in the document.

But you should still follow the correct syntax:

<rect style="fill:url(#myLinearGradient1)" ... />
+3
source

All Articles