Gradient

You can apply gradients as fills or strokes.

There are two types of gradient in SVG: linear and radial gradient.

You would declare gradients inside the <defs> element, and you must give the gradient an id attribute so you can reference it.

Gradient SVG attributes

gradientUnits

Defines the coordinate system for the gradient. Specify either:

  • objectBoundingBox uses the size of the object that references the gradient (the coordinate values will range from 0 to 1).
  • userSpaceOnUse uses the viewport coordinates (you need to know where the object is and place the gradient coordinates accordingly).

Gradient SVG elements

<linearGradient>

  • x1start x coordinate (default is 0%)
  • y1start y coordinate (default is 100%)
  • x2end x coordinate (default is 0%)
  • y2end y coordinate (default is 0%)
  • hrefreference to another <linearGradient> that will be used as a template
  • gradientUnitsdefines the coordinate system for attributes x1, y1, x2, y2: specify userSpaceOnUse (use the viewport coordinate) or objectBoundingBox (use the bounding box of the element that references this gradient)
  • spreadMethoddefines how the shape is filled beyond the defined edges of the gradient; specify pad (just use the edge color, default), reflect, or repeat
  • gradientTransformspace-separated CSS transform functions, e.g. skewX(20) translate(-35, 0)
| LIVE PREVIEW

<radialGradient>

  • cxx coordinate of the end circle's center (default is 50%)
  • cyy coordinate of the end circle's center (default is 50%)
  • rradius of the end circle (default is 50%)
  • fxx coordinate of the start circle's center (default is the same as cx)
  • fyy coordinate of the start circle's center (default is the same as cy)
  • frradius of the start circle (default is 0%)
  • hrefreference to another <linearGradient> that will be used as a template
  • gradientUnitsdefines the coordinate system for attributes cx, cy, r, fx, fy, fr: specify userSpaceOnUse (use the viewport coordinate) or objectBoundingBox (use the bounding box of the element that references this gradient)
  • spreadMethoddefines how the shape is filled beyond the defined edges of the gradient; specify pad (just use the edge color, default), reflect, or repeat
  • gradientTransformspace-separated CSS transform functions, e.g. skewX(20) translate(-35, 0)
| LIVE PREVIEW

Stop SVG element

<stop>

Represents a color stop in a gradient vector.

  • offsetwhere the stop is; specify a number like 0.5 or 50%
  • stop-colorcolor of the stop, e.g. #ff0000
  • stop-opacityopacity of the stop, e.g. 1

References