Alligator.io
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:
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:
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:
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.
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:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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.
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!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.