Easy Tutorial
❮ Svg Filters Intro Svg Ellipse ❯

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:


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:


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:


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:

❮ Svg Filters Intro Svg Ellipse ❯