SVG linearGradient
Learn the SVG linearGradient element and its stop children: offset, stop-color, x1/x2/y1/y2, gradientUnits and spreadMethod, with runnable examples.
A linear gradient is a fluid transition from one color to another along a straight line. In SVG you build one with two elements working together: a <linearGradient> that defines the direction and coordinate system of the line, and one or more <stop> children that place colors along it. You then point a shape's fill (or stroke) at the gradient by its id, using fill="url(#myGradient)".
This page covers the <linearGradient> element, its <stop> children, the x1/x2/y1/y2 coordinates that aim the gradient, and the gradientUnits and spreadMethod attributes that control how it is mapped and tiled.
Description of SVG gradients
A gradient is a smooth color blend that can be reused as a paint for any number of shapes. SVG has two main kinds of gradients:
- linear — colors blend along a straight line (this page).
- radial — colors blend outward from a center point. See SVG radialGradient.
The <linearGradient> element
The <linearGradient> element defines a linear gradient that fills (or strokes) graphical elements. It is a definition, not a visible shape, so it lives inside a <defs> block, is given an id, and is referenced from a shape with fill="url(#id)".
The direction of the gradient is set by the start point (x1, y1) and the end point (x2, y2). The gradient line runs from the start point to the end point; colors are interpolated along it.
Linear gradients can be horizontal, vertical, or angular:
- Horizontal —
y1andy2are equal, whilex1andx2differ. - Vertical —
x1andx2are equal, whiley1andy2differ. - Angular (diagonal) — both the x pair and the y pair differ.
If you omit the coordinates, the SVG defaults are x1="0%", y1="0%", x2="100%", y2="0%" — a left-to-right horizontal gradient.
Don't confuse an SVG linear gradient with the CSS linear-gradient function. CSS gradients paint HTML elements via background-image, while SVG gradients paint SVG shapes via fill or stroke.
The <stop> element
Each <stop> places a single color at a position along the gradient line. You need at least two stops to see a transition. Stops can be styled with presentation attributes (stop-color, stop-opacity) or with the equivalent CSS inside a style attribute, as in the examples below.
| Attribute | Description |
|---|---|
offset | Where the stop sits along the gradient line, as a number 0–1 or a percentage 0%–100%. 0% is the start point, 100% is the end point. |
stop-color | The color of the stop. Accepts any CSS color (named, #hex, rgb(), hsl()). Defaults to black. |
stop-opacity | The opacity of the stop's color, from 0 (transparent) to 1 (opaque). Defaults to 1. |
Example of the <linearGradient> element to create an ellipse with a horizontal linear gradient:
<!DOCTYPE html>
<html>
<body>
<svg height="200" width="300">
<defs>
<linearGradient id="ellipse" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(28, 135, 201);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(128, 0, 128);stop-opacity:1" />
</linearGradient>
</defs>
<ellipse cx="150" cy="90" rx="105" ry="65" fill="url(#ellipse)" />
</svg>
</body>
</html>Example of the <linearGradient> element to create an ellipse with a vertical linear gradient:
<!DOCTYPE html>
<html>
<body>
<svg height="300" width="400">
<defs>
<linearGradient id="ellipse" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:rgb(28, 135, 201);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(128, 0, 128);stop-opacity:1" />
</linearGradient>
</defs>
<ellipse cx="250" cy="100" rx="110" ry="70" fill="url(#ellipse)" />
</svg>
</body>
</html>Example of an angular (diagonal) linear gradient:
By default gradientUnits is objectBoundingBox, so the coordinates are fractions of the shape's bounding box. Using x1="0" y1="0" x2="1" y2="1" runs the gradient line from the top-left corner to the bottom-right corner, producing a diagonal blend.
<!DOCTYPE html>
<html>
<body>
<svg height="200" width="300">
<defs>
<linearGradient id="diagonal" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" style="stop-color:rgb(28, 135, 201);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(128, 0, 128);stop-opacity:1" />
</linearGradient>
</defs>
<rect x="20" y="20" width="260" height="160" fill="url(#diagonal)" />
</svg>
</body>
</html>The gradientUnits attribute
gradientUnits decides how x1, y1, x2, and y2 are interpreted:
objectBoundingBox(default) — coordinates are relative to the shape being painted, where0(or0%) is one edge and1(or100%) is the opposite edge. The gradient automatically stretches to fit each shape.userSpaceOnUse— coordinates are absolute positions in the SVG's user coordinate system (the same units as the shape's ownx/y).
A common gotcha: with userSpaceOnUse, percentage and 0–1 coordinates no longer track the shape. You must give real coordinates (e.g. x1="20" y1="0" x2="280" y2="0") that overlap the shape, or the fill may appear flat (a single color) because the gradient line never crosses it.
The spreadMethod attribute
spreadMethod controls what happens outside the [x1,y1]–[x2,y2] segment when the gradient line is shorter than the shape. It only has a visible effect when the gradient doesn't already span the whole shape.
| Value | Behavior |
|---|---|
pad (default) | The first and last stop colors extend ("pad out") to fill the remaining area. |
reflect | The gradient mirrors back and forth, repeating in alternating directions. |
repeat | The gradient tiles, jumping from the last stop back to the first and repeating. |
<!DOCTYPE html>
<html>
<body>
<svg height="120" width="300">
<defs>
<linearGradient id="repeated" x1="0" y1="0" x2="0.2" y2="0"
spreadMethod="repeat">
<stop offset="0%" stop-color="rgb(28, 135, 201)" />
<stop offset="100%" stop-color="rgb(128, 0, 128)" />
</linearGradient>
</defs>
<rect x="0" y="0" width="300" height="120" fill="url(#repeated)" />
</svg>
</body>
</html>Change spreadMethod="repeat" to reflect or pad in the example above to compare the three behaviors: repeat produces hard color jumps, reflect produces a smooth ping-pong, and pad shows the gradient once and then a solid block of the last color.
Attributes
| Value | Description |
|---|---|
| gradientUnits | This attribute specifies the coordinate system for attributes x1, x2, y1, y2. Values: userSpaceOnUse, objectBoundingBox. |
| gradientTransform | This attribute gives extra transformation to the gradient coordinate system. |
| href | This attribute specifies a reference to another <linearGradient> element. |
| spreadMethod | This attribute specifies how the gradient behaves if it starts or ends within the bounds of the shape that contains the gradient. Values: pad, repeat, reflect. |
| x1 | This attribute specifies the x coordinate of the beginning point of the vector gradient along which the linear gradient is being drawn. |
| x2 | This attribute specifies the x coordinate of the ending point of the vector gradient along which the linear gradient is being drawn. |
| y1 | This attribute specifies the y coordinate of the beginning point of the vector gradient along which the linear gradient is drawn. |
| y2 | This attribute specifies the y coordinate of the ending point of the vector gradient along which the linear gradient is drawn. |
The SVG <linearGradient> element also supports the Global Attributes and Event Attributes.
For radial color blends that spread outward from a center point instead of along a line, see SVG radialGradient.