SVG Gradients - Linear
SVG Gradients
A gradient is a smooth transition from one color to another. Additionally, multiple color transitions can be applied to the same element.
There are two main types of SVG gradients:
- Linear
- Radial
SVG Linear Gradient - <linearGradient>
The <linearGradient>
element is used to define a linear gradient.
The <linearGradient>
tag must be nested inside a <defs>
tag. The <defs>
tag stands for definitions and can define special elements like gradients.
A linear gradient can be defined as horizontal, vertical, or angular:
- A horizontal gradient is created when y1 and y2 are equal, but x1 and x2 are different.
- A vertical gradient is created when x1 and x2 are equal, but y1 and y2 are different.
- An angular gradient is created when x1 and x2 are different, and y1 and y2 are different.
Example 1
Define a horizontal linear gradient from yellow to red for an ellipse:
Here 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)" />
</svg>
For Opera users: View SVG file (right-click on the SVG graphic to preview the source).
Code Analysis:
- The
id
attribute of the<linearGradient>
tag defines a unique name for the gradient. - The
x1
,x2
,y1
, andy2
attributes of the<linearGradient>
tag define the start and end positions of the gradient. - The color range of the gradient can consist of two or more colors. Each color is specified by a
<stop>
tag. Theoffset
attribute defines the start and end positions of the gradient. - The
fill
attribute links the ellipse element to the gradient.
Example 2
Define a vertical linear gradient from yellow to red for an ellipse:
Here 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)" />
</svg>
For Opera users: View SVG file (right-click on the SVG graphic to preview the source).
Example 3
Define an ellipse with a horizontal linear gradient from yellow to red and add text inside the ellipse:
Here 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>
</svg>
For Opera users: View SVG file (right-click on the SVG graphic to preview the source).
Code Analysis:
- The
<text>
element is used to add text.