SVG Filters

What You’ll Learn
How to apply visual effects to SVG elements using SVG filters. You define a filter inside <defs> using a <filter> element and then apply it with filter="url(#id)".
You’ll learn the basic syntax, the most common filter primitives (feGaussianBlur, feOffset, feColorMatrix, feMerge), plus two practical examples: a blur effect and a drop-shadow effect.
⚡ Quick Reference — SVG Filter Basics
<filter>Inside <defs> with an id
filterurl(#filterId) on the target element
feGaussianBlurSoftens edges and creates glow-like effects
feOffset + blurClassic drop shadow pipeline
<svg>
<defs>
<filter id="filter-id">
<!-- filter primitives go here -->
</filter>
</defs>
<rect filter="url(#filter-id)" />
</svg>👀 Live Preview — Blur & Shadow
These previews show two of the most common SVG filter effects. Scroll further down for the full code in the examples.
Blur Filter (feGaussianBlur)
This example defines a Gaussian blur filter and applies it to a rectangle. The stdDeviation value controls how strong the blur is.
<!DOCTYPE html>
<html>
<body>
<svg width="240" height="160" viewBox="0 0 240 160">
<defs>
<filter id="blur-filter" x="-20%" y="-20%" width="140%" height="140%">
<feGaussianBlur stdDeviation="5" />
</filter>
</defs>
<rect x="40" y="35" width="160" height="90" rx="14" fill="#2563eb" filter="url(#blur-filter)" />
</svg>
</body>
</html>Drop Shadow (Offset + Blur + Merge)
A classic drop shadow is created by offsetting the original alpha, blurring it, tinting it, then merging the shadow and the original graphic.
<svg width="240" height="160" viewBox="0 0 240 160">
<defs>
<filter id="shadow" x="-20%" y="-20%" width="140%" height="140%">
<feOffset in="SourceAlpha" dx="0" dy="10" result="off" />
<feGaussianBlur in="off" stdDeviation="8" result="blur" />
<feColorMatrix in="blur" type="matrix"
values="0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0.35 0" result="shadow" />
<feMerge>
<feMergeNode in="shadow" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</defs>
<circle cx="120" cy="70" r="42" fill="#f97316" filter="url(#shadow)" />
</svg>🧠 How It Works
Define a filter in <defs>
Filters live in <defs> so you can reuse them. Give the <filter> an id and reference it later.
Use filter primitives
Each primitive performs one image-processing step. For example, feGaussianBlur softens pixels, feOffset moves them, and feColorMatrix adjusts colour/opacity.
Expand the filter region
Shadows and blurs extend beyond the shape. Increase x, y, width, and height on <filter> to prevent clipping.
Apply with filter="url(#id)"
Add the filter attribute to any SVG element: filter="url(#shadow)". The browser renders the element through your filter pipeline.
Pro-level SVG effects
SVG filters can create blur, shadows, colour shifts, and stylised visuals while staying resolution-independent.
💡 Best Practices
Do
- Use
<defs>so filters are reusable - Expand the filter region to avoid clipped blur/shadows
- Keep effects subtle for performance (smaller blur values)
- Prefer a single shared filter instead of duplicating many
- Test on mobile devices if you use heavy filters
Don’t
- Forget
url(#id)— filters won’t apply without it - Leave the default filter region if you see clipped shadows
- Stack many expensive primitives on large SVGs without testing
- Use filters when a simple shape/gradient can achieve the same look
- Assume identical results across all older browsers
Key Takeaways
Define filters in <defs> using <filter>
Apply them with filter="url(#id)"
Use feGaussianBlur for blur and glow effects
Use offset + blur + merge for drop shadows
Expand the filter region to prevent clipping
❓ Frequently Asked Questions
<filter> (usually in <defs>) and apply it with filter="url(#id)". A filter is a pipeline of primitives that process pixels and output an effect.<filter> using x, y, width, and height. Shadows and blurs need extra space beyond the original shape.feOffset + feGaussianBlur, then combine the shadow with the original graphic using feMerge. This is the most compatible cross-browser approach.id and apply it anywhere using filter="url(#id)".Add Motion to Your SVGs
Next, learn how to attach events, hover states, and simple animations to SVG elements.
6 people found this page helpful
