Chapter 23: SVG Radial Gradients
SVG Radial Gradients
Imagine we’re sitting together again, code editor open, and I’m going to walk you through this very slowly and clearly, just like before — with plenty of copy-paste examples you can try immediately.
What is a Radial Gradient in SVG?
A radial gradient creates a smooth color transition that spreads outward (or inward) from a central point in a circular or elliptical pattern — like ripples in a pond, a spotlight, a glowing orb, or the shine on a metallic button.
- Instead of changing color along a straight line (like linear gradients), it changes color radially — from the inside point outward.
- The center is usually one color, and it smoothly blends to other colors as it moves toward the edges.
- Very popular for: realistic buttons, orbs, glow effects, spotlights, metallic/reflective surfaces, radial backgrounds, 3D-looking spheres, badges, loaders, etc.
Where do we define it? → Inside <defs>
Just like linear gradients:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
<svg width="400" height="300"> <defs> <!-- radialGradient goes here --> </defs> <!-- then use it: fill="url(#myRadial)" --> </svg> |
The <radialGradient> element – Core attributes
| Attribute | What it controls | Common values | Default | Very important? |
|---|---|---|---|---|
| id | Unique name to reference it | “glow”, “orb”, “shine” | — | ★★★★★ |
| cx | Horizontal center of the gradient circle (%) | 50%, 30%, 0.5 | 50% | ★★★★★ |
| cy | Vertical center of the gradient circle (%) | 50%, 70% | 50% | ★★★★★ |
| r | Radius of the gradient circle (how far it spreads) | 50%, 70%, 0.8 | 50% | ★★★★★ |
| fx, fy | Focal point (where the “brightest” spot starts) | 50%, 40% | same as cx/cy | ★★★★☆ |
| gradientUnits | Units for cx/cy/r/fx/fy | “objectBoundingBox” or “userSpaceOnUse” | objectBoundingBox | ★★★★☆ |
| spreadMethod | What happens beyond radius | pad / reflect / repeat | pad | ★★★☆☆ |
Most beginner-friendly & common setting (use this 90% of the time):
|
0 1 2 3 4 5 6 7 |
gradientUnits="objectBoundingBox" cx="50%" cy="50%" r="50%" |
→ Gradient perfectly centered in the shape’s bounding box, spreading to the edges.
The <stop> elements — same as linear gradients
You control the colors the same way:
|
0 1 2 3 4 5 6 7 8 |
<stop offset="0%" stop-color="white" stop-opacity="1"/> <stop offset="70%" stop-color="#2196F3" stop-opacity="0.8"/> <stop offset="100%" stop-color="#0D47A1" stop-opacity="0"/> |
- offset=”0%” → color at the exact center (or focal point)
- offset=”100%” → color at the outer edge (radius)
Example 1 – Simple centered radial gradient (classic orb/glow)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<svg width="300" height="300" style="border:1px solid #eee; background:#f9f9f9;"> <defs> <radialGradient id="simpleOrb"> <stop offset="0%" stop-color="white"/> <stop offset="100%" stop-color="#673AB7"/> <!-- purple --> </radialGradient> </defs> <circle cx="150" cy="150" r="120" fill="url(#simpleOrb)"/> <text x="150" y="165" font-size="42" text-anchor="middle" fill="white"> Glowing Orb </text> </svg> |
Result: White center fading smoothly to purple at the edges — looks like a soft glowing ball.
Here are some visual examples of simple radial gradients (imagine seeing these side by side):
- White → light blue → deep blue
- Yellow → orange → red (sun-like)
- Silver → dark gray (metallic sphere)
Example 2 – Focal point offset (realistic shine/highlight)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<svg width="320" height="320"> <defs> <radialGradient id="shinyButton" cx="35%" cy="35%" r="60%" fx="30%" fy="30%"> <stop offset="0%" stop-color="white" stop-opacity="0.9"/> <stop offset="40%" stop-color="white" stop-opacity="0.4"/> <stop offset="70%" stop-color="#2196F3" stop-opacity="0.8"/> <stop offset="100%" stop-color="#0D47A1" stop-opacity="1"/> </radialGradient> </defs> <circle cx="160" cy="160" r="140" fill="url(#shinyButton)"/> <text x="160" y="175" font-size="48" text-anchor="middle" fill="white"> Shiny! </text> </svg> |
→ fx/fy moved to top-left → creates a bright highlight spot (very common for buttons/icons).
Example 3 – Elliptical gradient (non-circular)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<svg width="400" height="240"> <defs> <radialGradient id="ellipseGrad" cx="50%" cy="50%" r="70%" fx="50%" fy="50%"> <stop offset="0%" stop-color="#FFEB3B"/> <stop offset="100%" stop-color="#F44336"/> </radialGradient> </defs> <ellipse cx="200" cy="120" rx="180" ry="100" fill="url(#ellipseGrad)"/> </svg> |
→ Because the shape is elliptical, the gradient stretches to match → creates oval glow.
Quick Cheat Sheet – Popular radial gradient styles
| Style / Look | cx/cy/fx/fy settings | r value | Typical stops sequence |
|---|---|---|---|
| Centered orb/glow | 50% 50% 50% 50% | 50–70% | white → color → darker color |
| Button shine/highlight | 30–40% 30–40% 30–40% 30–40% | 60–80% | white (high opacity) → base color → dark |
| Spotlight effect | 50% 50% 50% 50% | 40–60% | bright → fade to transparent |
| Metallic/chrome | 20% 20% 30% 30% | 80–100% | white → light gray → dark gray → black |
| Sunset/radial background | 50% 30% 50% 50% | 100% | orange → purple → navy |
Common beginner mistakes
- Forget id → fill=”url(#wrongId)” shows nothing
- r=”0.5″ instead of “50%” → tiny gradient (use % with objectBoundingBox)
- No <stop> at 0% and 100% → gradient may look empty or wrong
- gradientUnits=”userSpaceOnUse” without adjusting numbers → gradient doesn’t scale with shape
- Too many stops too close → hard bands instead of smooth blend
Mini practice tasks
- Make a red-to-yellow sunset orb (centered radial gradient)
- Create a modern button look: blue circle + offset white highlight shine
- Try a metallic-looking sphere (silver gradient with offset focal point)
Paste your code here if you want feedback — I’ll review it like we’re pair-programming together 😊
Radial gradients are super powerful for giving flat shapes depth and realism — you now have both linear and radial in your toolbox!
Next we can cover repeating gradients, gradient on text/stroke, combining linear + radial, or animating gradients. Which direction sounds most fun to you? Or any part of radial gradients still fuzzy? Just say — we’ll zoom in until it’s crystal clear! 🚀
