Tutorial

Radial Gradients in SVG

Published on July 1, 2016
    author

    Alligator.io

    Radial Gradients in SVG

    While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

    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.

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about our products


    About the authors
    Default avatar
    Alligator.io

    author

    Still looking for an answer?

    Ask a questionSearch for more help

    Was this helpful?
     
    Leave a comment
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Featured on Community

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel