Radial Gradients in SVG

I already went over how linear gradients are defined in SVG, so let's also go over some examples of radial gradients. The syntax is very similar to linear gradients.

Again, here's our base crossbones SVG image:

Base SVG image

And here's the SVG markup for it, simplified by changing the path data to :

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
  <style>.bones{fill:#ccc ;} .eye{fill:#666;}</style>

  <path class="bones" d="..."/>

  <path class="bones" d="..."/>

  <g>
    <path class="eye" d="..."/>
    <path class="eye" d="..."/>
  </g>
</svg>

Now here's a version with a dark green to lime radial gradient:

SVG radial gradient example 1

And here's the markup. Notice the highlighted sections and the defs, radialGradient and stop elements:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
  <style>.eye{fill:#F9EC31;}</style>

  <defs>
    <radialGradient id="bones-gradient">
	  <stop offset="0%" style="stop-color:#FF9133;" />
	  <stop offset="100%" style="stop-color:#FF0015;" />
    </radialGradient>
  </defs>

  <g fill="url(#bones-gradient)">
	  <path class="bones" d="..."/>
	  <path class="bones" d="..."/>
  </g>

  <g>
    <path class="eye" d="..."/>
    <path class="eye" d="..."/>
  </g>
</svg>

Now a version with multiple color stops and the use of stop-opacity:

SVG radial gradient example 2

And the markup for it:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
  <style>.eye{fill:#211533;}</style>

  <defs>
	<radialGradient id="bones-gradient">
	  <stop offset="0%" style="stop-color:#5AA8DF;stop-opacity:1" />
	  <stop offset="50%" style="stop-color:#FB7629;stop-opacity:0.7" />
	  <stop offset="100%" style="stop-color:#FAE9B0;stop-opacity:0.5" />
	</radialGradient>
  </defs>

  <g fill="url(#bones-gradient)">
	  <path class="bones" d="..."/>
	
	  <path class="bones" d="..."/>
  </g>

  <g>
    <path class="eye" d="..."/>
    <path class="eye" d="..."/>
  </g>
</svg>

And finally, to demonstrate how to change the position of the gradient let's do show it on simpler circle shapes.

SVG radial gradient example 3

Here's the markup:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
  <defs>
    <radialGradient id="gradient1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
	<stop offset="0%" style="stop-color:#FF00FF;" />
	<stop offset="100%" style="stop-color:#FFBD2E;" />
    </radialGradient>

    <radialGradient id="gradient2" cx="50%" cy="50%" r="70%" fx="50%" fy="50%">
	<stop offset="0%" style="stop-color:#FF00FF;" />
	<stop offset="100%" style="stop-color:#FFBD2E;" />
    </radialGradient>

    <radialGradient id="gradient3" cx="20%" cy="50%" r="50%" fx="50%" fy="50%">
	<stop offset="0%" style="stop-color:#FF00FF;" />
	<stop offset="100%" style="stop-color:#FFBD2E;" />
    </radialGradient>

    <radialGradient id="gradient4" cx="50%" cy="50%" r="50%" fx="20%" fy="90%">
	<stop offset="0%" style="stop-color:#FF00FF;" />
	<stop offset="100%" style="stop-color:#FFBD2E;" />
    </radialGradient>

    <radialGradient id="gradient5" cx="20%" cy="20%" r="50%" fx="20%" fy="20%">
	<stop offset="0%" style="stop-color:#FF00FF;" />
	<stop offset="100%" style="stop-color:#FFBD2E;" />
    </radialGradient>

    <radialGradient id="gradient6" cx="50%" cy="50%" r="50%" fx="80%" fy="80%">
	<stop offset="0%" style="stop-color:#FF00FF;" />
	<stop offset="100%" style="stop-color:#FFBD2E;" />
    </radialGradient>
  </defs>

  <!-- top left circle -->
  <circle cx="40" cy="60" r="30" fill="url(#gradient1)" />
  <!-- top center circle -->
  <circle cx="100" cy="60" r="30" fill="url(#gradient2)" />
  <!-- top right circle -->
  <circle cx="160" cy="60" r="30" fill="url(#gradient3)" />

  <!-- bottom left circle -->
  <circle cx="40" cy="140" r="30" fill="url(#gradient4)" />
  <!-- bottom center circle -->
  <circle cx="100" cy="140" r="30" fill="url(#gradient5)" />
  <!-- bottom right circle -->
  <circle cx="160" cy="140" r="30" fill="url(#gradient6)" />
</svg>

Here are quick pointers about the new properties introduced:

  • cx & cy: The position of the center of the gradient. This affects the whole gradient.
  • r: The radius of the gradient circle.
  • fx & fy: Position of the focal point of the gradient. It affects the 1st color stop position.
✖ Clear

🕵 Search Results

🔎 Searching...