SVG Linear Gradients

What You’ll Learn
How to create linear gradients in SVG to blend two or more colours along a straight line. Gradients add depth, polish, and visual hierarchy in icons, buttons, charts, and illustrations.
You’ll learn the syntax for <linearGradient> inside <defs>, how <stop> offsets work, how to control direction using x1, y1, x2, y2, and how to apply gradients to both fill and stroke.
⚡ Quick Reference — SVG Linear Gradient
<linearGradient>Inside <defs> with an id
<stop>Use offset + stop-color
url(#id)Use in fill or stroke
x1/y1 → x2/y2Controls the gradient vector
gradientUnitsobjectBoundingBox (default) or userSpaceOnUse
<svg>
<defs>
<linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#f97316" />
<stop offset="100%" stop-color="#3b82f6" />
</linearGradient>
</defs>
<rect fill="url(#g)" />
</svg>👀 Live Preview — Gradient Directions
Linear gradients are defined by a direction vector. Here are a few common directions you can reuse.
Fill a Shape with a Linear Gradient
A classic use: fill a rectangle with a left-to-right gradient using two colour stops.
<svg width="300" height="200" viewBox="0 0 300 200">
<defs>
<linearGradient id="gradientID" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="red" />
<stop offset="100%" stop-color="blue" />
</linearGradient>
</defs>
<rect x="20" y="20" width="260" height="160" rx="18" fill="url(#gradientID)" />
</svg>Gradient Stroke (Outline)
You can apply the same linear gradient to the stroke of a shape. This is great for badges and ring-style UI.
<svg width="240" height="160" viewBox="0 0 240 160">
<defs>
<linearGradient id="strokeGrad" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#22c55e" />
<stop offset="50%" stop-color="#0ea5e9" />
<stop offset="100%" stop-color="#a855f7" />
</linearGradient>
</defs>
<circle cx="120" cy="80" r="48" fill="none" stroke="url(#strokeGrad)" stroke-width="10" />
</svg>🧠 How It Works
Define the gradient in <defs>
Gradients should be defined inside <defs> so they can be reused across multiple shapes.
Add colour stops
Each <stop> specifies a colour at a position using offset. Two stops are enough for most gradients.
Control direction with x1/y1/x2/y2
Think of the values as an arrow. Left-to-right is 0% 0% → 100% 0%. Top-to-bottom is 0% 0% → 0% 100%.
Apply via url(#id)
Use fill="url(#id)" to fill shapes or stroke="url(#id)" to paint outlines.
Smooth colour transitions
Linear gradients make SVG graphics feel more modern and dimensional while staying crisp at any scale.
💡 Best Practices
Do
- Keep gradients subtle for UI (2–3 stops is usually enough)
- Use consistent directions across icons in the same set
- Use
gradientUnits="userSpaceOnUse"when you need fixed coordinates - Use gradients on
strokefor elegant outlines and rings - Name ids clearly (e.g.
btnGrad,strokeGrad)
Don’t
- Reuse the same gradient id across separate inline SVGs on one page
- Overuse many stops unless you really need complex shading
- Forget to define the gradient before referencing it in
fill/stroke - Mix random colour palettes across related graphics
- Assume gradients look identical on every display without checking
❓ Frequently Asked Questions
<linearGradient> inside <defs> with an id, add <stop> elements, then apply it using fill="url(#id)" (or stroke="url(#id)").x1="0%" y1="0%" x2="100%" y2="0%". Top-to-bottom is x1="0%" y1="0%" x2="0%" y2="100%".offset is the position of that stop along the gradient vector. Common values are 0%, 50%, and 100%.stroke="url(#gradientId)" and set a stroke-width. This is common for rings, outlines, and badges.gradientUnits="userSpaceOnUse" and use absolute values.Make SVG Clickable
Next, learn how to turn SVG elements into links and build interactive SVG navigation.
4 people found this page helpful
