Introduction to SVG Gradients:

 
  • SVG gradients are a powerful feature that allows you to create smooth transitions of colors or shades within an SVG graphic. There are two main types of gradients in SVG:
  • Linear Gradient: A linear gradient creates a smooth transition of colors along a straight line. It is defined by two points: the starting point and the ending point, and the colors transition from one point to another.
  • Radial Gradient: A radial gradient creates a smooth transition of colors radiating outward from a center point. It is defined by a center point, a radius, and the colors transition from the center point outward.


Creating Linear Gradients with SVG:
 

To create a linear gradient with SVG, you can use the <linearGradient> element within your SVG document. Here's an example:



 
<svg width="400" height="200">
  <linearGradient id="linearGradient1" x1="0%" y1="0%" x2="100%" y2="0%">
    <stop offset="0%" stop-color="red" />
    <stop offset="100%" stop-color="blue" />
  </linearGradient>

  <rect x="50" y="20" width="300" height="150" fill="url(#linearGradient1)" />
</svg>




In this example:

 
  • <linearGradient> defines the linear gradient with an ID of "linearGradient1".
  • x1, y1, x2, y2 specify the starting and ending points of the gradient. Here, it starts at (0%, 0%) and ends at (100%, 0%), creating a horizontal gradient.
  • <stop> elements define the color stops of the gradient. The first <stop> has an offset of 0% and a color of red, while the second <stop> has an offset of 100% and a color of blue.
  • The <rect> element is filled with the linear gradient using fill="url(#linearGradient1)".



Creating Radial Gradients with SVG:

 

To create a radial gradient with SVG, you can use the <radialGradient> element within your SVG document. Here's an example:



 

<svg width="400" height="200">
  <radialGradient id="radialGradient1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
    <stop offset="0%" stop-color="yellow" />
    <stop offset="100%" stop-color="green" />
  </radialGradient>

  <circle cx="200" cy="100" r="80" fill="url(#radialGradient1)" />
</svg>
 



In this example:
 
  • <radialGradient> defines the radial gradient with an ID of "radialGradient1".
  • cx, cy specify the center point of the gradient, r specifies the radius, and fx, fy (optional) specify the focal point for the gradient.
  • <stop> elements define the color stops of the gradient, transitioning from yellow to green.
  • The <circle> element is filled with the radial gradient using fill="url(#radialGradient1)".



Using Gradients for Text:
 

You can also apply gradients to text elements in SVG. Here's an example of applying a linear gradient to text:

 
<svg width="400" height="200">
  <linearGradient id="textGradient" x1="0%" y1="0%" x2="100%" y2="0%">
    <stop offset="0%" stop-color="purple" />
    <stop offset="100%" stop-color="pink" />
  </linearGradient>

  <text x="50" y="100" font-size="40" fill="url(#textGradient)">SVG Text with Gradient</text>
</svg>

In this example, the <text> element is filled with the linear gradient using fill="url(#textGradient)".



Advantages of Using SVG Gradients:
 
  • Flexible Color Effects: Gradients allow for smooth color transitions and effects that enhance the visual appeal of SVG graphics.
  • Scalability: Gradients scale with the SVG graphics, maintaining their quality and appearance at different sizes.
  • Reuse and Efficiency: Gradients can be defined once and reused across multiple shapes or text elements, promoting code efficiency.
  • Interactive Gradients: SVG gradients can be animated or modified dynamically using JavaScript for interactive effects.

 



Practice Excercise Practice now