Chapter 21: SVG Drop Shadow 1
1. What is a Drop Shadow in SVG?
A drop shadow is a soft, dark, offset “shadow” underneath or behind a shape/text/icon — it makes things look like they are lifted off the page (3D/depth effect), exactly like box-shadow or filter: drop-shadow() in CSS.
In SVG we create it using a <filter> with these steps:
- Take the shape’s outline (alpha channel)
- Blur it (Gaussian blur)
- Move it down-right (offset)
- Color it dark + semi-transparent
- Put it behind the original shape
2. The Classic “Drop Shadow 1” Method (Long Version – Most Detailed Control)
This is the traditional way — uses <feGaussianBlur>, <feOffset>, <feFlood>, <feComposite>, and <feMerge>.
Copy-paste this complete example right now:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>SVG Drop Shadow – Classic Method</title> </head> <body> <h1>Classic SVG Drop Shadow (Method 1)</h1> <svg width="500" height="300" viewBox="0 0 500 300" style="border:1px solid #eee; background:#f8f9fa;"> <defs> <filter id="classicShadow" x="-50%" y="-50%" width="200%" height="200%"> <!-- Step 1: Blur only the alpha (shape outline) --> <feGaussianBlur in="SourceAlpha" stdDeviation="8" result="blur"/> <!-- Step 2: Move the blur down-right to create offset --> <feOffset in="blur" dx="10" dy="10" result="offsetBlur"/> <!-- Step 3: Color the shadow (black with 50% opacity) --> <feFlood flood-color="#000000" flood-opacity="0.5" result="shadowColor"/> <!-- Step 4: Apply color only where the blurred offset exists --> <feComposite in="shadowColor" in2="offsetBlur" operator="in" result="shadow"/> <!-- Step 5: Layer shadow behind original shape --> <feMerge> <feMergeNode in="shadow"/> <feMergeNode in="SourceGraphic"/> <!-- original shape on top --> </feMerge> </filter> </defs> <!-- Apply the filter --> <rect x="100" y="80" width="300" height="140" rx="20" fill="#ff6b6b" filter="url(#classicShadow)"/> <text x="250" y="170" font-size="48" text-anchor="middle" fill="white"> Classic Shadow </text> </svg> </body> </html> |
What you see:
- Red rounded rectangle with soft black shadow offset 10px down-right
- Text inside also gets the shadow (because it’s inside the filtered rect)
Why this is called “Drop Shadow 1” or classic:
- It was the only reliable way before <feDropShadow> became widely supported
- Gives full control over every step
- Works perfectly in all browsers (even older ones)
3. Breakdown – Each Primitive Explained Like Blackboard
- <feGaussianBlur in=”SourceAlpha” stdDeviation=”8″ result=”blur”/> → Blurs only the transparency mask (alpha) of the shape → creates soft edge shadow base
- <feOffset in=”blur” dx=”10″ dy=”10″ result=”offsetBlur”/> → Shifts the blur 10px right (dx) and 10px down (dy) → this is the “drop” part
- <feFlood flood-color=”#000000″ flood-opacity=”0.5″ result=”shadowColor”/> → Creates a solid color layer (black 50% opacity)
- <feComposite operator=”in” in=”shadowColor” in2=”offsetBlur”/> → Keeps color only where the blurred offset exists (like clipping)
- <feMerge> → Puts shadow layer first, then original shape (SourceGraphic) on top
4. Quick Comparison: Classic vs Modern <feDropShadow>
Modern short way (2020+ browsers – cleaner & faster):
|
0 1 2 3 4 5 6 7 8 |
<filter id="modernShadow"> <feDropShadow dx="10" dy="10" stdDeviation="8" flood-color="#00000080"/> </filter> |
→ Same result as the long version above, but one line!
When to use which?
- Use classic long method if:
- You need to support very old browsers
- You want to do advanced tricks (e.g., different color per channel, inner shadow variation)
- You see it in legacy code/tutorials
- Use <feDropShadow> if:
- Modern project (Chrome/Edge/Firefox/Safari 2026)
- Want clean, readable code
- Faster performance
5. More Fun Variations (Try Changing These!)
A. Softer / Bigger Shadow
|
0 1 2 3 4 5 6 |
stdDeviation="12" dx="15" dy="15" flood-opacity="0.4" |
B. Colored Shadow (Purple Shadow)
|
0 1 2 3 4 5 6 |
flood-color="#6a0dad" flood-opacity="0.6" |
C. Shadow Only on Text (Neon + Shadow Combo)
|
0 1 2 3 4 5 6 7 8 |
<text … fill="#00ffff" filter="url(#classicShadow)"> GLOW + SHADOW </text> |
D. Inner Shadow (Reverse Offset)
Change dx=”-8″ dy=”-8″ and adjust blur → looks like pressed button / inset
6. Teacher’s Quick Tips 😄
- Always set x=”-50%” y=”-50%” width=”200%” height=”200%” on <filter> → prevents clipping when shadow goes outside shape
- flood-opacity=”0.3–0.6″ = natural shadow (too dark = ugly)
- Combine with blur on text/icons → modern UI looks (neumorphism, glassmorphism)
- Common mistake: Forget in=”SourceAlpha” → blurs whole shape including fill color
- Animation idea: Animate stdDeviation or dx/dy → floating / depth effect
Understood SVG Drop Shadow (classic method) now? This is the foundation almost every SVG shadow tutorial starts with.
Tell me what you want next:
- Animated drop shadow (hover lift effect)?
- Inner shadow version?
- Shadow on imported photo / <image>?
- Multiple shadows (hard + soft)?
- Or move to glow / outline glow next?
Just say — we can build any shadow style together! 🚀
