<filter>

Adds a blur or shadow effect to an SVG tag on an HTML page.
3 min read
Believemy logo

The <filter> tag is used in an SVG document to apply graphical effects, such as blurs, drop shadows, or color blending, to SVG elements (not with the <svg> tag).

It serves as a container for various filter effects, allowing you to apply complex visual effects to your SVG elements.

Good to know

Use <filter> to centralize reusable filters in your SVG. Simplification! 😊

 

Structure and syntax of the <filter> tag

Structure

Here is a basic example of syntax:

HTML
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <filter id="blurFilter">
            <feGaussianBlur in="SourceGraphic" stdDeviation="5" />
        </filter>
   </defs>

    <circle cx="100" cy="100" r="50" fill="blue" filter="url(#blurFilter)" />
</svg>

In this example:

  • A filter (<filter>) is defined within <defs> with an id (#blurFilter);
  • The filter is then applied to the circle using filter="url(#blurFilter)".

Here is the result of this example:

 

Example

Here is an example using multiple filters to create a drop shadow effect:

HTML
<svg width="300" height="200" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <filter id="dropShadow">
            <feOffset dx="5" dy="5" result="offset" />
            <feGaussianBlur in="offset" stdDeviation="3" result="blur" />
            <feBlend in="SourceGraphic" in2="blur" mode="normal" />
        </filter>
    </defs>

    <text x="50" y="100" font-size="40" fill="black" filter="url(#dropShadow)">
        Shadowed Text
    </text>
</svg>

In this example:

  • <feOffset> offsets the text to create the shadow effect.
  • <feGaussianBlur> blurs the shadow.
  • <feBlend> combines the original text with the blurred shadow.

Here is the result:

 

Attributes of the <filter> tag

The <filter> tag accepts several attributes to customize its behavior:

AttributeDescription
idUnique identifier to reference the filter.
x, yPosition of the filter relative to the object (default is -10%).
width, heightDimensions of the filter. By default, they include the visual effects in the object's bounds.
filterUnitsUnits for dimensions (userSpaceOnUse or objectBoundingBox).
primitiveUnitsUnits for internal primitives (userSpaceOnUse or objectBoundingBox).

 

Best practices

Here are some tips for effectively using <filter>:

  • Always place your filters in a <defs> container to centralize their definition (your code will be simpler).
  • Use clear and meaningful ids for the filters defined.
  • Combine <filter> with CSS attributes or transformations to efficiently reuse graphical resources.
Good to know

For advanced usage, combine <filter> with other tags like <use> to create reusable groups.

 

Browser compatibility

The <filter> tag is supported by all modern browsers:

BrowserCompatibility
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Internet ExplorerPartial (IE 9+)

Related terms

Discover our hTML and CSS glossary

Browse the terms and definitions most commonly used in HTML and CSS development.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.