SVG Linear Gradients

SVG supports rich color gradients natively. A linear gradient smoothly transitions between two or more colors along a straight line.

How Linear Gradients Work

Creating a gradient in SVG is a two-step process:

  1. Define the gradient inside defs and give it an id.
  2. Reference it on a shape's fill (or stroke) using url(#yourId).

The linearGradient Element

The gradient direction is controlled by four attributes that define a vector from start to end as percentages of the SVG's width and height:

To create a top-to-bottom gradient, use x1="0%" y1="0%" x2="0%" y2="100%".

To create a diagonal gradient, use x1="0%" y1="0%" x2="100%" y2="100%".

The stop Element

Inside the linearGradient, you define the colors at key points along the gradient vector using stop elements. Each has two key attributes:

You can have as many stop elements as you like to create multi-color or complex gradients.

Example

View Output