Back to Content

Index

files/en-us/web/svg/reference/element/lineargradient/index.md

latest4.2 KB
Original Source

The <linearGradient> SVG element lets authors define linear gradients to apply to other SVG elements.

Usage context

{{svginfo}}

Attributes

  • {{SVGAttr("gradientUnits")}}
    • : This attribute defines the coordinate system for attributes x1, x2, y1, y2 Value type: userSpaceOnUse | objectBoundingBox; Default value: objectBoundingBox; Animatable: yes
  • {{SVGAttr("gradientTransform")}}
    • : This attribute provides additional transformation to the gradient coordinate system. Value type: <transform-list>; Default value: identity transform; Animatable: yes
  • {{SVGAttr("href")}}
    • : This attribute defines a reference to another <linearGradient> element that will be used as a template. Value type: <URL>; Default value: none; Animatable: yes
  • {{SVGAttr("spreadMethod")}}
    • : This attribute indicates how the gradient behaves if it starts or ends inside the bounds of the shape containing the gradient. Value type: pad | reflect | repeat; Default value: pad; Animatable: yes
  • {{SVGAttr("x1")}}
    • : This attribute defines the x coordinate of the starting point of the vector gradient along which the linear gradient is drawn. Value type: <length>; Default value: 0%; Animatable: yes
  • {{SVGAttr("x2")}}
    • : This attribute defines the x coordinate of the ending point of the vector gradient along which the linear gradient is drawn. Value type: <length>; Default value: 100%; Animatable: yes
  • {{SVGAttr("xlink:href")}} {{deprecated_inline}}
    • : An <IRI> reference to another <linearGradient> element that will be used as a template. Value type: <IRI>; Default value: none; Animatable: yes
  • {{SVGAttr("y1")}}
    • : This attribute defines the y coordinate of the starting point of the vector gradient along which the linear gradient is drawn. Value type: <length>; Default value: 0%; Animatable: yes
  • {{SVGAttr("y2")}}
    • : This attribute defines the y coordinate of the ending point of the vector gradient along which the linear gradient is drawn. Value type: <length>; Default value: 0%; Animatable: yes

DOM Interface

This element implements the {{domxref("SVGLinearGradientElement")}} interface.

Example

Basic linear gradient

css
html,
body,
svg {
  height: 100%;
}
html
<svg
  viewBox="0 0 10 10"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <linearGradient id="myGradient" gradientTransform="rotate(90)">
      <stop offset="5%" stop-color="gold" />
      <stop offset="95%" stop-color="red" />
    </linearGradient>
  </defs>

  <!-- using my linear gradient -->
  <circle cx="5" cy="5" r="4" fill="url('#myGradient')" />
</svg>

{{EmbedLiveSample('Basic linear gradient', 150, '100%')}}

Repeating angled gradient

In this example, the two {{SVGElement("rect")}} elements have different aspect ratios, but the angle of the gradient is the same.

css
html,
body,
svg {
  height: 100%;
}
html
<svg
  viewBox="0 0 500 500"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <linearGradient
      id="grad"
      x1="0"
      y1="0"
      x2="20"
      y2="20"
      spreadMethod="repeat"
      gradientUnits="userSpaceOnUse">
      <stop offset="50%" stop-color="red" />
      <stop offset="50%" stop-color="gold" />
    </linearGradient>
  </defs>
  <rect width="100%" height="25%" fill="url(#grad)" />
  <rect width="100%" height="65%" fill="url(#grad)" y="30%" />
</svg>

{{EmbedLiveSample('Repeating angled gradient', 500, '100%')}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}