Latest web development tutorials

SVG Gradient - Linear

SVG Gradient

The gradient is a smooth transition from one to another color to color. In addition, the transition can be applied to a plurality of colors on the same element.

There are two main types of SVG Gradient:

  • Linear
  • Radial

SVG linear gradients - <linearGradient>

<LinearGradient> element is used to define a linear gradient.

<LinearGradient> tag must be nested inside <defs> interior. <Defs> tag is the acronym definitions, such as special elements that can be defined like gradient.

Linear gradient can be defined as horizontal, vertical or angle gradient:

  • When y1 and y2 are equal, but not both x1 and x2, create a horizontal gradient
  • When x1 and x2 equal, y1 and y2 are not the same, you can create a vertical gradient
  • When x1 and x2 are different and y1 and y2 are not the same, you can create a gradient angle

Example 1

It defines the horizontal linear gradient from yellow to red oval:

Here is the SVG Code:

Examples

<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>

try it"

For Opera users: view SVG file (right-click on the preview source SVG graphics).

Code analysis:

  • id attribute <linearGradient> tag can be a unique name for the gradient definition
  • <LinearGradient> tag X1, X2, Y1, Y2 attribute defines the beginning and end of the gradient
  • Gradient color range by two or more colors. Each color by a <stop> tag to specify. offset is used to define the start and end position of the gradient.
  • Fill property element to link to the gradient of the ellipse

Example 2

Define a vertical linear gradient from yellow to red oval:

Here is the SVG Code:

Examples

<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>

try it"

For Opera users: view SVG file (right-click on the preview source SVG graphics).


Example 3

The definition of an ellipse, the horizontal linear gradient from yellow to red within the text and add an ellipse:

Here is the SVG Code:

Examples

<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>

try it"

For Opera users: view SVG file (right-click on the preview source SVG graphics).

Code analysis:

  • <Text> element is used to add a text