SVG Gradient - Linear
SVG Gradient
A gradient is a smooth transition from one color to another. In addition, transitions of multiple colors can be applied to the same element.
There are mainly two types of SVG gradients:
- Linear gradient -
- Radial gradient -
SVG linear gradient -
In SVG, the The element is used to create a linear gradient; it transitions from one color to another along a straight line, creating a smooth gradient effect.
Linear gradients can be used to fill or stroke SVG graphic elements, giving them rich color variations.
Basic Syntax
Parameter Description:
-
idThe attribute defines the unique identifier of a linear gradient, used to reference the gradient in an SVG image. -
x1andy1The attribute defines the starting point coordinates of the gradient. -
x2andy2The attribute defines the ending point coordinates of the gradient. -
The element is used to specify the colors in the gradient and their positions.
Linear gradients can be defined as horizontal, vertical, or angular gradients:
- When y1 and y2 are equal, and x1 and x2 differ, a horizontal gradient can be created
- When x1 and x2 are equal, and y1 and y2 differ, a vertical gradient can be created
- When x1 and x2 differ, and y1 and y2 differ, an angular gradient can be created
The following code defines a horizontal linear gradient from red to blue, then applies it to a filled rectangle, giving the rectangle a gradient fill effect.
Example
Example 1
Define a horizontal linear gradient from yellow to red ellipse:
Below is the SVG code:
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
defs
<
linearGradient
id
=
"
grad1
"
x1
=
"
0%
"
y1
=
"
0%
"
x2
=
"
100%
"
y2
=
"
0%
"
<
stop
offset
=
"
0%
"
style
=
"
stop-color:rgb(255,255,0);stop-opacity:1
"
/>
<
stop
offset
=
"
100%
"
style
=
"
stop-color:rgb(255,0,0);stop-opacity:1
"
/>
linearGradient
defs
<
ellipse
cx
=
"
200
"
cy
=
"
70
"
rx
=
"
85
"
ry
=
"
55
"
fill
=
"
url(#grad1)
"
/>
svgTry it »
Click to view:View SVG File.
Code analysis:
The id attribute of the tag can define a unique name for the gradient The X1, X2, Y1, Y2 attributes of the tag define the gradient start and end positions - The color range of a gradient can consist of two or more colors. Each color is specified by a
tag. The offset attribute is used to define the start and end positions of the gradient. - The fill attribute links the ellipse element to this gradient
Example 2
Define a vertical linear gradient from yellow to red ellipse:
Below is the SVG code:
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
defs
<
linearGradient
id
=
"
grad1
"
x1
=
"
0%
"
y1
=
"
0%
"
x2
=
"
0%
"
y2
=
"
100%
"
<
stop
offset
=
"
0%
"
style
=
"
stop-color:rgb(255,255,0);stop-opacity:1
"
/>
<
stop
offset
=
"
100%
"
style
=
"
stop-color:rgb(255,0,0);stop-opacity:1
"
/>
linearGradient
defs
<
ellipse
cx
=
"
200
"
cy
=
"
70
"
rx
=
"
85
"
ry
=
"
55
"
fill
=
"
url(#grad1)
"
/>
svgTry it »
Click to view:View SVG File.
Example 3
Define an ellipse, with a horizontal linear gradient from yellow to red and add text inside the ellipse:
Below is the SVG code:
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
defs
<
linearGradient
id
=
"
grad1
"
x1
=
"
0%
"
y1
=
"
0%
"
x2
=
"
100%
"
y2
=
"
0%
"
<
stop
offset
=
"
0%
"
style
=
"
stop-color:rgb(255,255,0);stop-opacity:1
"
/>
<
stop
offset
=
"
100%
"
style
=
"
stop-color:rgb(255,0,0);stop-opacity:1
"
/>
linearGradient
defs
<
ellipse
cx
=
"
200
"
cy
=
"
70
"
rx
=
"
85
"
ry
=
"
55
"
fill
=
"
url(#grad1)
"
/>
<
text
fill
=
"
#ffffff
"
font-size
=
"
45
"
font-family
=
"
Verdana
"
x
=
"
150
"
y
=
"
86
"
SVG
text
svgTry it »
Click to view:View SVG File.
Code analysis:
The element is used to add a text
Notes
-
A gradient can contain multiple
elements, eachelement represents a color and position in the gradient. -
The start and end points of a gradient can be located anywhere; the gradient interpolates along the line between the defined start and end points.
-
Gradient coordinate values can use percentages or specific length values; percentage values are relative to the size of the graphic element.
-
Linear gradients can be applied to fill or stroke SVG graphic elements.