Chapter 22: SVG Linear Gradients
SVG: Linear Gradients!
Imagine we’re sitting together with the code editor open, and I’m your patient teacher who’s going to explain everything about <linearGradient> step by step — slowly, clearly, with lots of copy-paste examples you can try right now.
What is a Linear Gradient in SVG?
A linear gradient is a smooth color transition that flows in a straight line across a shape.
- Instead of one solid color (fill=”blue”), the fill changes gradually from one color to another (or many colors).
- The transition happens along an invisible gradient line (called the gradient vector).
- You define this once in <defs>, give it an id, and then many shapes can reuse it with fill=”url(#myGradient)”.
This is perfect for:
- Buttons with shine/reflection
- Backgrounds (sky, sunset, metal, glass)
- Charts & data visualization
- Modern UI elements
- Logos & icons with depth
Where do we put it? → Inside <defs>
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
<svg width="500" height="300"> <defs> <!-- All gradients live here --> </defs> <!-- Then use them on shapes --> </svg> |
The <linearGradient> element – Core attributes
| Attribute | What it controls | Common values | Default | Very important? |
|---|---|---|---|---|
| id | Unique name so shapes can reference it | “sunset”, “blueSky”, “metal” | — | ★★★★★ |
| x1, y1 | Starting point of gradient line (0–1 or %) | 0%, 50%, 0 | 0% | ★★★★★ |
| x2, y2 | Ending point of gradient line | 100%, 0%, 45% | 100% | ★★★★★ |
| gradientUnits | What units x1/y1/x2/y2 use | “objectBoundingBox” or “userSpaceOnUse” | objectBoundingBox | ★★★★☆ |
| gradientTransform | Rotate/scale/skew the gradient (advanced) | “rotate(45)” | none | ★★☆☆☆ |
| spreadMethod | What happens outside gradient line | pad / reflect / repeat | pad | ★★★☆☆ |
Most common & beginner-friendly setting (recommended for 90% of cases):
|
0 1 2 3 4 5 6 7 |
gradientUnits="objectBoundingBox" ← percentages relative to the shape's bounding box x1="0%" y1="0%" x2="100%" y2="0%" ← horizontal left → right |
The real magic: <stop> elements (color points)
Inside <linearGradient>, you put one or more <stop> tags:
- Each <stop> says: “At this position along the line, use this color”
- offset = position (0% to 100%)
- stop-color = color at that point
- stop-opacity = transparency at that point (optional)
Example 1 – Simplest horizontal gradient (left to right)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<svg width="500" height="180" style="border:1px solid #eee; background:#f9f9f9;"> <defs> <linearGradient id="simpleHoriz"> <stop offset="0%" stop-color="#FFEB3B"/> <!-- yellow --> <stop offset="100%" stop-color="#F44336"/> <!-- red --> </linearGradient> </defs> <rect x="40" y="40" width="420" height="100" rx="12" fill="url(#simpleHoriz)"/> <text x="250" y="100" font-size="42" text-anchor="middle" fill="white"> Sunrise Glow </text> </svg> |
Result: Rectangle smoothly changes from yellow (left) to red (right).
Here are some visual examples of basic horizontal gradients (imagine seeing these side by side):
- Yellow → Orange → Red
- Blue → Purple → Pink
- Black → Gray → White (chrome look)
Example 2 – Vertical gradient (top to bottom)
Just change the coordinates:
|
0 1 2 3 4 5 6 7 8 9 |
<linearGradient id="vertical"> <stop offset="0%" stop-color="#2196F3"/> <!-- top blue --> <stop offset="100%" stop-color="#0D47A1"/> <!-- bottom dark blue --> </linearGradient> |
|
0 1 2 3 4 5 6 |
<rect ... fill="url(#vertical)" /> |
Example 3 – Diagonal gradient (very popular for buttons)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<defs> <linearGradient id="diagonal"> <stop offset="0%" stop-color="#673AB7"/> <!-- purple --> <stop offset="50%" stop-color="#9C27B0"/> <!-- mid magenta --> <stop offset="100%" stop-color="#E91E63"/> <!-- pink --> </linearGradient> </defs> <rect x="50" y="50" width="300" height="120" rx="20" fill="url(#diagonal)"/> |
→ Gradient flows from top-left to bottom-right.
Example 4 – Multiple stops + opacity (glass / shine effect)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<defs> <linearGradient id="glass"> <stop offset="0%" stop-color="white" stop-opacity="0.9"/> <stop offset="40%" stop-color="white" stop-opacity="0.3"/> <stop offset="50%" stop-color="white" stop-opacity="0.1"/> <stop offset="100%" stop-color="white" stop-opacity="0"/> </linearGradient> </defs> <rect x="60" y="60" width="280" height="100" rx="16" fill="#2196F3"/> <rect x="60" y="60" width="280" height="100" rx="16" fill="url(#glass)"/> |
→ Creates a nice highlight/shine on top of a blue button.
Quick Cheat Sheet – Common gradient directions
| Direction | x1 y1 x2 y2 | Typical use case | |————————|——|——|——|——|———————————–| | Horizontal left→right | 0% 0% 100% 0% | Most buttons, bars | | Vertical top→bottom | 0% 0% 0% 100% | Sky, backgrounds | | Diagonal top-left→bottom-right | 0% 0% 100% 100% | Modern cards, shiny effects | | Diagonal bottom-left→top-right | 0% 100% 100% 0% | Alternative diagonal | | Right→left | 100% 0% 0% 0% | Reverse direction |
Common beginner mistakes
- Forget id → gradient invisible (fill=”url(#wrongId)” does nothing)
- No <stop> tags → gradient black or transparent
- Wrong percentages → gradient jumps or stays one color
- Use gradientUnits=”userSpaceOnUse” without thinking → gradient size fixed, not scaling with shape
- Too few stops → hard transitions (add more for smooth rainbow)
Mini practice for you
- Make a sunset rectangle: orange → purple → dark blue (vertical)
- Create a shiny button: base color + white-to-transparent diagonal gradient overlay
- Try a rainbow gradient (at least 6–7 stops) horizontal across the whole SVG
Paste your code here if you want feedback — I’ll review it like we’re pair-programming 😄
This is the foundation of linear gradients — super useful and looks professional instantly! Next we can cover radial gradients (circular), repeating gradients, angle control, or combining gradients with filters.
Which part feels most exciting or confusing? Diagonal directions? Multiple stops? Shine effects? Just tell me — we’ll zoom in with more examples until it clicks perfectly! 🚀
