Tutorial

Linear Gradients in SVG

Published on June 16, 2016
    author

    Alligator.io

    Linear Gradients in SVG

    Even though realistically most of the time you’ll be creating SVG files using tools like Adobe Illustrator instead of coding them by hand, some SVG features are easy to implement by hand to give your images the extra pop. Linear gradients is one such feature.

    Let’s learn with an example. Here’s our base crossbones image:

    Base SVG image

    And here’s the SVG markup for it. I’ve simplified by changing the path data with …:

    <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 an orange gradient:

    SVG linear gradient example 1

    And here’s the markup. Notice the highlighted sections and the defs, linearGradient and stop elements:

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
      <style>.eye{fill:#F9EC31;}</style>
    
      <defs>
        <linearGradient id="bones-gradient" x1="0%" y1="0%" x2="100%" y2="0%">
    	  <stop offset="0%" style="stop-color:#FF9133;" />
    	  <stop offset="100%" style="stop-color:#FF0015;" />
        </linearGradient>
      </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. Also notice the use of fill-opacity for the eyes:

    SVG linear 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; fill-opacity: 0.5;}</style>
    
      <defs>
    	<linearGradient id="bones-gradient" x1="0%" y1="0%" x2="100%" y2="0%">
    	  <stop offset="0%" style="stop-color:#f8f8f8;stop-opacity:0.5" />
    	  <stop offset="50%" style="stop-color:#fc00ff;stop-opacity:1" />
    	  <stop offset="100%" style="stop-color:#f8f8f8;stop-opacity:0.5" />
    	</linearGradient>
      </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 in this last example we use a different angle for the gradient:

    SVG linear gradient example 3

    Here’s the markup:

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
      <style>.eye{fill:#F9EC31;}</style>
    
      <defs>
    	<linearGradient id="bones-gradient" x1="0%" y1="0%" x2="50%" y2="100%">
    	  <stop offset="0%" style="stop-color:blue;" />
    	  <stop offset="100%" style="stop-color:#FF0015;" />
    	</linearGradient>
      </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>
    
    

    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

    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.

    Still looking for an answer?

    Ask a questionSearch for more help

    Was this helpful?
     
    1 Comments
    

    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!

    I know this is old but Thank you!, btw you can actually remove the style in the stop tag and use it like this:

    before:

    <stop offset="0%" style="stop-color:blue;" />
    <stop offset="100%" style="stop-color:#FF0015;" />
    
    

    after:

    <stop offset="0%" stop-color="blue" />
    <stop offset="100%" stop-color="FF0015" />
    
    

    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!

    Become a contributor for community

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

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.